【发布时间】:2014-08-07 07:35:29
【问题描述】:
下面的 register.jsp 是我的用户注册页面,希望使用 hibernate 将用户详细信息存储到 db,但是当 register.jsp 文件运行时,它一直显示异常。我已经在控制器中映射了表单动作。
register.jsp 文件是: 在此文件中,用户详细信息和我给出了在 EmployeeController 中映射的操作“reg”。
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中我有另一个表单控制器具有相同的默认“命令”表单提交。效果很好。但在这里我有我搜索的可能性。但不起作用。
【问题讨论】:
-
我已经尝试过你在那个链接中所说的。即)我添加了 commanName="reg" 并在控制器中放入了 @ModelAttribute("reg") 但我仍然得到异常 ie)java.lang.IllegalStateException: Bean name 'reg' 的 BindingResult 和普通目标对象都不能用作请求属性。为什么会这样......就像另一种形式一样工作,即)作为默认值,没有 commandName 并且在控制器中使用 ModelAttribute("command")。你能解决吗..
-
非常感谢你..我明白了错误..