Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Neither BindingResult nor plain target object for bean name 'command' available

  1. #1

    Question Neither BindingResult nor plain target object for bean name 'command' available

    Hi all,

    I use a SimpleFormController (in spring 2.0.3) to use a search criteria named email, then do the search and return the results model to the same jsp page where I display the results list.

    In SimpleFormController I have implemented the onSubmit() method.
    Code:
    protected ModelAndView onSubmit(Object command) throws Exception{
    
    Map model = getService().search(command); model.put("command", command); return new ModelAndView(viewName, modelName, model);
    }
    On the same jsp page, I have the form and the list of records display. It looks like follows.

    Code:
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    ......
    <form:form method="post">Email: <form:input path="email" /> 
    <INPUT type="submit" /></form:form>
    ......
    <!-- displaying part-->
    I do not use any validations or error messages in the form.

    My jsp gets loaded correctly, but when I do the search following exception is thrown.

    Is it a must to use validations? How can I avoid this error?

    - Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    at org.springframework.web.servlet.support.BindStatus .<init>(BindStatus.java:137)
    at org.springframework.web.servlet.tags.form.Abstract DataBoundFormElementTag.getBindStatus(AbstractData BoundFormElementTag.java:179)
    at org.springframework.web.servlet.tags.form.Abstract HtmlElementTag.resolveCssClass(AbstractHtmlElement Tag.java:502)
    at org.springframework.web.servlet.tags.form.Abstract HtmlElementTag.writeDefaultAttributes(AbstractHtml ElementTag.java:479)
    at org.springframework.web.servlet.tags.form.Abstract HtmlInputElementTag.writeDefaultAttributes(Abstrac tHtmlInputElementTag.java:171)
    at org.springframework.web.servlet.tags.form.InputTag .writeTagContent(InputTag.java:175)
    at org.springframework.web.servlet.tags.form.Abstract FormTag.doStartTagInternal(AbstractFormTag.java:90 )
    at org.springframework.web.servlet.tags.RequestContex tAwareTag.doStartTag(RequestContextAwareTag.java:7 7)
    at org.apache.jsp.WEB_002dINF.jsp.customerSearchRecor ds_jsp._jspx_meth_form_input_0(customerSearchRecor ds_jsp.java:153)
    at org.apache.jsp.WEB_002dINF.jsp.customerSearchRecor ds_jsp._jspx_meth_form_form_0(customerSearchRecord s_jsp.java:119)
    at org.apache.jsp.WEB_002dINF.jsp.customerSearchRecor ds_jsp._jspService(customerSearchRecords_jsp.java: 78)
    at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:334)
    at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:314)
    at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:264)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:672)
    at org.apache.catalina.core.ApplicationDispatcher.pro cessRequest(ApplicationDispatcher.java:463)
    at org.apache.catalina.core.ApplicationDispatcher.doF orward(ApplicationDispatcher.java:398)
    at org.apache.catalina.core.ApplicationDispatcher.for ward(ApplicationDispatcher.java:301)
    at org.springframework.web.servlet.view.InternalResou rceView.renderMergedOutputModel(InternalResourceVi ew.java:142)
    at org.springframework.web.servlet.view.AbstractView. render(AbstractView.java:247)
    at org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1103)
    at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:840)
    at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:754)
    at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:399)
    at org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:364)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:173)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:213)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:178)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:126)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:105)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:107)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:148)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:869)
    at org.apache.coyote.http11.Http11BaseProtocol$Http11 ConnectionHandler.processConnection(Http11BaseProt ocol.java:664)
    at org.apache.tomcat.util.net.PoolTcpEndpoint.process Socket(PoolTcpEndpoint.java:527)
    at org.apache.tomcat.util.net.LeaderFollowerWorkerThr ead.runIt(LeaderFollowerWorkerThread.java:80)
    at org.apache.tomcat.util.threads.ThreadPool$ControlR unnable.run(ThreadPool.java:684)
    at java.lang.Thread.run(Thread.java:595)
    Kamal Mettananda
    Software Engineer
    At Computerized World

  2. #2
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by lkamal View Post
    Is it a must to use validations? How can I avoid this error?
    No, validation is not a must. But you must not break the default method calls of Spring controller hierarchy Actually it's your

    Code:
    protected ModelAndView onSubmit(Object command) throws Exception {
      Map model = getService().search(command);
      model.put("command", command);
      return new ModelAndView(viewName, modelName, model);
    }
    changing the default Spring behaviour by returning a "wrong" model.

    What you need to do is not to set up your own model map, but to reuse the existing one and to inject your result.

    I have implemented a RedisplayingSearchFormController in which I return the result as part of referenceData.

    Code:
    protected Map referenceData(HttpServletRequest request,
                                Object command,
                                Errors errors) throws Exception {
      Map referenceData = new HashMap();
      Object result = getResult(request, command, errors);
      referenceData.put("result", result);
      return referenceData;
    }
    
    protected abstract Object getResult(HttpServletRequest request,
                                        Object command,
                                        Errors errors) throws Exception;
    The important part is hidden in Spring's controller hierarchy: The model MUST be retrieved by errors.getModel() - otherwise you have to set up your model exactly in that way Spring does it or you get the exception you observed.

    Hope that gets you the idea.

    Jörg

  3. #3

    Question

    Thanks Heinicke for the quick reply.

    But I have some concerns with ur answer as I'm new to Spring MVC.

    1.
    But you must not break the default method calls of Spring controller hierarchy Actually it's your code changing the default Spring behaviour by returning a "wrong" model.
    As I have read and learned (according to Spring in Action book as well), when you have to forward the user to a display page as the successView after a form submit, you have to implement the onSubmit(Object command, BindException e) method rather than the onSubmitAction().

    Code:
    public ModelAndView onSubmit(Object command, BindException e){
    
    ---- return new ModelAndView(getSuccessView(), "modelName", model)
    }
    2. Is the data returned from the referenceData() method automatically get available inside the jsp page. How can those data be accessed?

    Thanks in advance.
    Kamal Mettananda
    Software Engineer
    At Computerized World

  4. #4
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by lkamal View Post
    As I have read and learned (according to Spring in Action book as well), when you have to forward the user to a display page as the successView after a form submit, you have to implement the onSubmit(Object command, BindException e) method rather than the onSubmitAction().
    I do not really understand your concern here or how it refers to my post. I did not touch the submit handling at all. With the method you overwrite in your code (onSubmit(Object command)) you change the default behaviour of Spring - and that's your problem. There are only different solutions to solve it.

    Btw, the default hook for submits is doSubmitAction(Object command). But this method is of return type void, so you can't do anything with your search result.

    Quote Originally Posted by lkamal View Post
    Is the data returned from the referenceData() method automatically get available inside the jsp page. How can those data be accessed?
    Yes, it is - in the form view, but not the success view (should have mentioned this). The idea of my RedisplayingSearchFormController is indeed to show the form view again after a submit and not the success view as you normally just change the search criteria and submit the form again.

    It is accessible in the JSP just by using it's key in the map; in the example I made above by using "result".

    If you don't want to go the RedisplayingSearchFormController way you might implement onSubmit(Object command, BindException errors) in the following way:

    Code:
    protected ModelAndView onSubmit(Object command, BindException errors) throws Exception {
      Object searchResult = getService().search(command);
      Map model = errors.getModel();
      model.put("result", searchResult);
      return super.onSubmit(command, errors);
    }
    In that way you don't break the default method call flow of the Spring controller hierarchy, but you only modify the model to your needs. As in my RedisplayingSearchFormController the search result is available in the JSP by using "result".

    Jörg

  5. #5

    Question

    Thanks for the comments. I have corrected the errors that I was doing.

    But still I have some issue. (Sorry for bothering too much).

    Code:
    protected ModelAndView onSubmit(
              HttpServletRequest request, HttpServletResponse response,                                                                         
              Object command, BindException errors) throws Exception {
        
        prForm(request, response, errors.getModel(), command);	
        return super.onSubmit(request, response, command, errors);
    }
    
    public void prForm(
              HttpServletRequest request, HttpServletResponse response,                                                                         
              Map model, Object command) throws Exception {
    	
         model.put("testValue", "Test Value");
    }
    Now no errors occur as I'm using the Model returned from the errors object.

    But still I can not access this Model from the jsp. My jsp is as follows.

    Code:
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
    
    <form:form method="post">Email: <form:input path="email" /> 
    <INPUT type="submit" /></form:form>
    
    <c:if test="${testValue}" >
    	<c:out value="${testValue}" />
    </c:if>
    Non of these two prints the value since the model name. So the data inside the Model is not available just through the keys (without the model name).

    But when I was using the previous methodology of creating a new ModelAndView object with;

    new ModelAndView("viewName", "modelName", model);

    I could access the data by as follows.
    Code:
    <c:out value="${modelName.testValue}" />
    Am I doing anything wrong here?
    Kamal Mettananda
    Software Engineer
    At Computerized World

  6. #6
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by lkamal View Post
    (Sorry for bothering too much)
    No problem. If it were too much bothering I would just stop answering

    Your questions are absolutely valid - and I might learn from them as well.

    Quote Originally Posted by lkamal View Post
    Now no errors occur as I'm using the Model returned from the errors object. But still I can not access this Model from the jsp. [..] Am I doing anything wrong here?
    You make me a bit clueless. It should work.

    I can't see a difference at the end between your working code and the one with the model map. Just have a view at the ModelAndView constructors and the ModelMap that gets instantiated. With the default code ModelMap.addAllObjects(Map) (with Map being the one from errors.getModel()) is called, with your one ModelMap.addObject(modelName, modelObject) (calling Map.putAll(Map) and Map.put(key, value)).

    Can you debug the code and have a look at the ModelMap and how it is different from the objects in it?

    Jörg

  7. #7
    Join Date
    Dec 2008
    Posts
    1

    Default answer Neither BindingResult nor plain target object for bean name 'command' availab

    If you are using a controller and
    your context path does not supported by controller
    you can get this message
    if you have a controller this controller does not have this attribute

    at Spring referance this described clearly

    The ControllerClassNameHandlerMapping finds all of the various handler (or Controller) beans defined in its application context and strips 'Controller' off the name to define its handler mappings.

    Let's look at some more examples so that the central idea becomes immediately familiar.

    *

    WelcomeController maps to the '/welcome*' request URL
    *

    HomeController maps to the '/home*' request URL
    *

    IndexController maps to the '/index*' request URL
    *

    RegisterController maps to the '/register*' request URL
    *

    DisplayShoppingCartController maps to the '/displayshoppingcart*' request URL

    (Notice the casing - all lowercase - in the case of camel-cased Controller class names.)

    In the case of MultiActionController handler classes, the mappings generated are (ever so slightly) more complex, but hopefully no less understandable. Some examples (all of the Controller names in this next bit are assumed to be MultiActionController implementations).

    *

    AdminController maps to the '/admin/*' request URL
    *

    CatalogController maps to the '/catalog/*' request URL

    If you follow the pretty standard convention of naming your Controller implementations as xxxController, then the ControllerClassNameHandlerMapping will save you the tedium of having to firstly define and then having to maintain a potentially looooong SimpleUrlHandlerMapping (or suchlike).

    The ControllerClassNameHandlerMapping class extends the AbstractHandlerMapping base class so you can define HandlerInterceptor instances and everything else just like you would with many other HandlerMapping implementations.

    Soner Sivri
    From Turkey

  8. #8
    Join Date
    May 2008
    Location
    Central Florida, U.S.A.
    Posts
    36

    Default

    Inserting into the errors model is not good practice, as it may break depending up the type of DataBinder being used.

    For instance, the AbstractBindingResult#getModel() actually creates a new HashMap. So any changes your adding will be lost because this method will get called again from inside the onSubmit(Object command, BindException errors) method in the SimpleFormController. Thus creating a new HashMap without your model object (which is what is happening here).

    Instead you should add the model object to the ModelAndView after the submit action occurs. So do this instead:

    Code:
    protected ModelAndView onSubmit(
              HttpServletRequest request, HttpServletResponse response, 
              Object command, BindException errors) throws Exception {
        
        ModelAndView modelAndView = super.onSubmit(request, response, command, errors);
        prForm(modelAndView);	
        return modelAndView;
    }
    
    public void prForm(ModelAndView modelAndView) throws Exception {
    	modelAndView.addObject("testValue", "Test Value");
    }
    Now your object should be available to your template.

  9. #9
    Join Date
    Aug 2008
    Posts
    3

    Default

    If for the view form a controller should be used extend the AbstractFormController.
    Be careful to include the command class in the view like this

    Code:
            ModelAndView modelAndView = new ModelAndView("comments");
            modelAndView.addObject("commentComp", new CommentComp());
            modelAndView.addObject("model", comments);
            return modelAndView;

  10. #10
    Join Date
    Jan 2010
    Posts
    2

    Default Neither BindingResult nor plain target object for bean name 'command' available

    Did you get this to work? I have been trying to achieve the same jsp (i.e. search and then return the search results to the same jsp), but I'm finding it hard to put the search results into the model for display.

    I'd like to see how the RedisplayingSearchFormController idea works, could you send me more details? i.e. how do you get two controllers to 'forward' to the same jsp, when I tried this it just gave me binding errors.

Posting Permissions

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