Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483465
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 17:24:23

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.


* User: leizhimin


* Date: 2008-8-16 19:14:39


* 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);

    }
}
--------------------next---------------------


阅读(144) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~