Jakarta Commons Jexl 起步

jopen 12年前發布 | 1K 次閱讀
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
 
下面是一個應用實例:
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);

    }
}
 
運行結果:
對表達式 country 求值結果: CHINA
對表達式 person.getName() 求值結果: zhangsan
對表達式 person.getName().length() 求值結果: 8
對表達式 person.getAge() 求值結果: 22

Process finished with exit code 0
 
jexl包是很多模板工具的核心依賴包。
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!