Spring MVC form Binding

Spring form tags are defined in the spring-form.tld and they are bundled in spring.jar

To use spring form tags, we need to add below taglib directive on top of jsp page.

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

where ‘form’ is the tag name prefix you want to use for the tags from this library.
This tag acts as an html form tag + binding the form data

project structure



Lets create a class called Person as below

  1. package com.kb.model;
  2.  
  3. public class Person {
  4. String firstName;
  5. String lastName;
  6. public String getFirstName() {
  7.     return firstName;
  8. }
  9. public void setFirstName(String firstName) {
  10.     this.firstName = firstName;
  11. }
  12. public String getLastName() {
  13.     return lastName;
  14. }
  15. public void setLastName(String lastName) {
  16.     this.lastName = lastName;
  17. }
  18. }
package com.kb.model;

public class Person {
String firstName;
String lastName;
public String getFirstName() {
	return firstName;
}
public void setFirstName(String firstName) {
	this.firstName = firstName;
}
public String getLastName() {
	return lastName;
}
public void setLastName(String lastName) {
	this.lastName = lastName;
}
}


Lets create the PersonForm.jsp page as below

  1. <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
  2. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  3.     pageEncoding="ISO-8859-1"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <form:form modelAttribute="person" action="${pageContext.request.contextPath}/personDetails" method="POST">
  12.     <table>
  13.         <tr>
  14.             <td>First Name:</td>
  15.             <td><form:input path="firstName" /></td>
  16.         </tr>
  17.         <tr>
  18.             <td>Last Name:</td>
  19.             <td><form:input path="lastName" /></td>
  20.         </tr>
  21.         <tr>
  22.             <td colspan="3">
  23.                 <input type="submit" value="Save Changes" />
  24.             </td>
  25.         </tr>
  26.     </table>
  27. </form:form>
  28. </body>
  29. </html>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form:form modelAttribute="person" action="${pageContext.request.contextPath}/personDetails" method="POST">
    <table>
        <tr>
            <td>First Name:</td>
            <td><form:input path="firstName" /></td>
        </tr>
        <tr>
            <td>Last Name:</td>
            <td><form:input path="lastName" /></td>
        </tr>
        <tr>
            <td colspan="3">
                <input type="submit" value="Save Changes" />
            </td>
        </tr>
    </table>
</form:form>
</body>
</html>


< form:form > tag is used to define the form backing object along with html form action.

We can specify that form backing object using modelAttribute property.

It specifies the name of the bean which we are using to bind the form data.

As shown in PersonForm.jsp,
In line number 11 modelAttribute=”person” defines that the person object is the form binding object.

Note :

modelAttribute name should match with the model attribute name which we add in the controller.
So i’m using person on both jsp and controller, otherwise binding will not happen.

Define the controller class which can receive this form data

  1. package com.kb.controllers;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.ui.Model;
  5. import org.springframework.web.bind.annotation.ModelAttribute;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8.  
  9. import com.kb.model.Person;
  10.  
  11. @Controller
  12. public class UserController {
  13.  
  14.     @RequestMapping(value="/displayPersonPage",method=RequestMethod.GET)
  15.     public String displayUserPage(Model model){
  16.         Person person = new Person();
  17.         model.addAttribute("person", person);
  18.         return "/personForm";
  19.     }
  20.    
  21.     @RequestMapping(value="/personDetails",method=RequestMethod.POST)
  22.     public String displayUserDetails(@ModelAttribute Person person,Model model){
  23.         model.addAttribute("person", person);
  24.         return "/personDetails";
  25.     }
  26. }
package com.kb.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.kb.model.Person;

@Controller
public class UserController {

	@RequestMapping(value="/displayPersonPage",method=RequestMethod.GET)
	public String displayUserPage(Model model){
		Person person = new Person();
		model.addAttribute("person", person);
		return "/personForm";
	}
	
	@RequestMapping(value="/personDetails",method=RequestMethod.POST)
	public String displayUserDetails(@ModelAttribute Person person,Model model){
		model.addAttribute("person", person);
		return "/personDetails";
	}
}


In the above controller, displayUserDetails method gets the form binded Person object using @ModelAttribute.
It automatically gets all the values entered in the form.

And let’s access the same values in the personDetails.jsp page

Now define the personDetails jsp page as below

  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. Hi user ${person.firstName}
  11. </body>
  12. </html>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hi user ${person.firstName}
</body>
</html>


Right click on project and run as maven install and copy the war file into server’s webapps folder

Start the server and access the below url

http://localhost:8080/SpringMVC_Form/displayPersonPage

img_55fdba2bceb12

img_55fdba4fa6fd1

See the below page with data

img_55fdba6a1585a

Download this project SpringMVCFormBinding.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on