I have something like this..
<c:if test="${(not empty students) && (studentID != null)}">
<form:input path=studentsList[${studentID}].name">
..........
..........
//some 50+ lines of code
</c:if>
This is when they are viewing partic开发者_运维问答ular student's page. If the are viewing some generic page I want to change the code to something like this..
<c:forEach items="${students}" var="student">
<form:input path=student.name">
..........
..........
//some 50+ lines of code
</c:forEach>
I can have an if statement to check if they are viewing a particular page or generic page
<c:when ${particularPage}>
<c:if test="${(not empty students) && (studentID != null)}">
<form:input path=studentsList[${studentID}].name">
..........
..........
//some 50+ lines of code
</c:if>
<c:otherwise>
<c:forEach items="${students}" var="student">
<form:input path=student.name">
..........
..........
//some 50+ lines of code
</c:forEach>
</c:otherwise>
Can anyone tell me how can we change the code so that I dont have to repeat those 50+ lines??
Thanks
Use of a jsp tag file would come in handy here. Simply create a tag (custom.tag
, for example) that takes the form:input
path as an attribute:
<%@ attribute name="path" required="true" %>
<form:input path="${path}">
//some 50+ lines of code
And then use invoke it in your calling jsp:
<%@ taglib tagdir="/WEB-INF/tags" prefix="h" %>
...
<c:when ${particularPage}>
<c:if test="${(not empty students) && (studentID != null)}">
<h:custom path="studentsList[${studentID}].name"/>
</c:if>
<c:otherwise>
<c:forEach items="${students}" var="student">
<h:custom path="student.name"/>
</c:forEach>
</c:otherwise>
</c:when>
Thanks for the reply, but I solved it. I wrote scriplets and solved the problem.
<%for(int i = request.getAttribute("studentId") == null ? 0 : ((Integer)request.getAttribute("studentId")).intValue();
i< ((List)request.getAttribute("students")).size(); i++){
request.setAttribute("id", new Integer(i));
%>
<form:input path=studentsList[${id}].name">
..........
..........
//some 50+ lines of code
<% if(!"true".equals(request.getAttribute("specificPage")){break;}
}
%>
I am not sure if this is the correct way to do this but its working!!!
Please let me know if what I am doing is wrong.
精彩评论