Search This Blog

Spring MVC JSP Form Tags Example

In this quick article, we will discuss all Spring MVC JSP form tags with sample code examples.

These Spring MVC form tags, they give us extra support. They support data binding, so this allows us to automatically set data and retrieve data from Java objects and beans.

As of version 2.0, 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.

Spring’s form tag library

The form tag

The input tag

The checkbox tag

The checkboxes tag

The radiobutton tag

The radiobuttons tag

The password tag

The select tag

The option tag

The options tag

The textarea tag

The hidden tag

The errors tag

HTML5 tags

Let’s go through all these form tags and look at an example of how each tag is used. We have included generated HTML snippets where certain tags require further commentary.

Configuration

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

How To Reference Spring MVC Form Tags

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

where a 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:

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

When the form is loaded, spring MVC will class user.getFirstName() and getLastName() (getter methods). When the form is submitted, Spring MVC will call user.setFirstName() and user.setLastName() methods.

There are 3 approaches to the checkbox tag which should meet all our checkbox needs.

Approach One - When the bound value is of type java.lang.Boolean, the input(checkbox) is marked as 'checked' if the bound value is true. The value attribute corresponds to the resolved value of the setValue(Object) value property.

Approach Two - When the bound value is of type array or java.util.Collection, the input(checkbox) is marked as 'checked' if the configured setValue(Object) value is present in the bound Collection.

Approach Three - For any other bound value type, the input(checkbox) is marked as 'checked' if the configured setValue(Object) is equal to the bound value.

Note that regardless of the approach, the same HTML structure is generated. Below is an HTML snippet of some checkboxes:

HTML5 tags

Starting with Spring 3, the Spring form tag library allows entering dynamic attributes, which means you can enter any HTML5 specific attributes.

In Spring 3.1, the form input tag supports entering a type attribute other than 'text'. This is intended to allow rendering new HTML5 specific input types such as 'email', 'date', 'range', and others. Note that entering type='text' is not required since 'text' is the default type.