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 に答える