I wrote a JSP with two parameters filter and filter_t. I want to split the parameter by ',' and create a label for each filter_t and input field for each filter. So I wrote code like below:
<c:forEach var="splt_t" items="${fn:split(param.filter_t,',')}">
<label>${splt_t}</label>
</c:forEach>
<c:forEach var="splt" items="${fn:split(param.filter,',')}">
<input type="text" name="${splt}" />
</c:forEach>
But in this it create two labels first and then two input fields. If I want to one label and one input field, how to modify the code? I'm sure these two parameters contain same number of ','. Thx.
Here is code that demonstrates one solution.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix = "fn" uri = "http://java.sun.com/jsp/jstl/functions" %>
<%
String f = "a,b,c,d";
String t = "1,2,3,4";
pageContext.setAttribute("f", f);
pageContext.setAttribute("t", t);
%>
<c:forEach var="splt_t" items="${fn:split(t,',')}" varStatus="tStatus">
<label>${splt_t}</label>
<c:forEach var="splt" items="${fn:split(f,',')}" varStatus="fStatus">
<c:if test="${tStatus.index == fStatus.index}">
<input type="text" name="${splt}" value="${splt}"/>
</c:if>
</c:forEach>
</c:forEach>
Another solution:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
String f = "a,b,c,d";
String t = "1,2,3,4";
pageContext.setAttribute("f", f);
pageContext.setAttribute("t", t);
%>
<c:forTokens var="current_t" items="${t}" delims="," varStatus="tStatus">
<label>${current_t}</label>
<c:forTokens var="current" items="${f}" delims="," varStatus="fStatus">
<c:if test="${tStatus.index == fStatus.index}">
<input type="text" name="${current}" value="${current}"/>
</c:if>
</c:forTokens>
</c:forTokens>