Spring MVC Form Tags are the reusable building blocks of a web page. These Spring tags are used for data binding that automatically sets data to Java object/bean and retrieves from it. It makes an application easy to develop, read, and maintain.
There are various tags available in Spring MVC:
- 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
These tags come under spring-webmvc.jar. You need to add the following directive at the beginning of the JSP page:
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
The form tag: It is a parent tag that contains all the other tags.
<form:form action="nextFormPath" modelAttribute=?abc?>
This will generate an HTML form tag and exposes a binding path to inner tags.
The input tag: It generates an HTML input tag.
<form:input path="name" />
It also provides other input types such as email, date, tel, etc.
For email:
<form:input type=?email? path="email" />
For date:
<form:input type=?date? path="date" />
The password tag: This tag generates an HTML tag with type=’password’.
<form:password path="password" />
The textarea tag: It generates an HTML textarea.
<form:textarea path="notes" rows="3" cols="20"/>
Number of rows and columns can be specified as required.
<form:checkbox path="receiveNewsletter" />
The above is applicable for a single checkbox whose return type is Boolean. After setting the value as true, the checkbox will be checked by default.
For Multiple Checkboxes:
Bird watching: <form:checkbox path="hobbies" value="Bird watching"/> Astronomy: <form:checkbox path="hobbies" value="Astronomy"/> Snowboarding: <form:checkbox path="hobbies" value="Snowboarding"/>
The radiobutton and radiobuttons tag: This will generate the HTML input tag with type=’radio’.
For Single Radiobutton:
Male: <form:radiobutton path="sex" value="M"/> Female: <form:radiobutton path="sex" value="F"/> For Multiple Radiobutton: <form:radiobuttons path="sex" items="${sexOptions}" />
The select tag: This will generate the HTML select tag.
<form:select path="country" items="${countryItems}" />
The hidden tag: This will generate the HTML input tag with type=’hidden’.
<form:hidden path="id" value="12345" />
The option and options tag: This is used to select option from a drop-down option containing a list of elements.
To generate single option tag: <form:option value="abc" label="xyz"/> To generate multiple option tag: <form:options items="${elementList}" itemValue="abc" itemLabel="xyz"/>
The errors tag: This is used to represent the HTML errors in the JSP page created by Validators.
<form:errors path="name" cssClass="error" />
Example of Validator in error tag:
public class UserValidator implements Validator { public boolean supports(Class candidate) { return User.class.isAssignableFrom(candidate); } public void validate(Object obj, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required."); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required."); } }
Form.jsp will look like:
<form:form> <table> <tr> <td>First Name:</td> <td> <form:input path="firstName" /> </td> <%-- Show errors for firstName field --%> <td> <form:errors path="firstName" /> </td> </tr> <tr> <td>Last Name:</td> <td> <form:input path="lastName" /> </td> <%-- Show errors for lastName field --%> <td> <form:errors path="lastName" /> </td> </tr> <tr> <td colspan="3"> <input type="submit" value="Save Changes" /> </td> </tr> </table> </form:form>
The HTML file will look like:
<form method="POST"> <table> <tr> <td>First Name:</td> <td><input name="firstName" type="text" value="" /></td> <%-- Associated errors to firstName field displayed --%> <td><span name="firstName.errors">Field is required.</span></td> </tr> <tr> <td>Last Name:</td> <td><input name="lastName" type="text" value="" /></td> <%-- Associated errors to lastName field displayed --%> <td><span name="lastName.errors">Field is required.</span></td> </tr> <tr> <td colspan="3"> <input type="submit" value="Save Changes" /> </td> </tr> </table> </form>