Java EL係列-2.JUEL快速入門
inkfish翻譯,請勿商業性質轉載,轉載請注明來源(https://blog.csdn.net/inkfish )。本文是我學習JUEL同時,對原網站進行的簡單的翻譯,原網站地址:https://juel.sourceforge.net/guide/start.html 。
快速入門
JUEL 發行版包含下麵一些jar文件:(來源:https://blog.csdn.net/inkfish)
1.juel-api-2.2.x.jar
——包含javax.el
包下的一些類
2.juel-impl-2.2.x.jar
——包含de.odysseus.el
實現類
3.juel-spi-2.2.x.jar
——包含META-INF/service/javax.el.ExpressionFactory
服務提供資源的定義(如果你的classpath裏有多個EL的實現,而你又希望使用JUEL的實現,那麼需要調用ExpressionFactory.newInstance()
)
4.juel-2.2.x.jar
——包含java.el
包下的類,並且包含de.odysseus.el
下的實現類,而且還有服務提供接口(spi)
也就是說:juel-2.2.x.jar = juel-api-2.2.x.jar + juel-impl-2.2.x.jar + juel-spi-2.2.x.jar
。(來源:https://blog.csdn.net/inkfish)
下麵是所有你在你的應用中使用EL所需要的(假設你已經把juel-2.2.x.jar
放到classpath下,並且導入了javax.el.*
):(來源:https://blog.csdn.net/inkfish)
1.工廠和上下文(來源:https://blog.csdn.net/inkfish)
//ExpressionFactory類的實現是de.odysseus.el.ExpressionFactoryImpl ExpressionFactory factory = new de.odysseus.el.ExpressionFactoryImpl(); //de.odysseus.el.util provides包提供即時可用的子類ELContext de.odysseus.el.util.SimpleContext context = new de.odysseus.el.util.SimpleContext();
2. 函數和變量(來源:https://blog.csdn.net/inkfish)
//設置map函數math:max(int, int)使用java.lang.Math.max(int, int) context.setFunction("math", "max", Math.class.getMethod("max", int.class, int.class)); //map變量foo設置為0 context.setVariable("foo", factory.createValueExpression(0, int.class));
3. 解析和求值(來源:https://blog.csdn.net/inkfish)
//解析表達式 ValueExpression e = factory.createValueExpression(context, "${math:max(foo,bar)}", int.class); //設置頂級的屬性"bar"值為1 factory.createValueExpression(context, "${bar}", int.class).setValue(context, 1);
最後更新:2017-04-02 04:01:45