Jakarta Commons Jexl 起步
Jakarta Commons Jexl是一种表达式语言(Java Expression Language)解释器, 它深受JSP2.0中表达式语言(EL)特征的影响,Commons Jexl是对JSP表达式语言的一种扩充,并且不依赖于Servlet API。这就意味着它可以被集成到任何需要表达式语言的应用程序中。
jaxl依赖logging包,因此运行jexl最小包集合为:
commons-jexl-1.1.jar
commons-logging.jar
commons-logging.jar
下面是一个应用实例:
import org.apache.commons.jexl.Expression;
import org.apache.commons.jexl.JexlHelper;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.ExpressionFactory;
/**
* Created by IntelliJ IDEA.<p>
* User: leizhimin<p>
* Date: 2008-8-16 19:14:39<p>
* Apache Commons JEXL 测试
*/
public class TestJexl {
public static void main(String[] args) throws Exception {
testBasic();
}
public static void testBasic() throws Exception {
Person person = new Person( "zhangsan", "men", 22);
JexlContext jexlContext = JexlHelper.createContext();
jexlContext.getVars().put( "country", person.country);
jexlContext.getVars().put( "person", person);
/* 创建一个表达式country */
Expression expression1 = ExpressionFactory.createExpression( "country");
/* 结合上下文对表达式求值 */
Object message1 = expression1.evaluate(jexlContext);
System.out.println( "对表达式 country 求值结果: " + message1);
Expression expression2 = ExpressionFactory.createExpression( "person.getName()");
Object message2 = expression2.evaluate(jexlContext);
System.out.println( "对表达式 person.getName() 求值结果: " + message2);
Expression expression3 = ExpressionFactory.createExpression( "person.getName().length()");
Object message3 = expression3.evaluate(jexlContext);
System.out.println( "对表达式 person.getName().length() 求值结果: " + message3);
Expression expression4 = ExpressionFactory.createExpression( "person.getAge()");
Object message4 = expression4.evaluate(jexlContext);
System.out.println( "对表达式 person.getAge() 求值结果: " + message4);
}
}
import org.apache.commons.jexl.JexlHelper;
import org.apache.commons.jexl.JexlContext;
import org.apache.commons.jexl.ExpressionFactory;
/**
* Created by IntelliJ IDEA.<p>
* User: leizhimin<p>
* Date: 2008-8-16 19:14:39<p>
* Apache Commons JEXL 测试
*/
public class TestJexl {
public static void main(String[] args) throws Exception {
testBasic();
}
public static void testBasic() throws Exception {
Person person = new Person( "zhangsan", "men", 22);
JexlContext jexlContext = JexlHelper.createContext();
jexlContext.getVars().put( "country", person.country);
jexlContext.getVars().put( "person", person);
/* 创建一个表达式country */
Expression expression1 = ExpressionFactory.createExpression( "country");
/* 结合上下文对表达式求值 */
Object message1 = expression1.evaluate(jexlContext);
System.out.println( "对表达式 country 求值结果: " + message1);
Expression expression2 = ExpressionFactory.createExpression( "person.getName()");
Object message2 = expression2.evaluate(jexlContext);
System.out.println( "对表达式 person.getName() 求值结果: " + message2);
Expression expression3 = ExpressionFactory.createExpression( "person.getName().length()");
Object message3 = expression3.evaluate(jexlContext);
System.out.println( "对表达式 person.getName().length() 求值结果: " + message3);
Expression expression4 = ExpressionFactory.createExpression( "person.getAge()");
Object message4 = expression4.evaluate(jexlContext);
System.out.println( "对表达式 person.getAge() 求值结果: " + message4);
}
}
运行结果:
对表达式 country 求值结果: CHINA
对表达式 person.getName() 求值结果: zhangsan
对表达式 person.getName().length() 求值结果: 8
对表达式 person.getAge() 求值结果: 22
Process finished with exit code 0
对表达式 person.getName() 求值结果: zhangsan
对表达式 person.getName().length() 求值结果: 8
对表达式 person.getAge() 求值结果: 22
Process finished with exit code 0
jexl包是很多模板工具的核心依赖包。