通常,唯一的解决方案是尝试一下。
我尝试了三种不同的方法——JSP 自定义标记库、参数化的 JSP 包含和 JSP2 标记文件。
前两个不起作用(虽然我怀疑可以使标签库起作用),但是标签文件起作用了!该解决方案大致基于Expert Spring MVC 和 Web Flow中给出的示例。
这是我在 WEB-INF/tags/renderConditionalControl.tag 中的代码:
<%@ tag body-content="tagdependent" isELIgnored="false" %>
<%@ attribute name="readOnly" required="true" %>
<%@ attribute name="path" required="true" %>
<%@ attribute name="type" required="false" %>
<%@ attribute name="className" required="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="form" uri="/WEB-INF/spring-form.tld" %>
<%@ taglib prefix="spring" uri="/WEB-INF/spring.tld" %>
<c:if test="${empty type}">
<c:set var="type" value="text" scope="page" />
</c:if>
<spring:bind path="${path}">
<c:choose>
<c:when test="${readOnly}">
<span class="readOnly">${status.value}</span>
</c:when>
<c:otherwise>
<input type="${type}" id="${status.expression}" name="${status.expression}"
value="${status.value}" class="${className}" />
</c:otherwise>
</c:choose>
</spring:bind>
这是jsp中的代码:
首先,使用其他 taglibs 指令:
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag" %>
并在表格内:
<tag:renderConditionalControl path="someObject.someField" type="text" readOnly="${someBoolean}" className="someClass" />