7

Using Struts2, I have a comma separated String of my images captions. While iterating the images to render on JSP I need to display caption along with specific images and could not get any specific tag to split caption String over delimiter and to access specific caption. I am trying the below code and don't know what to use in place of something to get the current iteration index in iterator.

<s:iterator value="images" status="incr">  
  <%= ((String)request.getAttribute("imageCaptionsString")).split(",")[something]%>
</s:iterator>

I know that using scriptlets and expression tags in JSP are not recommended, but I don't have any idea how to avoid it.

3
  • Yes you are right about scriptlets, but what do you mean by "expression tags"? Commented Nov 8, 2013 at 10:41
  • expression tags to evaluate and display the expressions <%= %> Commented Nov 8, 2013 at 10:54
  • 1
    For this purposes use OGNL. Commented Nov 8, 2013 at 11:07

3 Answers 3

11

The current iteration index is available via the status attribute of the <s:iterator> tag. In your case is #incr.index. If you want to display that index

<s:iterator value="images" status="incr"> 
  <s:property value="%{#incr.index}"/>

then scriplet could be changed to OGNL expression

<s:property value='#attr.imageCaptionsString.split(",")[%{#incr.index}]'/>
Sign up to request clarification or add additional context in comments.

3 Comments

No Luck ! It is not displaying anything without any error. What is attr here ?
value="#attr.imageCaptionsString" is giving me the entire caption String but split method don't work. Any suggestion/idea??
I have made update to the expression, could you check if it works?
1

After struggling a lot and with the help of answer suggested by Mr. Roman C, I got the solution and keeping here for sake of any future needy user.

<s:iterator value="images" status="incrementer">
  <s:set var="cnt" value="%{#incrementer.index}" />
  <s:property value="#attr.imgCaptions.get(#cnt)"/>
</s:iterator>

This way I got the captions. Thanks to Mr. Roman C.

Comments

0

This should do

<s:iterator value="imageCaptionsString.split(",")">  
    <s:property/>
</s:iterator>

If that doesn't work, then might be because it's a request attribute & hence, not available directly on valueStack, in which case you can use #attr.imageCaptionsString instead of plain imageCaptionsString in the iterator.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.