我正在开发一个 Spring 项目,其中有两个 Controller
AddOwnerForm.java 和 EditOwnerForm.java。两者都将流程转发到form.jsp
AddOwnerForm 将新的 Owner 对象传递给 jsp,而 EditOwnerForm 从数据库获取 Owner 对象,然后将其传递给 jsp。
下面是JSP代码。
表单.jsp
<%@ include file="/WEB-INF/view/include.jsp" %>
<%@ include file="/WEB-INF/view/header.jsp" %>
<c:choose>
<c:when test="${owner['new']}"><c:set var="method" value="post"/></c:when>
<c:otherwise><c:set var="method" value="put"/></c:otherwise>
</c:choose>
<h2><c:if test="${owner['new']}">New </c:if>Owner:</h2>
<form:form modelAttribute="owner" method="${method}">
<table>
<tr>
<th>
First Name:
<br/>
<form:input path="firstName" size="30" maxlength="80"/>
</th>
</tr>
<tr>
<th>
Last Name:
<br/>
<form:input path="lastName" size="30" maxlength="80"/>
</th>
</tr>
<tr>
<th>
Address:
<br/>
<form:input path="address" size="30" maxlength="80"/>
</th>
</tr>
<tr>
<th>
City:
<br/>
<form:input path="city" size="30" maxlength="80"/>
</th>
</tr>
<tr>
<th>
Telephone:
<br/>
<form:input path="telephone" size="20" maxlength="20"/>
</th>
</tr>
<tr>
<td>
<c:choose>
<c:when test="${owner['new']}">
<p class="submit"><input type="submit" value="Add Owner"/></p>
</c:when>
<c:otherwise>
<p class="submit"><input type="submit" value="Update Owner"/></p>
</c:otherwise>
</c:choose>
</td>
</tr>
</table>
</form:form>
<%@ include file="/WEB-INF/view/footer.jsp" %>
我不明白这段代码
<c:choose>
<c:when test="${owner['new']}"><c:set var="method" value="post"/></c:when>
<c:otherwise><c:set var="method" value="put"/></c:otherwise>
</c:choose>
A. JSTL 标记如何检查 Owner 对象是否是新的。 “new”是 JSTL 的关键字吗?
B.为什么他们使用 PUT 方法来编辑所有者而不是 POST?
最佳答案
我在这里添加我的答案以供记录,因为我搜索了很多,终于找到了正确的答案。
${owner['new']}
相当于
${owner.isNew()}
该方法在类 BaseEntity.java 中定义,该类是模型包中所有实体的父类(super class)。
public boolean isNew() {
return (this.id == null);
}
关于java - 检查 JSTL 中的对象是否是新的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20943797/