0

I have 5 variables that need to be used inside a foreach thats opened, so I named each variable like IMG1 IMG2 IMG3 IMG4 IMG5. I would like to know how to call the src of the image like this

src="${IMG + index of the foreach}" 

so it call foreach a new image.

edit:

i have this five variables

<c:set var="imgPN1" value="23421E1" />
<c:set var="imgPN2" value="32543QS" />
<c:set var="imgPN3" value="23945ZS" />
<c:set var="imgPN4" value="23421E0" />
<c:set var="imgPN5" value="34352WS" />

and inside a foreach i need to place each of this variable in an image src so, the problem is that i count use another foreach inside the foreach already used, cause will generate 25 img instead of 5, so what i would need, is using varstatus or some index, be able to do inside the foreach already created something like this so it picks up the variable with its proper name.

2 Answers2

1

I understood that you've prepared the images as follows for some reason:

request.setAttribute("IMG1", img1);
request.setAttribute("IMG2", img2);
request.setAttribute("IMG3", img3);
request.setAttribute("IMG4", img4);
request.setAttribute("IMG5", img5);

To concretely answer your question, you first need to create another variable which represents the scoped attribute name with <c:set>, then you can use this as key of the desired scope map, such as ${requestScope} for request attributes:

<c:forEach begin="1" end="5" varStatus="loop">
    <c:set var="imgId" value="IMG${loop.index}" />
    <img src="${requestScope[imgId]}" />
</c:forEach>

However, this is a rather bad way of doing it. This is a fairly clumsy and illogical approach. It would make so much more sense to prepare those closely related variables in an array or a collection.

String[] images = new String[] { img1, img2, img3, img4, img5 };
request.setAttribute("images", images);

So that you can access it as follows:

<c:forEach items="${images}" var="image">
    <img src="${image}" />
</c:forEach>

You can even access the item at a specific index directly when not inside a loop:

<img src="${images[2]}" /> <!-- returns img3 -->
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • actually don´t know if it changes anything but they´re set with and the problem is that i´m already in a foreach so if a use the foreach AGAIN, it´ll multiply again and post 25 images instead of the 5 that i need, what i really need is to concat the variable name, so it calls ${IMG + an index} so when it does the for each, will call each different source. – Sebastian Uriel Murawczik Apr 22 '13 at 15:42
  • Sorry, I have no idea what you mean. Have you tried the code in the answer? You sound like as you didn't understood it and actually never tried it. Otherwise, edit your question to include an SSCCE which shows how you're preparing the data and how exactly you'd like the generated HTML to end up look like. – BalusC Apr 22 '13 at 15:43
  • 1
    Ah, you've set them all with `` in its default scope (which is page scope)? Just use `${pageScope}` instead of `${requestScope}` in my answer. – BalusC Apr 22 '13 at 15:48
0

In JSP-EL + is purely for numbers. Try doing this instead:

src="${IMG}${index}"
Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125