Search code examples
javaformsjspstruts2ognl

In Struts2, dynamically generate form element name using a member value


My requirement: In Struts 2, dynamically generate form element name by using member value.

I had tried with: <s:textarea name="employee_<s:property value='employeeNumber'/>"/>

Resulting code in web browser: <textarea name="employee_&lt;s:property value='employeeNumber'/&gt;"></textarea>

My expectation in web browser code: <textarea name="employee_101"></textarea>. 101 as an employeeNumber is only used for demonstration only.

Please help, thanks!


Solution

  • In Struts2 you can always use OGNL expression in the Struts tag's attribute. Just provide the value from the value stack. You should know that the action bean is on top of it, and having a getter for employeeNumber should return desired value 101.

    <s:textarea name="employee_%{employeeNumber}"/>
    

    Also be aware of Struts doesn't allow nested tags in the attributes in favor of OGNL.