Search code examples
javajspstruts2ognlstruts-tags

Output parametric bean property with struts tags


I got a bean beanName and a variable paramName that holds the name of the property to be used so that:

<s:textfield name="%{'beanName.' + paramName}"/>

outputs (given paramName == "year" and beanName.getYear() == 1976):

<input type="text" name="beanName.year" value="1976" >

How am I supposed to use <s:property> to output the same property value?

I'd suppose that:

<s:push value="beanName">
   <s:property value="%{paramName}"/>
</s:push>

would do that, but it just writes paramName value.


Solution

  • You need to evaluate paramName first before using it to get value from the bean.

    Use square brackets for that:

    <s:property value="beanName[paramName]" />
    

    OGNL firstly gets value of paramName and then gets value from beanName with this resolved variable.