Jsp 逻辑问题
我有这样的东西Jsp 逻辑问题,jsp,jstl,logic,expression,Jsp,Jstl,Logic,Expression,我有这样的东西 <c:if test="${(not empty students) && (studentID != null)}"> <form:input path=studentsList[${studentID}].name"> .......... .......... //some 50+ lines of code </c:if> .......... .......... /
<c:if test="${(not empty students) && (studentID != null)}">
<form:input path=studentsList[${studentID}].name">
..........
..........
//some 50+ lines of code
</c:if>
..........
..........
//大约50多行代码
我可以使用if语句来检查他们是否正在查看特定页面或通用页面
<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>
..........
..........
//大约50多行代码
有谁能告诉我如何修改代码,这样我就不必重复那50多行了
谢谢你的回复,但我已经解决了。我写了便笺,解决了这个问题
<%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;}
}
%>
在这里使用a会很方便。只需创建一个标记(custom.tag
,例如),它将表单:input
路径作为属性:
<%@ attribute name="path" required="true" %>
<form:input path="${path}">
//some 50+ lines of code
//大约50多行代码
然后在调用jsp中使用invoke:
<%@ 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>
...
scriptlet是一种糟糕的方式。再看一看btiernay的标记文件解决方案。
<%@ 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>