For forms directly related to a model object, for example, a form to add User
, the submitted form data can be automatically saved in an instance of that object.
In the controller method processing the form submission, add the object as an argument and make the field names in the JSP match its attributes:
Add a
User
argument annotated with@ModelAttribute
to the controller method processing the form submission:@RequestMapping(value="addUser", method=RequestMethod.POST) public void addUser(@ModelAttribute User user) { ...
In the JSP, make sure that the form fields correspond to the existing attributes of the object:
<form:input path="firstName" /> <form:input path="age" />