Java 在scriptlet数组索引中访问struts迭代器的索引值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19856296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
To access the index value of struts iterator in scriptlet array index
提问by
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.
我是网络开发的新手。使用 Struts2,我有一个逗号分隔String
我的图像标题。在迭代图像以在 JSP 上呈现时,我需要将标题与特定图像一起显示,并且无法获得任何特定标签以String
通过分隔符分割标题并访问特定标题。我正在尝试下面的代码,但不知道用什么来代替某些东西来获取迭代器中的当前迭代索引。
<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.
我知道不建议在 JSP 中使用 scriptlet 和表达式标记,但我不知道如何避免它。
Any help will be appreciated.
任何帮助将不胜感激。
采纳答案by Roman C
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
当前迭代索引可通过标签的status
属性获得s:iterator
。在你的情况下是#incr.index
. 如果要显示该索引
<s:iterator value="images" status="incr">
<s:property value="%{#incr.index}"/>
then scriplet could be changed to OGNL expression
那么scriplet可以改为OGNL表达式
<s:property value='#attr.imageCaptionsString.split(",")[%{#incr.index}]'/>
回答by coding_idiot
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.
如果这不起作用,那么可能是因为它是一个请求属性,因此不能直接在 valueStack 上使用,在这种情况下,您可以在迭代器中使用#attr.imageCaptionsString
而不是普通imageCaptionsString
的。
回答by coding_idiot
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.
经过一番挣扎并在 Roman C 先生建议的答案的帮助下,我得到了解决方案并保留在这里,以供将来有需要的用户使用。
<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.
这样我就得到了字幕。感谢罗曼 C 先生。