如何将c:forEach标记的循环索引附加到Struts HTML标记属性? [英] How to append loop index of c:forEach tag to Struts HTML tag attributes?
问题描述
如何将c:forEach标记的循环索引附加到struts select/text标记的属性?
How can I append the loop index of a c:forEach tag to the attributes of a struts select/text tag?
例如.
<%@ taglib uri="/WEB-INF/tld/struts-html.tld" prefix="html"%>
<c:forEach begin="2" end="${pageView.guestCount}" varStatus="gC">
<div class="section guest-details">
<html:select property='title_guest<c:out value="${gC.index}"/>'>
<html:options collection="titles" property="code" labelProperty="value" />
</html:select>
</div>
</c:forEach>
引发以下错误
javax.servlet.jsp.JspException at org.apache.struts.taglib.html.SelectTag.calculateMatchValues(SelectTag.java:246)
现在,当我在<html:select ...
处调试代码时,它表明当设置属性属性时,将其设置为"title_guest<c:out value="${gC.index}"/>"
可能是导致上述异常的原因.
Now, when I debug the code at <html:select ...
it shows that when the property attribute it set, its set as "title_guest<c:out value="${gC.index}"/>"
which could be the cause of the exception above.
另外,我应该提到的是,如果我使用上述格式将循环索引附加到标准的html标记属性(例如<select>
标记),则代码可以正常工作.
Also, I should mention, that if I use the above format for appending the loop index to a standard html tag attribute like a <select>
tag, the code works fine.
例如
<c:forEach begin="2" end="${pageView.guestCount}" varStatus="gC">
<div class="section guest-details">
<select name='title_guest<c:out value="${gC.index }"/>'>
<option value="">Select Title</option>
</select>
</div>
</c:forEach>
正确输出预期的HTML
Correctly outputs the intended HTML
我在做什么错,我应该使用EL创建将填充html:select标记的"property"属性的字符串吗?
What am I doing wrong, should I be using EL to create the string that will populate the "property" attribute of the html:select tag?
更新
还尝试了以下代码段,但该代码段也无效
<html:select property="title_guest${gC.index}">
The following snippet was also tried and that didn't work either
<html:select property="title_guest${gC.index}">
而且,这也行不通
<c:set var="guestTitle">title_guest${gC.index}</c:set>
<html:select property="${guestTitle}" styleClass="{required: true}">
<html:options collection="titles" property="code" labelProperty="value" />
</html:select>
推荐答案
经过一番痛苦的挖掘,我似乎找到了问题所在,因此找到了解决方案. c:forEach 标记不会将varStatus导出为脚本变量,因此无法在 RT Expr 中将varStatus变量用于属性 html:select 标签的属性.
After some painful digging around, I seemed to have found the problem and hence the solution. The c:forEach tag does not export the varStatus as a scripting variable and therefore the varStatus variable can not be used in the RT Expr for the property attribute of the html:select tag.
但是, c:forEach 确实将varStatus变量导出为pageContext属性,可以将其检索并用于提取索引/计数.唯一的问题是,您将必须导入javax.servlet.jsp.jstl.core.LoopTagStatus类,并使用该类手动重新创建varStatus变量,以便可以在脚本中使用它
However, the c:forEach does export the varStatus variable as a pageContext attribute, which can be retrieved and used to extract the index/count. The only catch is that you will have to import the javax.servlet.jsp.jstl.core.LoopTagStatus class and use that to manually recreate the varStatus variable so that it can be used inside a scriplet
这是有效的代码段
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
import="javax.servlet.jsp.jstl.core.LoopTagStatus"
%>
...
<c:forEach begin="2" end="${pageView.guestCount}" varStatus="gC">
<% LoopTagStatus gN = (LoopTagStatus)pageContext.getAttribute("gC"); %>
<html:select property='<%="title_guest"+gN.getIndex()%>'>
<html:options collection="titles" property="code" labelProperty="value" />
</html:select>
</c:forEach>
我认为这不是一个干净的解决方案(但可能是唯一的解决方案).因此,在我将其作为最终答案之前,我将让社区首先对此答案进行投票(或写出更好的答案).
I don't think this is a clean solution (but could be the only solution). Therefore, I will let the community vote on this answer first (or write a better answer), before I accept it as the final answer.
这篇关于如何将c:forEach标记的循环索引附加到Struts HTML标记属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!