Tags

Form Tag Library

Spring 2 and above provides a 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. The tag-generated HTML is HTML 4.01/XHTML 1.0 compliant.

Spring Tags binds the HTML form elements with Form bean attributes. The tag library descriptor (TLD) is included in the spring-webmvc.jar.

Configuration

The form tag library comes bundled in spring-webmvc.jar. The library descriptor is called spring-form.tld. Form tag library can be imported by taglib directive in JSP using following syntax:

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

where "form" is the tag name prefix that you will use to access tags of this library.

Here is a simple example that uses Spring's form tags. It binds HTML elements with LoginBean class attributes. Controller defines form-bean "loginForm" as model attribute and <form:form commandName="loginForm"> tag binds this form-bean with HTML elements using its "commandName" attribute.

<form:form action="login" method="post" commandName="loginForm">

Login: <form:input path="login" /><br>

Password: <form:password path="password" /><br>

<input type="submit" value="SignIn" name="operation">

</form:form>

@RequestMapping(value = "/login", method = RequestMethod.GET)

public String loginDisplay(@ModelAttribute("loginForm") LoginForm form, Model model) {

return "LoginBindingView";

}

public class LoginForm {

private String login = null;

private String password = null;

private String operation = null;

//Setter and Getter methods

}

The form tag

This tag renders an HTML form and exposes the form-bean object's binding path to inner tags to bind html elements and form-bean attributes. 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.

In above example form tag exposes command object loginForm to its inner tags and binds its login and password attributes to the html form elements.

<form:form action="login" method="post" commandName="loginForm">

The input tag

This tag renders an HTML input tag using the bound value and default type value text (type=text). Starting with Spring 3.1 you can use other types such HTML5-specific types like email, tel, date, and others. In above example we have used input elements:

<form:input path="login" />

The checkbox and checkboxes tag

This tag renders an HTML input tag using "checkbox" type. A checkbox can be bound to an attribute or multiple checkboxes can be bound to an array of elements. Checkboxes values can be coded in HTML or can be rendered by an array, collection or collection map.

To explain this tag we are assuming TagForm bean is containing attributes, those are bound to HTML form elements in TagView.jsp. Controller TagCtl will transfer control to the TagView.jsp.

public class TagForm {

private String name = null;

private boolean agreed = false;

private String[] selectedSkills = null;

private ArrayList<String> selectedCourses = null;

private String[] selectedLanguages = null;

public class TagCtl {

@ModelAttribute

public void preLoad(@ModelAttribute("tagForm") TagForm form, Model model) {

// Set Boolean value

form.setAgreed(true);

// Set list of elements as Array

String[] str = { "Java", "PHP", "Android" };

model.addAttribute("skillArray", str);

// Create Collection

ArrayList<String> courses = new ArrayList<String>();

courses.add("BE");

courses.add("MCA");

courses.add("MBA");

model.addAttribute("coursesList", courses);

// Create Map

HashMap<String, String> languages = new HashMap<String, String>();

languages.put("en", "English");

languages.put("hi", "Hindi");

languages.put("sp", "Spanish");

model.addAttribute("languagesMap", languages);

//..

}

1) Render simple checkbox and bind with form-bean attribute:

Below tag will render HTML checkbox. If checkbox is selected then it will set value "Yes" to name attribute otherwise value will be null.

Want Java? <form:checkbox path="name" value="Yes"/>

Below tag will bind boolean value. If selected then value of agreed attribute in form bean will be true otherwise false.

Agreed:<form:checkbox path="agreed" />

2) Bind multiple checkboxes of same name to a String array:

Below example will set selected values to selectedSkills[] array attributes in form bean.

<form:checkbox path="selectedSkills" value="Java" /> Java

<form:checkbox path="selectedSkills" value="PHP" /> PHP

<form:checkbox path="selectedSkills" value="Android" /> ANDROID

3) Render checkboxes from String Array or Collection:

You can render multiple checkboxes from a string array, stored as Model attribute. Here model attribute skillArray is used to render checkbox items.

<form:checkboxes path="selectedSkills" items="${skillArray}" />

It will create checkboxes just like example # 2

Similarly you render checkbox items from a Collection

<form:checkboxes path="selectedCourses" items="${coursesList}" />

4) Render checkboxes from a collection map:

You can also render checkbox items from a collection map. Collection Keys will become the checkbox values to be select. Collection values will become label of checkboxes.

<form:checkboxes path="selectedLanguages" items="${languagesMap}" />

The select tag

This tag renders an HTML select element. It binds selected value to the the form-bean attribute. It uses nested option and options tags.

Select elements can be rendered from an array, collection and collection map. If select list is rendered from array or collection then label and selected value will be same. If select list is rendered from collection map then map key will become selected value and map value will become label of list.

1) Render Select element from array, collection or map:

<form:select path="skill" items="${skillArray}"/>

<form:select path="course" items="${coursesList}"/>

<form:select path="lang" items="${languagesMap}"/>

2) Use nested tag option and options:

Here is a simple list:

<form:select path="gender">

<form:option value="Male"/>

<form:option value="Female"/>

</form:select>

Here list is created from a collection with an additional option value "--Please Select":

<form:select path="course">

<form:option value="-" label="--Please Select"/>

<form:options items="${coursesList}" />

</form:select>

3) Render Select element from a collection of bean:

List items can be rendered from a java bean collection. Tag attributes itemValue and itemLabel simply refer to bean properties used to make values and labels of list.

Here is an example that renders list from a collection of java-bean RoleDTO Collection is set to Model from TagCtl and list is shown at TagView.

<form:select path="role" items="${roleList}" itemValue="id" itemLabel="name" />

public class TagCtl {

@ModelAttribute

public void preLoad(@ModelAttribute("tagForm") TagForm form, Model model) {

//....

ArrayList<RoleDTO> roleList = new ArrayList<RoleDTO>();

RoleDTO role1 = new RoleDTO();

role1.setId(1);

role1.setName("Admin");

roleList.add(role1);

RoleDTO role2 = new RoleDTO();

role2.setId(2);

role2.setName("Manger");

roleList.add(role2);

RoleDTO role3 = new RoleDTO();

role3.setId(2);

role3.setName("Customer");

roleList.add(role3);

model.addAttribute("roleList", roleList);

}

//..

}

The items attribute is typically populated with a collection or array of item objects.

The radio tag

This tag renders an HTML input tag with type radio. It binds radio element's value to the the form-bean attribute.

Radio HTML elements can be rendered from an array, collection or collection map. If radio input tags are rendered from an array or a collection then label and input element's value will be same. If radio input tags are rendered from a collection map then map key will become value of radio input tag and map value will become label of radio input tag.

1) Render simple radio HTML input element

Below tag will render a simple radio HTML element and bind it with form-beam attribute "gender":

<form:radiobutton path="gender" value="Male" /> Male

<form:radiobutton path="gender" value="Female" /> Female

2) Render radio elements from array or collection

You can render multiple radio elements from a string array or collection, stored as Model attribute. Here model attribute skillArray and coursesList are used to render radio elements:

<form:radiobuttons path="skill" items="${skillArray}"/>

<form:radiobuttons path="course" items="${coursesList}"/>

3) Render radio elements from collection map

You can also render radio input elements from a collection map. Collection Keys will become the value of radio tag. Collection values will become label of radio input elements.

<form:radiobuttons path="lang" items="${languagesMap}"/>

See TagView.jsp for complete example.

The textarea tag

This tag renders an HTML textarea.

<form:textarea path="address" rows="3" cols="20" />

The hidden tag

This tag renders an HTML input tag with type hidden using the bound value.

<form:hidden path="house" />

The errors tag

This tag renders field errors in an HTML span tag. It provides access to the errors created in your controller or those were created by any validators associated with your controller.

<form:input path="lastName" />

<form:errors path="lastName" />

You can display entire list of errors messages for the current view page using wildcard mapping in path attribute:

<form:errors path="*" />

Attribute path may have following values:

  • path="*" - displays all errors

  • path="lastName" - displays all errors associated with the lastName field

  • if path is omitted - object errors only are displayed

JSTL Tags

See