How to add loop index for c: forEach tag for Struts HTML tag attributes?

How can I add a c: forEach tag loop index to the attributes of a select attribute or a text tag?

For instance.

<%@ 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> 

produces the following error

javax.servlet.jsp.JspException at org.apache.struts.taglib.html.SelectTag.calculateMatchValues(SelectTag.java:246)

Now, when I debug the code in <html:select ... , it shows that with the property attribute set, its value "title_guest<c:out value="${gC.index}"/>" may be the cause of the above exception.

In addition, I should mention that if I use the above format to add a loop index to the standard attribute of the html tag, such as the <select> , the code works fine.

for instance

 <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> 

The correct HTML output

What am I doing wrong, should I use EL to create a string that will populate the property attribute of the html: select tag?

UPDATE

The following snippet was also tested, and this did not work <html:select property="title_guest${gC.index}">

And it also does not work

 <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> 
+4
source share
3 answers

After some painful digging, I seem to have found a problem and therefore a solution. The c: forEach tag does not export varStatus as a script variable, so the varStatus variable cannot be used in RT Expr for the html: property attribute : select the tag .

However, c: forEach exports the varStatus variable as a pageContext attribute, which can be retrieved and used to retrieve the index / count. The only catch is that you have to import the javax.servlet.jsp.jstl.core.LoopTagStatus class and use it to manually recreate the varStatus variable so that it can be used inside the script

Here is a snippet of code that worked

 <%@ 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 do not think this is a pure solution (but may be the only solution). Therefore, I will allow the community to vote for this answer first (or write a better answer) before I accept it as the final answer.

+8
source

This will be a nested expression that is not allowed, try using this instead

 <html:select property='title_guest${gC.index}'> 
0
source

My way

  <c:forEach begin="1" end="${page.totalPages}" varStatus="lp"> <li><a href="<c:url value="/course?page=${pageScope.lp.index}"/>">${pageScope.lp.index}</a></li> </c:forEach> 
0
source

All Articles