Spring tag libraries

Spring provides a comprehensive set of data binding-aware tags for handling form elements when using JSP and Spring Web MVC. Each tag provides support for the set of attributes of its corresponding HTML tag counterpart, making the tags familiar and intuitive to use. The tag-generated HTML is HTML 4.01/XHTML 1.0 compliant. Unlike other form/input tag libraries, Spring’s form tag library is integrated with Spring Web MVC, giving the tags access to the command object and reference data your controller deals with.

Configuration :

The form tag library comes bundled in spring.jar. The library descriptor is called spring-form.tld.

To use the tags from this library, add the following directive to the top of your JSP page:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

Where form is the tag name prefix you want to use for the tags from this library.

The form tag

            This tag renders an HTML ‘form’ tag and exposes a binding path to inner tags for binding. It puts the command object in the PageContext so that the command object can be accessed by inner tags. All the other tags in this library are nested tags of the form tag.

Let’s assume we have a domain object called User. It is a JavaBean with properties such as firstName and lastName. We will use it as the form backing object of our form controller which returns form.jsp. Below is an example of what form.jsp would look like:

<form:form>
<table>
<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form:form>

The firstName and lastName values are retrieved from the command object placed in the PageContext by the page controller.

The preceding JSP assumes that the variable name of the form backing object is ‘command’. If you have put the form backing object into the model under another name (definitely a best practice), then you can bind the form to the named variable like so:

<form:form commandName="user">
<table>
<tr>
<td>First Name:</td>
<td><form:input path="firstName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><form:input path="lastName" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form:form>

The input tag

This tag renders an HTML ‘input’ tag with type ‘text’ using the bound value.

The checkbox tag

This tag renders multiple HTML ‘input’ tags with type ‘checkbox’.

Below is an example of the JSP using this tag:

<form:form>
<table>
<tr>
<td>Interests:</td>
<td>
<%-- Property is of an array or of type java.util.Collection --%>
<form:checkboxes path="preferences.interests" items="${interestList}"/>
</td>
</tr>
</table>
</form:form>

The radiobutton tag

This tag renders an HTML ‘input’ tag with type ‘radio’. A typical usage pattern will involve multiple tag instances bound to the same property but with different values.

The password tag

This tag renders an HTML ‘input’ tag with type ‘password’ using the bound value.

<tr>
<td>Password:</td>
<td>
<form:password path="password" />
</td>
</tr>

The select tag

This tag renders an HTML ‘select’ element. It supports data binding to the selected option as well as the use of nested option and options tags. Let’s assume a User has a list of skills.

<tr>
<td>Skills:</td>
<td><form:select path="skills" items="${skills}"/></td>
</tr>

The option tag

This tag renders an HTML ‘option’. It sets ‘selected’ as appropriate based on the bound value.

<tr>
<td>House:</td>
<td>
<form:select path="house">
<form:option value="Gryffindor"/>
<form:option value="Hufflepuff"/>
<form:option value="Ravenclaw"/>
<form:option value="Slytherin"/>
</form:select>
</td>
</tr>

The textarea tag

This tag renders an HTML ‘textarea’.

<tr>
<td>Notes:</td>
<td><form:textarea path="notes" rows="3" cols="20" /></td>
<td><form:errors path="notes" /></td>
</tr>

The hidden tag

This tag renders an HTML ‘input’ tag with type ‘hidden’ using the bound value. To submit an unbound hidden value, use the HTML input tag with type ‘hidden’.

<form:hidden path="house" />

 


Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences. I am on Google+ and Facebook. I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)

Share This Post

Recent Articles

Powered by WordPress · Designed by Theme Junkie
You might also likeclose