Search code examples
jspstruts2ognl

Struts ognl expression to evalulate the result of the expression


This is going to be a little tricky to explain. I'm trying to write a tag to componentise a bunch of address fields, but I'm having trouble working out the ognl expression.

Expected usage:

member.address maps to an Address object (nothing too cleaver).

my tag (simplest version):

<%@taglib prefix="s" uri="/struts-tags" %>
<%@attribute name="name" required="true" rtexprvalue="true" type="java.lang.String" %>
<s:push value="%{#attr.name}">
    Address line 1:
    <s:property value="line1"/>
</s:push>

I think the issue is that <s:push value="%{#attr.name}"/> isn't actually pushing the result of member.address onto the stack it's just pushing a String of value 'member.address' instead.


Solution

  • A little more research and a long time staring at the ognl documentation results in the following:

    <%@taglib prefix="s" uri="/struts-tags" %>
    <%@attribute name="name" required="true" rtexprvalue="true" type="java.lang.String" %>
    <s:push value="%{(#attr.name)(#attr)}">
       Address line 1:
       <s:property value="line1"/>
    </s:push>
    

    Seems todo the trick.