Checking if the object is new in JSTL

余生颓废 提交于 2020-01-06 12:37:12

问题


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

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

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

Below is the JSP code.

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" %>

I don't understand this code snippet

<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. How is the Jstl tag checking if the Owner object is new. Is "new" a keyword for JSTL?

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


回答1:


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

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.

Related:

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

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

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);

when you then do

${owner['new']}

JSTL, internally, does something like

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

and returns that.




回答2:


I add my answer here for the record because I searched a lot and finally found the correct answer.

${owner['new']}

is the equivalent of

${owner.isNew()}

The method is defined in the class BaseEntity.java which is the superclass for all the entities in the model package.

public boolean isNew() {
    return (this.id == null);
}


来源:https://stackoverflow.com/questions/20943797/checking-if-the-object-is-new-in-jstl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!