JSP/JSTL question

Z

Zane

Hi,
I am new to using the JSTL library and have a quick question that I
cannot find the qnswer to. Basically I have a form that is posted to a
JSP page which basically needs to print out the parameters passed to
it in the request. Where I am having trouble is that I am using the
forEach tag to iterate which prints out unique keys fine, but it when
it comes to checkboxes with the same key it only prints out the first
check box instead of all of them. For example say the JSP was passed
the following request header via a GET method:

chess.jsp?piece=pawn&piece=rook&piece=queen

and the jsp has the following code:

<c:forEach items="${param}" var="chessPiece">
<c:eek:ut value"${chessPiece.value}" />
</c:forEach>

the output is just:

pawn

instead of:

pawn rook queen

What am I missing?

Thanks,

Zane
 
C

Chris Smith

Zane said:
chess.jsp?piece=pawn&piece=rook&piece=queen

and the jsp has the following code:

<c:forEach items="${param}" var="chessPiece">
<c:eek:ut value"${chessPiece.value}" />
</c:forEach>

the output is just:

pawn

instead of:

pawn rook queen

What am I missing?

In JSTL terms, you have one parameter, called "piece", and it has three
different values. You can get to those values with the map called
'paramValues'. The trick is that the 'value' of an element of
'paramValues' is actually a list of it's own, so you'd accomplish
something similar to your code above by writing:

<c:forEach items="${paramValues}" var="pieces">
<c:forEach items="${pieces.value}" var="chessPiece">
<c:eek:ut value="${chessPiece}" />
</c:forEach>
</c:forEach>

Because this (having multiple values for the same parameter) is an
unusual case and would make the EL difficult when you AREN'T expecting
multiple values, JSTL also provides a simpler way to look at parameters:
'param' is a map containing each parameter's *first* value only (though
you'd typically only use it when you only expect there to be one value
anyway). So, think of 'param' as a useful shortcut, but if you want the
whole picture, use 'paramValues'.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

JSTL attempt in JSP 5
JSTL Question 10
Beyond trivial JSTL 1
View page without JSTL 2
JSTL: getting a map's keys 19
page counter in JSP & JSTL 0
JSP works, JSTL doesn't 4
problem in jsp/jstl 2

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top