检查JSTL中的对象是否为新对象 [英] Checking if the object is new in JSTL

查看:214
本文介绍了检查JSTL中的对象是否为新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个有两个控制器的Spring项目

I am working on a Spring project where there are two controllers

AddOwnerForm.java& EditOwnerForm.java。将流转发到form.jsp

AddOwnerForm.java & EditOwnerForm.java. Both the forwarding the flow to form.jsp

AddOwnerForm将新的Owner对象传递给jsp,而EditOwnerForm从db中获取Owner对象,然后将其传递给jsp。

AddOwnerForm passes a new Owner object to jsp whereas EditOwnerForm fetches the Owner object from the db then passes it to the jsp.

以下是JSP代码。

Form.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的关键字吗?

A. How is the Jstl tag checking if the Owner object is new. Is "new" a keyword for JSTL?

B.为什么他们使用PUT方法编辑所有者为什么不POST?

B. Why are they using a PUT method for editing the owner why not POST?

推荐答案


A。 Jstl标记如何检查Owner对象是否是新对象。 new是JSTL的关键字吗?

A. How is the Jstl tag checking if the Owner object is new. Is "new" a keyword for JSTL?

这不是检查对象是否是新的。它正在考虑所有者作为地图并尝试访问映射到键 new 的元素。

That is not checking if an object is new. It's considering owner as a map and trying to access the element mapped to the key new.

相关:

  • Get value from hashmap based on key to JSTL
  • EL access a map value by Integer key

B.为什么他们使用PUT方法编辑所有者为什么不POST?

B. Why are they using a PUT method for editing the owner why not POST?

这取决于API。请注意,通常,浏览器不支持使用PUT方法提交表单。您需要使用javascript发送PUT请求。

That's up to the API. Note that, typically, browsers does not support submitting forms with a PUT method. You will need to use javascript to send a PUT request.

要回答您的评论,请不要。它认为所有者是一个实际的地图。例如,

To answer your comment, no. It thinks owner is an actual Map. For example,

Map<String, Integer> owner = new HashMap<>();
map.put("new", someInt);
request.put("owner", owner);
// or
model.addAttribute("owner", owner);

当你这么做时

${owner['new']}

JSTL,内部,类似

JSTL, internally, does something like

mapValue = (Map) request.getAttribute("owner");
value = owner.get("new");

并返回。

这篇关于检查JSTL中的对象是否为新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆