Spring MVC Form Tag Library

Facebooktwitterredditpinterestlinkedinmailby feather

The Spring MVC provides a variety of tags available in the form of a tag library, which is used to develop web pages (JSP view pages). The Spring’s form tag library is associated with the Spring Web MVC. The Spring form tag has access to the command object, and it also deals with the data managed by the Spring controller.

The command object is defined as a JavaBean which stores user input entered through an HTML form. The Spring form tags provide a smooth move to develop, maintain, and read JSP files. These tags are also used to create user interface elements such as texts and buttons. Each tag of the tag library provides support for the set of attributes of its corresponding HTML tag counterpart, which makes the tags easy to use and understand.

Configuration of Spring MVC Form Tag 

The Spring form tag library comes under the spring-webmvc.jar. The spring-form.tld is known as the Tag Library Descriptor (tld) file, which generates the HTML tags. The tag library must be defined on the top of a JSP page.

Below is the code of Spring form tag library:

In the above code, “form” is the tag name prefix, which is going to be used for the tags from this Spring form tag library in JSP pages.

There are many tags available in the Spring form tag library. The following table shows some important tags of the tag library:

Tag Description
form:form It generates the HTML <form> tag. It also refers to a container tag that contains all the other tags.  
form:input It represents the HTML input text tag, which is used to generate the text field.
form:checkbox It represents the HTML input checkbox tag, which is used to generate checkboxes.
form:password It represents the HTML input password tag, which is used to insert the password input fields.
form:radiobutton It represents the HTML input radio button tag, which is used to generate the radio buttons.
form:select It represents the HTML select list tag, which is used to generate a drop-down list.
form:option It represents the HTML options tag, which is used to set ‘selected’ as the appropriate based on bound value.
form:errors It represents the HTML span tag, which is used to access the error.
form:textarea It represents the HTML text area tag, which is used to generate the multi-line text field.
form:hidden It represents the HTML hidden tag, which is used to generate hidden input fields.

Now, we are going to discuss the Spring MVC form tags briefly.

The <form:form> tag

The <form:form> tag is used to generate an HTML <form:form> tag inside the JSP pages of the Spring application. It is also used for binding the inner tags, all the other form tags are nested inside the <form:form> tag.

The following code snippet sows how to use the <form:form> tag:

In the above code, action and modelAttribute are the attributes of the form tag. The action attribute is used to set the operation of the form, which specifies how to send the form data, such as to get or post. The modelAttribute is used for binding a method parameter or method return value to a named model attribute.

The <form:input> tag

The <form:input> tag is used for inserting text by the user in the JSP pages of the Spring application. It is mainly used for generating the text fields.

The following code snippet shows how to use the <form:input> tag:

In the above code, the path is one of the most important attributes of the input tag. It binds the input field to the form-backing object’s property.

The <form:checkbox> tag

The <form:checkbox> tag is used to generate a checkbox in the JSP pages of the Spring application. It is very similar to the HTML <input> tag, which is of checkbox type.

The following code snippet shows how to use the <form:checkbox> tag:

In the above code, value and label are the attributes of the checkbox tag. The label attribute is used to display the value or output. The value attribute is used for providing the values of the checkbox.

The <form:password> tag

The <form:password> tag represents the HTML <input> tag of a password type in the JSP pages of the Spring applications. By default, the value of the password field is not shown by the browser. To show the value of the password field, we can enable the showPassword attribute.

The following code snippet shows how to use the <form:password> tag:

The <form:radiobutton> tag

The <form:radiobutton> tag represents the HTML <input> tag of radio type in JSP pages of the spring application. A radio button is a two-state button that can be checked or unchecked. It is used when we have to choose one among many options at a time.

The following code snippet shows how to use the <form:radiobutton> tag:

The <form:select> tag

The <form:select> tag represents the HTML <select> tag in JSP pages of the Spring application. It is used to create a drop-down list and binds the selected option with its value. The <form:option> tag is nested inside the <form:select> tag.

The following code snippet shows how to use the <form:select> tag:

The <form:option> tag

The <form:option> tag is represents the HTML <option> tag in the JSP pages of the Spring application. It is used to define the options of a drop-down list.

The following code snippet shows how to use the <form:option> tag:

The <form:errors> tag

The <form:errors> tag represents the HTML errors in the JSP pages of the Spring application. It is used for retrieving the errors defined in the org.springframework.validation.Validator interface.

The following code snippet shows how to use the <form:errors> tag:

The <form:textarea> tag

The <form:textarea> tag represents the HTML <textarea> tag in the JSP pages of the Spring application. The primary use of <form:textarea> tag is to generate multi-line text field.

The following code snippet shows how to use the <form:textarea> tag:

The <form:hidden> tag

The <form:hidden> tag represents the HTML hidden field tag in the JSP pages of the Spring application.

The following code snippet shows how to use the <form:hidden> tag:

Facebooktwitterredditpinterestlinkedinmailby feather