Reputation: 1562
I have a register.jsp
page where I have bind the following data to a modelAttribute userform
.
<form:form method="post" modelAttribute="userform"
enctype="multipart/form-data" autocomplete="off" class="form">
<spring:bind path="firstName">
<label>First Name: </label>
<form:input path="firstName" placeholder="First Name" />
</spring:bind>
<spring:bind path="lastName">
<label class="control-label">Last Name: </label>
<form:input path="lastName" placeholder="Last Name" />
</spring:bind>
</form:form>
where get and post methods on the controller are:
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(@ModelAttribute("userform") Employee employee, Model model) {
List<Role> roleList = roleService.getAllList();
model.addAttribute("roleList", roleList);
model.addAttribute("userform", employee);
return "employee/register";
}
@RequestMapping(value = { "/register" }, method = RequestMethod.POST)
public String processRegistration(@Valid @ModelAttribute("userform") Employee employee, BindingResult result,
Model model, HttpServletRequest request) throws IllegalStateException, IOException {
//(expecting the data from jsp) nothing in the employeee object :(
//dosomething
return "employee/register";
}
Although I have used same name userform
and the attributes name on the entity Employee
is exactly the same, I am unable to get the form data from JSP to the Controller. I must be doing something wrong here but could not find it. Any help would be appreciated.
Upvotes: 0
Views: 4424
Reputation: 1562
I figured out the answer myself.
Since I have used enctype="multipart/form-data"
in my JSP form, it needed a configuration of bean called "CommonsMultipartResolver" in the servlet-context.xml
. The bean as a whole can be written as
<beans:bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
Upvotes: 1
Reputation: 2325
In addition to @Ralph answer I want to Explain why you need to remove below lines from your jsp
<spring:bind path="firstName">
<spring:bind path="lastName">
As Per Answer given on this link Difference between spring:bind and form:form
Its Unnecessary to use <spring:bind>
when you use <form:form>
because both does same thing with respect to model attributes.
here <form:form>
also generates HTML form markup, whereas with <spring:bind>
you need to write markup yourself.
Also you should know how sping handles binding of Domain Object, As @Ralph mention in his answer You need to use form backing object and modify you method like below
@RequestMapping(value = "/register", method = RequestMethod.GET)
public String register(Employee employee, Model model) {
}
Q- What is happening in above code?
A- When Spring MVC finds out domain object is present as a method argument an instance is automatically introduced by Spring MVC,with respect to domain objects this instance is same as instance created by new keyword like below
Employee emp=new Employee();
The properties of Employee domain object are normally uninitialized accept any parameters with same names as Employee object properties available in the URL query String. Spring MVC uses Java reflection to dinf out names of properties of the domain object.
For more information visit Binding and Validation of Handler Parameters in Spring MVC Controllers
Upvotes: 0