SpringMVC错误:BindingResult和bean名称'user'的普通目标对象都不可用作请求属性

时间:2012-07-20 13:31:37

标签: spring-mvc

环境:SpringMVC Maven Netbeans Glassfish

  

严重:BindingResult既不是bean名称也不是普通的目标对象' user'可用作请求属性   java.lang.IllegalStateException:既不是BindingResult也不是bean名称的普通目标对象' user'可用作请求属性       在org.springframework.web.servlet.support.BindStatus。(BindStatus.java:141)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)       在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)       at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126)       在org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)       在org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)       在org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)       在org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)       at org.apache.jsp.user_jsp._jspx_meth_form_input_0(user_jsp.java:178)       at org.apache.jsp.user_jsp._jspx_meth_form_form_0(user_jsp.java:120)       在org.apache.jsp.user_jsp._jspService(user_jsp.java:76)       在org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:847)       在org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)       在org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)       在org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:847)       在org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)       在org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)       在org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)       在com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)       at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)       在org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)       at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)       at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)       在com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)       在com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)       at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)       at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)       在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)       在com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)       在com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)       at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)       在com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)       在com.sun.grizzly.ContextTask.run(ContextTask.java:71)       at com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)       在com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)       在java.lang.Thread.run(Thread.java:722)

user.jsp:

    <form:form action="createuser.do" modelAttribute="user" method="POST">
        <table align="center" >
            <tr>
                <td>User Name :</td>
                <td><form:input path="userName"/></td>
            </tr>
            <tr>  
                <td><input type="submit" value="Submit" /></td>
            </tr>
        </table>
    </form:form>

我的控制器:

@Controller
public class UserController {
    @RequestMapping(value = "/createuser.do", method = RequestMethod.POST)
    public void create(@ModelAttribute("user")  User user, BindingResult result) {
        System.out.print("user name: "+user.getUserName());
    }
}

我访问&#34; http:// localhost:8080 / NBGFMN / user.jsp&#34;我在浏览器上出现此错误 java.lang.IllegalStateException:既不是BindingResult也不是bean名称的普通目标对象&#39; user&#39;可用作请求属性

如果我从jsp中删除form:input path="userName"),当我点击提交按钮控制台输出&#34;用户名:null&#34;时,jsp会正确显示。

stackoverflow中有很多关于同样错误的帖子,但没有一个解决方案适合我。

我添加了以下GET方法和welcome.jsp

@RequestMapping(value = "/welcome.do", method = RequestMethod.GET)
public ModelAndView printHello()
{
    ModelAndView m = new ModelAndView("welcome");
    m.addObject("ppp", "hello");
    return m;
}

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <c:out value="${ppp}" ></c:out>
    </body>
</html>

访问&#34; http:// localhost:8080 / NBGFMN / welcome.do&#34;它运作得很好。

我添加以下GET(创建用户),它可以工作:

@RequestMapping(value = "/createuser.do", method = RequestMethod.GET)
public String create(ModelMap mm) 
{
    mm.addAttribute("auser", new User("Andy"));
    return "welcome";
}

<body>
    <c:out value="${auser.userName}" ></c:out>
</body>

我访问&#34; http:// localhost:8080 / NBGFMN / createuser.do&#34;,浏览器输出&#34; Andy&#34;

但表格POST仍无效。

2 个答案:

答案 0 :(得分:0)

您是否尝试在名称为GET的{​​{1}}处理程序中设置请求属性,并为某个包含字段user的对象赋值?像这样:

userName

在JSP中,您需要为@Controller public class UserController { @RequestMapping(value = "/createuser.do", method = RequestMethod.GET) public void create(ModelMap mm) { mm.addAttribute("user", new UserFormObject()); } @RequestMapping(value = "/createuser.do", method = RequestMethod.POST) public void create(@ModelAttribute("user") User user, BindingResult result) { System.out.print("user name: "+user.getUserName()); } } 元素指定method=postdefault method is GET,这与您的处理程序不匹配。

答案 1 :(得分:0)

在这方面我必须提出两点:

  1. BindingResult与即时@ModelAttribute条目
  2. 相关联
  3. 报告错误时,将其与模型属性关联。此属性名称应出现在@ModelAttribute中,并且区分大小写。
  4. 样品:

    @ModelAttribute("side") SideDetails s,BindingResult sideResult          
    @ModelAttribute("track") TrackDetails t, BindingResult trackResult
    

    在上面的示例中,sideResult的目标为SideDetails。同样,trackResultTrackDetails作为目标。

    在使用sideResult报告错误时,将在模型SideDetails中搜索属性名称。如果找不到此属性,我们会收到此博客中报告的错误。

    希望这有帮助。