c:forEach - values

Hello All,

I have something like this in my jsp...

<c:forEach var="value" items="${List}">

<tr>

<td>

<c:out value="${"value.name}" />

</td>

<td>

<input type="text" id="roll" name="roll" maxlength="5"/>

</td>

</tr>

</c:forEach>

i.e., I have a table with all the names displayed and the user has to enter his roll # in he text box beside the name...my question is how can i get the roll # associated with that particular roll ...I know we can use request.getParameter("roll") but since it is <c:forEach> I donno how to get the values in my action class...

Help is greatly appreciated...

Thanks,

Greeshma...

[769 byte] By [Greeshma12a] at [2007-11-27 6:23:40]
# 1
What about this, so that you can get the associated value.<input type="text" id="roll" name="roll" maxlength="5" value='<c:out value="${"value.name}" />' />
skp71a at 2007-7-12 17:41:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks for your reply ...but I need the name , roll combination in my java class once the form is submitted , so that i can perform some validations ...Thanks,Greeshma...Message was edited by: Greeshma12
Greeshma12a at 2007-7-12 17:41:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

The standard approach is to give each control on the screen a unique name (normally numbering them).Give all controls in one iteration the same number, that way you can associate them together.

Frameworks like Struts/JSF etc will automate this for you. The Commons BeanUtils package also has a couple of helper methods for populating beans from request parameters in this fashion.

The basic idea:

<c:forEach var="value" items="${List}" varStatus="status">

<tr>

<td>

<c:out value="${"value.name}" />

</td>

<td>

<input type="hidden" name="userid_${status.index}" value="${value.id}">

<input type="text" id="roll" name="roll_${status.index}" maxlength="5"/>

</td>

</tr>

</c:forEach>

This will create fields like this:

<input type="hidden" name="userid_0" value="42">

<input type="text" id="roll" name="roll_0" maxlength="5"/>

<input type="hidden" name="userid_1" value="69">

<input type="text" id="roll" name="roll_1" maxlength="5"/>

<input type="hidden" name="userid_2" value="666">

<input type="text" id="roll" name="roll_2" maxlength="5"/>

Then in your java code you need code something like

int p = 0;

String paramValue = request.getParameter("roll_" + p);

while (paramValue != null){

String paramId = request.getParameter("userid_" + p);

// handle one mapping of paramId and paramValue

p++;

paramValue = request.getParameter("roll_" + p);

}

Note I added a "userid" field which is meant to be used to identify each row with a user, rather than just using the index number.

Its a fairly straight-forward approach, if a little tricky to implement.

As stated before, most frameworks will handle this sort of thing for you, so you don't need to. Still its nice to know what goes on under the hood.

Cheers,

evnafets

evnafetsa at 2007-7-12 17:41:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Thanks evnafets :)It really helps
Greeshma12a at 2007-7-12 17:41:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Thanks for your reply

int p = 0;

String paramValue = request.getParameter("roll_" + p);

while (paramValue != null){

String paramId = request.getParameter("userid_" + p);

// handle one mapping of paramId and paramValue

p++;

request.setAttribute("roll_" + p, paramValue );

paramValue = request.getParameter("roll_" + p);

}

'request.setAttribute("roll_" + p, paramValue ); ' how to retrive the value of this paramater in my jsp ...as I'm using JSTL 1.0, I'm not able to utilize JSTL completely ...

<c:forEach var="value" items="${List}" varStatus="status">

<tr>

<td>

<c:out value="${"value.name}" />

</td>

<td>

<input type="hidden" name="<c:out value="userid_${status.index}" />" value="<c:out value ="${value.id}"/>" />

<input type="text" id="roll" name="<c:out value="roll_${status.index}"/>" maxlength="5"

value= "?"/>

</td>

</tr>

</c:forEach>

Help is really appreciated...

Thanks...

Greeshma12a at 2007-7-12 17:41:52 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...