0

I was working the expressions and trying to play with the contexts (the one we use at creation time and the one we use at evaluation time). Here is some code below that is trying to reproduce my needs and highlight the problem.

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

I setup a basic Maven project on https://github.com/laurentvaills/test-juel-expression to reproduce it .

Can you tell me why I got the following error : javax.el.PropertyNotFoundException: Cannot find property exchange ?

4

1 回答 1

0

这不是 JUEL 问题,而是一般 EL 问题。变量在解析时绑定。一旦解析了表达式,就无法更改它们。在评估时,您需要改用属性:

evalContext.getELResolver().setValue(
    evalContext,
    null,
    "exchange",
    new MyExchange(host));

有关详细信息,请参阅ELResolver 的文档

于 2015-01-28T16:17:35.203 回答