[英]Why do I get an error “Neither BindingResult nor plain target object for bean name 'user' available as request attribute” in this case?
Why do I get this error? 为什么会出现此错误? Before connecting the validation and adding forms of the form: label / input - everything worked for me
在连接验证并添加以下表单之前:标签/输入-一切对我有用
@RequestMapping(method = POST, value = "/reg")
public String registration(@ModelAttribute("userReg") @Valid User user, BindingResult bindingResult,
HttpSession session) {
if(bindingResult.hasErrors()) return "redirect:/reg";
if (userDAO.findByLogin(user.getLogin()) == null) {
userDAO.persist(user);
session.setAttribute("login", user.getLogin());
return "welcomepage";
}
return "Registration";
}
Exceptions: 例外:
org.apache.jasper.JasperException: An exception occurred processing JSP page /Registration.jsp at line 18
15: <form:form action="/reg" method="post" modelAttribute="user">
16: <table>
17: <tr>
18: <td><form:label path="login">Login:</form:label></td>
19: <td> <form:input path="login"/> </td>
20: <td> <form:errors path="login"/></td>
21: </tr>
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
Continuing the form from the error: 从错误继续表格:
...<p>
<tr>
<td> <form:label path="password">Password:</form:label></td>
<td> <form:input path="password"/> </td>
<td> <form:errors path="password"/></td>
</tr>
<p>
<input type="submit" value="Registration">
</form:form>
This error happens because you don't pass the object 'user' to Model in your controller. 发生此错误是因为您没有将对象“用户”传递给控制器中的“模型”。 Returning ModelAndView is a option but you can put the object in the Model object and return your page address.
返回ModelAndView是一个选项,但是您可以将对象放在Model对象中并返回页面地址。
@GetMapping("/user")
public View retrieveUser(ModelMap model, HttpServletRequest request) {
model.addAttribute("user", userService.getUser());
String contextPath = request.getContextPath();
return new RedirectView(contextPath); }
The problem was that the page did not have the required attribute. 问题是页面没有必需的属性。 And the error crashed because the name field was not found in the "user" that was not filled.
由于未在未填写的“用户”中找到名称字段,因此错误崩溃了。 To fix the error i added object ModelAndView() with "user" atribute.
为了解决该错误,我添加了带有“用户”属性的对象ModelAndView()。
@RequestMapping(method = GET, value = "/reg")
public ModelAndView registration() {
return new ModelAndView("Registration","user",new User());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.