Results 1 to 10 of 10

Thread: SpringMVC error: Neither BindingResult nor plain target object for bean name

  1. #1
    Join Date
    Jul 2012
    Posts
    11

    Default SpringMVC error: Neither BindingResult nor plain target object for bean name

    Environment: SpringMVC-3.1.2 Maven Netbeans Glassfish

    Got following error when trying to access "index.jsp"

    Code:
    SEVERE: Neither BindingResult nor plain target object for bean name 'user' available as request attribute 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:178) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142) at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:126) at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421) at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142) at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) at 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) at org.apache.jsp.user_jsp._jspService(user_jsp.java:76) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595) at 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) at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330) at 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) at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725) at 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) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104) at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90) at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79) at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54) at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) at com.sun.grizzly.ContextTask.run(ContextTask.java:71) at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) at java.lang.Thread.run(Thread.java:722)

    index.jsp
    Code:
           <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
    Code:
    @Controller
    public class UserController
    {
        @RequestMapping(value = "/createuser.do", method = RequestMethod.POST)
        public String postCreate(@ModelAttribute("user") User user, BindingResult result)
        {
            System.out.print("user name: " + user.getUserName());
            return "welcome";
        }
        
        @RequestMapping(value = "/createuser.do", method = RequestMethod.GET)
        public String getCreate(ModelMap mm) {
            mm.addAttribute("auser", new User("Andy"));
            return "welcome";
        }
    }

    welcome.jsp
    Code:
    <%@ 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="${auser.userName}" ></c:out>
        </body>
    </html>
    It works perfectly when I access http://localhost:8080/NBGFMN/createuser.do, the browser displays "Andy";

    The problem is I can't access, http://localhost:8080/NBGFMN/index.jsp

    The index.jsp displays without error only if I remove "<form:input path="userName" />" from index.jsp; Then click "submit" button, console outputs "user name: null"
    Last edited by wlin; Jul 20th, 2012 at 04:19 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Please use [ code[/code ] tags when posting stack traces...

    Your model attribute is 'auser' not 'user' ... Also you should pass through the controller, and if yuou access the index.jsp now it doesn't pass through the controller which doesn't register the user as a model attribute.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2012
    Posts
    11

    Default

    Thank you very much. I got it working. But I have several doubts:

    Is this the normal way to do a POST:
    (1) browser access controller's url first
    (2) controller(GET method) create a User instance with no values: model.addAttribute(new User());
    (3) the jsp shows empty fields such as "Name", "Email", "Phone", etc
    (4) enter values for the fields above and submit to a POST method of a controller by using "ModelAttribute"

    Is it possible to skip step(2) above? I feel it is kind of inconvenience because every time you have to manually create an instance (new User()) first. Or ideally is there a way to skip both step (1) and (2), and allow browser to access jsp directly and POST an object?


    Quote Originally Posted by Marten Deinum View Post
    Please use [ code[/code ] tags when posting stack traces...

    Your model attribute is 'auser' not 'user' ... Also you should pass through the controller, and if yuou access the index.jsp now it doesn't pass through the controller which doesn't register the user as a model attribute.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    No... You need first to go to the server to construct the form backing object, this could also be an object that comes from the database so you really need to pass through the controller. And frankly speaking I don't see the problem as your jsp files shouldn't be directly accessible, but that is IMHO of course.

    You could make it work but then you would loose the ability to use the form tags and do binding/filling of the input fields.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    Jul 2012
    Posts
    11

    Default

    Got it. Thank you Marten.

    Quote Originally Posted by Marten Deinum View Post
    No... You need first to go to the server to construct the form backing object, this could also be an object that comes from the database so you really need to pass through the controller. And frankly speaking I don't see the problem as your jsp files shouldn't be directly accessible, but that is IMHO of course.

    You could make it work but then you would loose the ability to use the form tags and do binding/filling of the input fields.

  6. #6
    Join Date
    Jul 2012
    Posts
    11

    Default

    a follow up question:

    How to let step (2) execute just once? E.g. when I go to other page and come back to this page again (by clicking <a href="user.do"/> ), I want the previous input of "Name", "Email" and "Phone" in the text fields rather than creating a new instance (new User()) over and over again.

    How to modify?

    Code:
    @RequestMapping("/user.do")
    public class UserController
    {
        @ModelAttribute
        public UserForm createUserForm()
        {
            return new UserForm();
        }
        
        @RequestMapping(method=RequestMethod.GET)
        public String userSearch()
        {
            return "search.user";
        }
    }
    Last edited by wlin; Jul 25th, 2012 at 04:15 PM.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    Not sure if that is really what you want but alas. I suggest the web chapter of the reference guide and especially the use of the @SessionAttributes annotation.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  8. #8
    Join Date
    Jul 2012
    Posts
    11

    Default

    With @SessionAttributes, the value of the object is saved only after I click "Submit" button (POST), which makes sense because it is equivalent to HttpSession.setAttribute in servlet.

    Unfortunately, this is not what I need.

    I want to keep user's input without doing "POST". i.e. I have several Tabs in one page, called "search user", "search phone number", "search address", "search business" etc. Suppose I enter "Lisa" as "First Name" under "search user" tab and then click other tab, I want "Lisa" still in the screen after I switch back to "search user" tab.

    I think this is very common utilization but is seems that with Spring controller's mechanism it is impossible to do so, unless I give up the model attribute binding and use the traditional form field submission. Is my understanding correct?

    Quote Originally Posted by Marten Deinum View Post
    Not sure if that is really what you want but alas. I suggest the web chapter of the reference guide and especially the use of the @SessionAttributes annotation.

  9. #9
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    With @SessionAttributes, the value of the object is saved only after I click "Submit" button (POST), which makes sense because it is equivalent to HttpSession.setAttribute in servlet.
    Then you have an error in your setup, the type of request doesn't matter. Your first request should be a get, the second a post. You can save the model attribute in the session in between requests that is the whole point of @SessionAttributes. However those GET and POST methods should be in the same controller! @SessionAttributes isn't intended to be used to share objects between controllers!!!


    I think this is very common utilization but is seems that with Spring controller's mechanism it is impossible to do so, unless I give up the model attribute binding and use the traditional form field submission. Is my understanding correct?
    Your understanding is wrong as mentioned above.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  10. #10
    Join Date
    Jul 2012
    Posts
    11

    Default

    Hi Marten, Thank you so much for you patience and help.

    I actually got your point. I did use "GET" first then "POST", and they are in the same controller.

    The thing is, @SessionAttributes can "remember" what I typed only if I POST that object. E.g., I key enter "Lisa" as name and "NW 15 street" as street, then click "submit" button, these 2 fields are "remembered" in screen no matter how I switch between tabs.

    However, if I append "Apartment 101" to "NW 15 street" without clicking "submit", this "Apartment 101" will not be in screen if I go to other tab come back to this tab, only "NW 15 street" is there.

    I solved it by using javascript on the tabs!



    Quote Originally Posted by Marten Deinum View Post
    Then you have an error in your setup, the type of request doesn't matter. Your first request should be a get, the second a post. You can save the model attribute in the session in between requests that is the whole point of @SessionAttributes. However those GET and POST methods should be in the same controller! @SessionAttributes isn't intended to be used to share objects between controllers!!!


    Your understanding is wrong as mentioned above.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •