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?
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 keynew
.Related:
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 actualMap
. For example,when you then do
JSTL, internally, does something like
and returns that.
I add my answer here for the record because I searched a lot and finally found the correct answer.
is the equivalent of
The method is defined in the class BaseEntity.java which is the superclass for all the entities in the model package.