Spring 3 MVC 错误:BindingResult 和 bean 名称“用户”的普通目标对象都不是;可用的

发布于 12-11 08:09 字数 1770 浏览 0 评论 0原文

我知道这个问题可能已在其他地方得到解决,但我无法找到满意的解决方案来解决我的问题。顺便说一句,我正在使用 spring 3.0.2

Login.jsp

<form:form id="_LoginForm" name="LoginForm" modelAttribute="user" action="login" method="POST">
    <form:input path="username" value=""/>
    <form:input path="password" value=""/>
    <input type="submit" value="Submit"/>

LoginController.java

@RequestMapping(value="login", method=RequestMethod.POST)
public String login(@ModelAttribute("user") User user, BindingResult result) {
    System.out.println("recd request");
    return null;
}

当我尝试访问 login.jsp 页面时,出现以下错误:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:123)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)

I know this issue might have been addressed elsewhere but I'm unable to find a satisfactory solution to my problem. Btw, I'm working with spring 3.0.2

Login.jsp

<form:form id="_LoginForm" name="LoginForm" modelAttribute="user" action="login" method="POST">
    <form:input path="username" value=""/>
    <form:input path="password" value=""/>
    <input type="submit" value="Submit"/>

LoginController.java

@RequestMapping(value="login", method=RequestMethod.POST)
public String login(@ModelAttribute("user") User user, BindingResult result) {
    System.out.println("recd request");
    return null;
}

When I try to access the login.jsp page, I get the following error:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:123)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:409)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

偏爱自由2024-12-18 08:09:38

您能否在控制器中提供您的 RequestMethod.GET 方法?
只是想确保您也在 GET 方法中添加 modelAttribute。

Can you please provide your RequestMethod.GET method in the controller?
Just want to make sure you are adding the modelAttribute in the GET method as well.

娇俏2024-12-18 08:09:38

我添加了以下方法来使其工作,尽管我觉得必须有更好的方法来使其工作,而不必每次都编写设置方法。

applicationContext.xml

<mvc:view-controller path="/" view-name="index" />

index.jsp

<jsp:forward page="index.action"/>

LoginController.java

@RequestMapping(value="index.action", method=RequestMethod.GET)
public String setupLogin(Map<String, Object> modelMap) {        
    modelMap.put("user", new User());
    return "Login";
}

I added the the following method to make this work, though I feel there must be a better way to get this working without having to write a setup method everytime.

applicationContext.xml

<mvc:view-controller path="/" view-name="index" />

index.jsp

<jsp:forward page="index.action"/>

LoginController.java

@RequestMapping(value="index.action", method=RequestMethod.GET)
public String setupLogin(Map<String, Object> modelMap) {        
    modelMap.put("user", new User());
    return "Login";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文