In one of the articles we saw the form handling example for spring mvc. Now we will discuss the 10 Commonly Used Spring MVC Form tags.
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" %>
Using various Spring MVC Form tags
1. 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.
1 2 3 4 5 6 7 8 |
<form:form> <table> <tr> <td>Name</td> <td>KS Codes</td> </tr> </table> </form:form> |
You can bind the form with a named variable as shown below. this is optional.
<form:form commandName=”employee”>
Also check – Spring MVC Form handling Example
2. input Tag
Input tag is used to create a text box in the Form and get data from user.
<form:input path="firstName" />
For a detailed example check – Spring MVC Input tag Example
3. label Tag
Spring MVC label tag is used to display labels on form.
<form:label path="firstName">First Name</form:label>
For a detailed example check – Spring MVC Label Tag Example
4. checkbox / checkboxes Tag
This tag can be used by mapping the checbox to a boolean or a list/map.
Using boolean
1 |
<form:checkbox path="isEmployed" /> |
Using a List/Map
1 |
<form:checkboxes items="${list}" path="departmentName" /> |
Also check the link for detailed example on Checkbox Tag –Spring MVC Checbox tag Example
5. radiobutton / radiobuttons Tag
Similar to checkbox, radiobutton can also be mapped using multiple syntax
1 2 |
<form:radiobutton path="status" value="Single"/>Single <form:radiobutton path="status" value="Married"/>Married |
Or
1 |
<form:radiobuttons path="favFruit" items="${fruitList}" /> |
Also check the link for detailed example on RadioButton Tag Spring MVC RadioButton tag Example
6. password Tag
1 |
<form:password path="password" /> |
The HTML code that gets generated is
1 |
<input id="password" name="password" type="password" value=""> |
For a detailed example on password tag check the link – Spring MVC Password Tag Example
7. select Tag
Select Tag is used in rendering the dropdown box.
1 |
<form:select path="sport" items="${sportsList}" /> |
For a detailed example on select tag check the link – Spring MVC Select Tag Example
8. textarea Tag
textarea tag is mainly used when we need the user to fill in a data that’s multiline. e.g a description of a item OR a email message on a form.
1 |
<form:textarea path="description" rows="20" cols="2"/> |
For a detailed example on TextArea tag check the link – Spring MVC textarea tag example
9. hidden Tag
In some cases we need data that needs to be send to the server but is not displayed to the user.
In that situation we can use the hidden tag.
1 |
<form:hidden path="id" /> |
For a detailed example on Hidden tag check the link – Spring MVC Hidden tag Examples
10. errors Tag
When a form is submitted the server, we generally validate the form for the correct input values.
Error tags are very useful to show the exact validation error on a specific input field.
1 |
<form:errors path="firstName" /> |
Also check the link for detailed example on Error Tag –Spring MVC Error Tag Example
Please do let me know if you need information on any other tags that are not mentioned here.