Search code examples
jspstruts2jstlstrutsognl

Generate name and value attribute dynamically in Struts 2


I am migrating the code from Struts1 to Struts2

Struts1 code

<input type="text" value="<c:out value="${serviceVO.notifList}"/>" name="ServicesNotifList-<c:out value="${serviceVO.globalId}"/>#<c:out value="${serviceVO.id}"/>" size="50" maxlength="1000"/>

in Struts2

I tried but not working

<c:set var="notifListTemp" value="ServicesNotifList-"></c:set>
<c:set var="notifListTemp1" >${notifListTemp}${serviceVO.globalId}</c:set>
<c:set var="notifListTemp2" value="#"></c:set>
<c:set var="notifListTemp3" >${notifListTemp1}${notifListTemp2}${serviceVO.id}</c:set>

<s:textfield theme="simple" value="${serviceVO.notifList}" name="${notifListTemp3}" 
 size="50" maxlength="1000"  />

where serviceVO is display:table id.


Solution

  • You can't use JSP EL expressions in Struts tag's attributes, but happily you can use OGNL expressions. Objects that are not in the value stack accessed via #attr.

    <s:textfield theme="simple" value="%{#attr.serviceVO.notifList}" name="%{#attr.notifListTemp3}" 
         size="50" maxlength="1000"  />