Spring MVC Form Tag Library

We all know the HTML form for submitting some data into servers like Registration Form, Login Form, etc. The Spring Framework also provides Spring MVC Form Tags Library.
Spring MVC Form Tags:
    • Spring MVC Form tags are the building block for web pages.
    • Spring MVC Form tags are configurable and reusable for the web page.
    • Spring MVC Form tags are making use of data binding.
    • Automatically setting/retrieving data from a Java object/bean to the web page.
    • These tags will generate HTML for us: 

Form TagDescription
<form:form></form:form>      Main form container
<form:input/>        Text Field
<form:textarea/>           Multi-line text field
<form:checkbox/>       Check box
<form:radiobutton/>          Radio buttons
more… 

HTML Form Tag:    

<form id="test"  action="/save"  method="post">
</form>

Spring Form Tag: 

<form:form action="/save"  method="post" modelAttribute="test">
</form:form>

HTML Input Tag: 

<input id ="test" name="test" type="text" value=" "/>   

Spring Form Input Tag: 

<form:input path="test"/>

HTML TextArea Tag:  

<textarea id ="test" name="test"  rows="4" cols="50"/>

 Spring Form TextArea Tag:   

<form:textarea path="test"  rows="4" cols="50"/>

HTML Input Password Tag: 

<input id="test" name="test" type="password" value=" "/>

Spring Form Input Password Tag: 

<form:input path="test"/>

HTML Checkbox Tag:

<input id="test" name="test" type="checkbox" value=" "/>   

 Spring Form Checkbox Tag:

<form:checkbox path="hobby" value="Painting"/>

  Spring Form Checkboxes Tag:

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

HTML RadioButton Tag: 

<input id="test" name="test" type="radio" value=" "/>  
<input id="test" name="test" type="radio" value=" "/>

Spring Form RadioButton Tag: 

<form:radiobutton path="gender" value="male"/>
<form:radiobutton path="gender" value="female"/>

 Spring Form RadioButtons Tag:

<form:radiobuttons path="experience" items="${year}"/>

HTML Select Tag:

<select id="job" name="job">
<option value="part-time">part-time</option>
<option value="full-time">full-time</option>
</select>

Spring Form Select Tag:

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

HTML Option Tag:

<option value="part-time">part-time</option>
<option value="full-time">full-time</option>

Spring Form Options Tag:

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

HTML Button Tag:

<button type="submit" value="Submit">Submit</button>

Spring Form Button Tag:

<form:button>Submit</form:button>

HTML Input Hidden Tag:

<input name="test" type="hidden" value=" "/>  

Spring Form Hidden Tag:

<form:hidden path="job"/>

HTML Error:

<span id="email.error" style= "color:red"> Please enter valid email id </span>

Spring Form Error Tag:

<form:error path="email" cssStyle="color:red"/>

→ In the above some of the HTML and Spring MVC form tags are written to show how we can write tags in the HTML and Spring MVC form tags on the view page.
→ It is nearly too similar to HTML tags but one thing is that it is different in the Spring MVC tags from HTML tags i.e., the “form” tag and “path” attribute are used. In Spring MVC form tags are used starting with “form” and the “path” attribute is used for wrapping our java class to the Spring MVC form view page.
→ Spring MVC form tag library is very helpful for making real-time applications. Because we can set the Java bean class in this request-handling method in the Controller class. This will wrap the class to view page JSP(Java Server Page) which contains the Spring MVC Form tag library.
→ Spring MVC form tag library is very convenient in the use of JSP view pages.
Web Page Structure:
JSP page with special Spring MVC Form Tags sample  

<html>
....regular html....
....Spring MVC form tags....
....more html....
</html>

 Configuration:
 
 Before using the form tag library. We must place it on the top of the JSP page.

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

Example: 

<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<form:form action="/save" modelAttribute="person" method= "post">
<table>
<tr>
<td>Name:</td>
<td><form:input path="personName" /></td>
</tr>
<tr>
<td>Mobile:</td>
<td><form:input path="personMobile" /></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save Changes" />
</td>
</tr>
</table>
</form:form>

Conclusion:
This topic is explained What is a Spring MVC Form Tag? What are the different Spring Form Tags?

Leave a Comment