简体   繁体   English

在这种情况下,为什么会出现错误“ Bean名称'user'的BindingResult和普通目标对象均不可用”?

[英]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.

相关问题 BeanResult的BindingResult或普通目标对象都不能用作请求属性,我也不知道为什么 - Neither BindingResult nor plain target object for bean name available as request attribute, and I don't know why BeanResult&#39;user&#39;的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'user' available as request attribute Spring-mvc 错误:Bean 名称“userBean”的 BindingResult 和普通目标对象都不能用作请求属性 - Spring-mvc error: Neither BindingResult nor plain target object for bean name 'userBean' available as request attribute 错误:Bean名称“ id”的BindingResult和普通目标对象都不能用作请求属性 - Error:Neither BindingResult nor plain target object for bean name 'id' available as request attribute 为什么我会在 Spring 中收到此消息? “ BindingResult 和 bean 名称 &#39;removedTeam&#39; 的普通目标对象都不能用作请求属性” - Why am I getting this message in Spring? “Neither BindingResult nor plain target object for bean name 'removedTeam' available as request attribute” BindingResult和bean的普通目标对象都不能作为请求属性使用 - Neither BindingResult nor plain target object for bean available as request attribute 循环时获取“既没有 BindingResult 也没有可用作请求属性的 bean 名称‘bean 名称’的普通目标对象” - Getting "Neither BindingResult nor plain target object for bean name 'bean name' available as request attribute" when looping BeanResult的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name available as request attribute Bean名称“所有者”的BindingResult和普通目标对象都不能用作请求属性 - Neither BindingResult nor plain target object for bean name 'owner' available as request attribute IllegalStateException:BeanResult&#39;getAppointment&#39;的BindingResult和普通目标对象都不能用作请求属性 - IllegalStateException: Neither BindingResult nor plain target object for bean name 'getAppointment' available as request attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM