Search code examples
javajspstruts2lambdaognl

Lambda expressions in Struts 2


I'm still using Struts 2.3.16. I have round tripped the Struts2 documentation and tried to use lambda expression on my JSP page. But it doesn't work. Also in my previous answers I have used a parenthesized expressions to evaluate nested expressions in OGNL. But it seems doesn't work anymore.

This is lambda expression that doesn't work

<s:property value="#fib =:[#this==0 ? 0 : #this==1 ? 1 : #fib(#this-2)+#fib(#this-1)], #fib(11)" />  

This is parenthesized expression from the answer that doesn't work

<% String str27 = "hello27"; %>
<%ActionContext.getContext().getValueStack().set("str27", str27); //set to root %>
<s:property value="str27"/> <br>
<%ActionContext.getContext().getValueStack().getContext().put("str27", str27); //set to context %>
<s:property value="#str27"/> <br>
<s:set var="str28" value="'str27'"/>

str28: <s:property value="%{(#str28)(0)}"/><br>

I also know that it works if I use value="%{#attr[#str28]}".


Solution

  • The evaluation of OGNL expressions is disabled by default since 2.3.14.1 because of security vulnerability (see S2-013 for more details).

    Of course you can enable evaluation by setting struts.ognl.enableOGNLEvalExpression to true.

    <constant name="struts.ognl.enableOGNLEvalExpression" value="true" />
    

    But it is not recommended.

    Instead use:

    <s:property value="#attr[some_var]"/>