[英]Springframework : Neither BindingResult nor plain target object for bean name
[英]Exception: Neither BindingResult nor plain target object for bean name
下面的register.jsp是我的用戶注冊頁面,想要使用hibernate將用戶詳細信息存儲到db,但是當register.jsp文件運行時,其保持顯示異常。 我已經在控制器中映射了表單動作。
register.jsp文件是:在此文件中,用戶詳細信息和給定的操作“ reg”映射到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");
}
}
我試圖從register.jsp調用EmployeeController.java中的saveUser方法,但是每當我運行register.jsp時,我都會遇到異常
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.
我嘗試了commandName和action的形式,並在基於此的控制器中進行了映射。 即使,它給出了與上面相同的例外。 所以我要如何配置控制器以提交此register.jsp表單。 我是否需要添加配置控制器的顯式或什么。。在EmployeeController.java中,我還有另一個具有相同默認“命令”表單提交的表單控制器。 效果很好。 但是在這里,我有尋找的可能性。 但不起作用。
您在form標記中缺少commandName,並且在服務器端方法中做錯了,如下所示
通知
<form:form method="POST" action="reg.html" commandName="userBean" modelAttribute="userBean" >
在服務器端有
public ModelAndView saveUser(@ModelAttribute("userBean") UserBean userBean
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.