我正在处理这些表达式,并试图处理上下文(我们在创建时使用的上下文和在评估时使用的上下文)。下面是一些代码,试图重现我的需求并突出问题。
ExpressionFactory factory = new ExpressionFactoryImpl();
SimpleContext createContext = new SimpleContext();
createContext.setVariable("myBean", factory.createValueExpression(new MyBean("Laurent"), MyBean.class));
String expression;
expression = "${myBean.foo} ${exchange.host}";
ValueExpression expr = factory.createValueExpression(createContext, expression, String.class);
System.out.println("expr Class : " + expr.getClass());
SimpleContext evalContext = new SimpleContext();
List<String> hosts = asList("www.example.com", "www.foo.com", "www.bar.com");
// I want to evaluate the same expression, but with different values for the variable exchange.
for (String host : hosts) {
evalContext.setVariable("exchange", factory.createValueExpression(new MyExchange(host), MyExchange.class));
System.out.println(expression + " = " + expr.getValue(evalContext));
}我在https://github.com/laurentvaills/test-juel-expression上设置了一个基本的Maven项目来再现它。
你能告诉我为什么会出现以下错误吗:javax.el.PropertyNotFoundException: Cannot find property exchange ?
发布于 2015-01-28 16:17:35
这不是朱尔的问题,而是一个普遍的问题。变量在解析时被绑定。一旦一个表达式被解析,它们就不能被更改。在评估时,您需要使用属性来代替:
evalContext.getELResolver().setValue(
evalContext,
null,
"exchange",
new MyExchange(host));有关细节,请参阅ELResolver文档。
https://stackoverflow.com/questions/28194123
复制相似问题