The below register.jsp is my user registration page and want to store user details to db using hibernate but when register.jsp file runs its keep showing an exception. and I have mapped the form action in controller.
register.jsp file is: In this file user details and I given action "reg" which is mapped in EmployeeController.
Register.jsp
<form:form method="POST" action="reg.html">
<table border="0">
<tr>
<td colspan="2" align="center"><h2>Spring MVC Form Demo - Registration</h2></td>
</tr>
<tr>
<td>User Name:</td>
<td><form:input path="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><form:password path="password" /></td>
</tr>
<tr>
<td>E-mail:</td>
<td><form:input path="email" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Register" /></td>
</tr>
</table>
</form:form>
EmployeeController.java
@Controller
public class EmployeeController {
@Autowired
private UserService userService;
@RequestMapping(value = "/reg", method = RequestMethod.POST)
public ModelAndView saveUser(@ModelAttribute("command") UserBean userBean,
BindingResult result) {
User user = prepareModelUser(userBean);
userService.addUser(user);
return new ModelAndView("RegistrationSuccess.html");
}
}
From register.jsp I am trying to call saveUser method which is in EmployeeController.java but whenever I am running register.jsp I am getting an exception
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute.
I tried with commandName and action in the form and in the controller I have mapped based on that. even though, it gives same exception like above. so how I want to configure the controller to submit this register.jsp form. is I required to add configure the controller explicit or what.. And in EmployeeController.java I have an another form controller with same default "command" form submit. that works fine. but here in this I have the possibilities what I searched. but does not works.
you are missing commandName in the form tag and also you are doing wrong in your server side method do it like the following
In form
<form:form method="POST" action="reg.html" commandName="userBean" modelAttribute="userBean" >
and in server side have it like
public ModelAndView saveUser(@ModelAttribute("userBean") UserBean userBean
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.