To access the index value of struts iterator in scriptlet array index

user2674752 picture user2674752 · Nov 8, 2013 · Viewed 27.6k times · Source

I am a newbie in web development. 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.

Any help will be appreciated.

Answer

Roman C picture Roman C · Nov 8, 2013

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}]'/>