lishman levelup
«previous  next»


Getting Started
Form Processing
IoC Container
Hibernate ORM



The Spring MVC Form View


Next, we introduce a new JSP page to maintain the country details.

Creating a Country

If the /countryForm.html page is requested without an 'id' parameter, then an empty form backing object is created by the controller, and a blank screen is displayed.

For example, we can use this code to navigate to the /countryForm.html page:
 
<button onclick="window.location='countryForm.html'">Create</button>
 
And this screen is displayed:

Country Create Form

Updating a Country

If the /countryForm.html page is requested with an 'id' parameter, then a form backing object is populated by the controller, and the relevant country details are displayed.

For example, the edit button (Edit Button) on the /countryList.html page uses this code:
<td>
  <a href="<c:url value='countryForm.html?id=${country.id}'/>">
    <img src="images/edit.gif" style="border-style:none;"/>
  </a>
</td>
Which results in this screen:

Country Update Form

The Spring Form Tag Library

Spring provides a set of integrated, data binding-aware form tags, which can access the model data produced by the controller.

On the countryForm.jsp page, the main data table is wrapped in a <form:form> element, which identifies the model attribute to be used by the nested <form:input> tags.
<form:form modelAttribute="country"
              action="countryForm.html"
              method="post">
  <table>
    <!-- buttons here -->
    <tr>
      <td>
        <table class="silver">

          <tr>
            <th colspan=2><spring:message code="country.details"/></th>
          </tr>

          <tr>
            <td><spring:message code="country.name"/></td>
            <td>
              <form:input path="name" size="20" maxlength="50" />
              <form:errors path="name" cssClass="errors"/>
            </td>
          </tr>

          <tr>
            <td><spring:message code="country.area"/></td>
            <td>
              <form:input path="area" size="8" maxlength="8" />
              <form:errors path="area" cssClass="errors"/>
            </td>
          </tr>

          <tr>
            <td><spring:message code="country.population"/></td>
            <td>
              <form:input path="population" size="10" maxlength="10" />
              <form:errors path="population" cssClass="errors"/>
            </td>
          </tr>

          <tr>
            <td><spring:message code="country.updatedOn"/></td>
            <td>
              <form:input path="populationLastUpdated" size="12" maxlength="12" />
              <form:errors path="populationLastUpdated" cssClass="errors"/>
            </td>
          </tr>

          <tr>
            <td><spring:message code="country.currency"/></td>
            <td>
              <form:input path="currency" size="20" maxlength="50" />
              <form:errors path="currency" cssClass="errors"/>
            </td>
          </tr>

        </table>
      </td>
    </tr>
  </table>
</form:form>
The items to be edited on the form use the path attribute of the <forms:input> tag to reference a property on the form backing object, which is identified by the modelAttribute in the <form:form> tag.

The <forms:errors> tags display any user input errors. Form Validation is covered in the next section.

Form Buttons

Finally on this page, form buttons are optionally displayed depending on whether the country is being created or updated.
<td align="right" valign="bottom">
  <c:choose>
    <c:when test="${country.new}">
      <button type="submit"
              name="create">
        Create
      </button>
    </c:when>
    <c:otherwise>
      <button type="submit"
              name="delete"
              onclick="return confirm('Are you sure you want to delete ${country.name}?')">
        Delete
      </button>
      <button type="submit"
              name="update">
        Update
      </button>
    </c:otherwise>
  </c:choose>
</td>
The isNew() method on the Country class from the model is used to determine the mode of the screen.
public boolean isNew() {
  return id==null;
}
»