How to append loop index of c:forEach tag to Struts HTML tag attributes?

早过忘川 提交于 2019-12-03 20:09:26

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.

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

Here's the 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 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.

That would be a nested expression, which is not allowed, try using this instead

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

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!