在没有Servlet容器的情况下评估JSP EL [英] Evaluating JSP EL without a Servlet container

查看:61
本文介绍了在没有Servlet容器的情况下评估JSP EL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

Map<String, Object> model = new Hashmap<String, Object>();
model.put("a", "abc");
model.put("b", new Hashmap<String, Object>());
model.get("b").put("c", "xyz");
String el = "A is ${a} and C is ${b.c}";
assertEquals(elEval(el, model), "A is abc and C is xyz");

这可能吗?

推荐答案

是的,有可能,您可以参考此 JUEL (它是EL表达式的实现之一)已经在以下版本中提供了这些类的很好的实现de.odysseus.el.util软件包.

Yes , it is possible , you can refer to this link for more information . As you can see , in order to use the EL expression standalone , you would have to implement several classes such as the javax.el.ELContext . I found JUEL , which is one of the implementation of the EL expression , already provides very nice implementations of these classes in the de.odysseus.el.util package.

我玩过JUEL,这是我的测试代码供您参考:

I have played around with JUEL .Here is my testing code for your reference:

/*
ExpressionFactoryImpl should be the implementation of ExpressionFactory used by  your application server. 
For example , in tomcat 7.0 , it is org.apache.el.ExpressionFactoryImpl , which is inside the jasper-el.jar .
jasper-el.jar  is the implemenation of EL expression provided by tomcat  , el-api.jar is the API of EL expression (i.e. JSR-245)
*/
ExpressionFactory factory = new ExpressionFactoryImpl();

/*
SimpleContext is the utility classes from fuel 
*/
SimpleContext context = new SimpleContext();    

//Set the variables in the context  
Map<String,Object> hashMap =  new HashMap<String,Object>();
hashMap.put("c", "xyz");
context.setVariable("a", factory.createValueExpression("abc", String.class));   
context.setVariable("b", factory.createValueExpression(hashMap, HashMap.class));    

//Create the EL expression 
ValueExpression expr = factory.createValueExpression(context,  "A is ${a} and C is ${b.c}", String.class);  
System.out.println(expr.getValue(context));

这篇关于在没有Servlet容器的情况下评估JSP EL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆