Creating the View with a form

Java Server Pages is one of the view technologies supported by Spring Framework. Spring Framework makes it easy to create views with JSPs by providing a tag library. This includes tags for various form elements, binding, validation, setting themes and internationalizing messages. We will use the tags from the Spring MVC tag library as well as standard JSTL tag libraries to create our view in this example.

Let's start with creating the /WEB-INF/views/user.jsp file.

First, let's add the reference to the tag libraries to be used:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/tags/form"
prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags"
prefix="spring"%>

The first two entries are for JSTL core and formatting tag libraries. We will use the Spring form tags extensively. We provide a prefix to act as a shortcut to refer to tags.

Let's create a form with one field first:

    <form:form method="post" modelAttribute="user"> 
<fieldset>
<form:label path="name">Name</form:label>
<form:input path="name"
type="text" required="required" />
</fieldset>
</form:form>

Important things to note are as follows:

  • <form:form method="post" modelAttribute="user">: This is the form tag from the Spring form tag library. Two attributes are specified. Data in the form is sent using the post method. The second attribute, modelAttribute, specifies the attribute from the model that acts as the form backing object. In the model, we added an attribute with the name user. We use that attribute as modelAttribute.
  • <fieldset>: This is the HTML element to group a set of related controls--labels, form fields, and validation messages.
  • <form:label path="name">Name</form:label>: This is the Spring form tag to show a label. The path attribute specifies the field name (from bean) this label is applied to.
  • <form:input path="name" type="text" required="required" />: This is the Spring form tag to create a text input field. The path attribute specifies the field name in the bean that this input field has to be mapped to. The required attribute indicates that this is a required field.

When we use the Spring form tags, the values from the form backing object (modelAttribute="user") are bound automatically to the form, and on submitting the form, the values from the form are automatically bound to the form backing object.

A more complete list of the form tags including the name and user ID fields are listed as follows:

    <form:form method="post" modelAttribute="user"> 
<form:hidden path="guid" />
<fieldset>
<form:label path="name">Name</form:label>
<form:input path="name"
type="text" required="required" />
</fieldset>
<fieldset>
<form:label path="userId">User Id</form:label>
<form:input path="userId"
type="text" required="required" />
</fieldset>
<!-password and password2 fields not shown for brewity-->
<input class="btn btn-success" type="submit" value="Submit" />
</form:form>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset