java - 检查 JSTL 中的对象是否是新的

标签 java jsp jakarta-ee jstl

我正在开发一个 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/

相关文章:

java - Tomcat 服务器测试 404 未找到错误

java-jsf 库,具有良好的移动浏览器支持

java - 如何从 doc 或 pdf 文件中读取特定页面。

java - 使用 EclipseLink 时生成脚本

java - java 中 tiff 图像的平铺?或 libTiff 的 java 包装器?

java - 错误 "Missing return statement"

java - 以编程方式将字符串中包含的 JSP 转换为 Servlet

java - 在servlet中从表(oracle)中获取数据并传递给jsp

java - 获取美元的 INR 价格

java - 使用用于数据访问的丰富 JavaScript API 开发开放式 Web 应用程序的最佳现代方式是什么?