Results 1 to 7 of 7

Thread: Neither BindingResult nor plain target object for bean...

  1. #1
    Join Date
    Jan 2008
    Posts
    3

    Question Neither BindingResult nor plain target object for bean...

    Hello,

    I saw some threads already on this but because I'm a newb, I'm having trouble applying it to my program:

    I'm writing a simple web app, that starts with a list of employees in a form that you select with a radio button. This employee that is selected plus the action is passed through the session to the next form for editing.

    I wanted to bind the an Employee command object for Validating the form here is the dispatcher-servlet.xml

    Code:
        <bean id="employeeFormValidator" class="bus.EmployeeFormValidator"/> 
        <bean id="employeeFormController" class="web.EmployeeFormController">
            <property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>employee</value></property>
            <property name="commandClass"><value>bus.Employee</value></property>
            <property name="validator"><ref bean="employeeFormValidator"/></property>
            <property name="formView"><value>index</value></property>
            <property name="successView"><value>salaryincrease.htm</value></property>
            <property name="employeeManager">
                <ref bean="emplMan"/>
            </property>
        </bean>
    relevant methods in my EmployeeFormController( extends SimpleFormController) class are:

    Code:
        @Override
        protected ModelAndView showForm(HttpServletRequest request,
                                    HttpServletResponse response,
                                    BindException errors,
                                    Map controlModel){
            
        
             
            String now = (new java.util.Date()).toString();
            logger.debug("returning index view with " + now);
    
            Map myModel = new HashMap();
            myModel.put("now", now);
            myModel.put("employees", getEmployeeManager().getEmployees());
    
            return new ModelAndView(getFormView(), "model", myModel);
        }    
        
        
        @Override
        protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors){
    
            
            String action = request.getParameter("action");
            String id = request.getParameter("id");
            
            logger.debug("action: " + action + " id: " + id);
            
            HttpSession sess =  request.getSession(true);
            sess.setAttribute("action", action);           
            sess.setAttribute("id", id); 
                    
            return new ModelAndView(new RedirectView(getSuccessView()));
            
        }

    Relevant jsp code is:

    Code:
                    
    <spring:bind path="employee.id">
            <c:forEach items="${model.employees}" var="empl">
                   <tr>
                        <td><input type="radio" name="id" value="${empl.id}"/></td><td><c:out value="${empl.name}"/></td><td>$ <c:out value="${empl.salary}"/></td>
                    </tr>
             </c:forEach>
     </spring:bind>

    and finally the error im seeing:

    Code:
    2008-01-03 09:41:58,281 ERROR [org.springframework.web.servlet.tags.BindTag] - Neither BindingResult nor plain target object for bean name 'employee' available as request attribute
    javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute
    	at org.springframework.web.servlet.tags.BindTag.doStartTagInternal(BindTag.java:120)
    	at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:77)
    	at org.apache.jsp.WEB_002dINF.jsp.index_jsp._jspService(index_jsp.java from :114)
    	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:93)
    and help to resolve the error as well as criticisms of best practices would be appreciated. thanks in advance.

    -Matt
    Last edited by mattrobert; Jan 3rd, 2008 at 09:58 AM.

  2. #2
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    I think you need to add a formBackingObject method to your controller and have it return(new Employee). The method's name strikes me as a less than ideal choice; I would have preferred formBindingObject, or formBoundObject for it. It returns an object that the <spring:bind> will bind to the form; what the documentation mysteriously refers to as "the bound object".

    I'm also thinking that your <spring:bind> path should be just employee, not employee.id.
    Last edited by lumpynose; Jan 4th, 2008 at 12:18 AM.

  3. #3
    Join Date
    Jan 2008
    Posts
    3

    Default

    thanks for the response, it is appreciated. I think I fixed it late last night. what I did was add the referenceData method and trashed the show form method:

    Code:
      @Override
        protected Map referenceData(HttpServletRequest request, Object command, Errors errors) {
               Enumeration e = request.getAttributeNames();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                Object value = request.getAttribute(key);
                logger.debug("debug: attribute->   " + key + " = " + value);
            }
    
               
            String now = (new java.util.Date()).toString();
            logger.debug("returning index view with " + now);
    
            Map myModel = new HashMap();
                   myModel.put("now", now);
            myModel.put("employees", getEmployeeManager().getEmployees());
    
            return myModel;
        }

    the bind to employee.id works. I then changed all references to model.<name> in the jsp to just <name>. I was then able to see in the debug logs the command objects as well as the other model objects get added to the model:

    Code:
    2008-01-03 17:18:12,812 DEBUG [org.springframework.web.servlet.view.JstlView] - Added model object 'org.springframework.validation.BindingResult.employee' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'index'
    2008-01-03 17:18:12,812 DEBUG [org.springframework.web.servlet.view.JstlView] - Added model object 'employee' of type [bus.Employee] to request in view with name 'index'
    2008-01-03 17:18:12,812 DEBUG [org.springframework.web.servlet.view.JstlView] - Added model object 'now' of type [java.lang.String] to request in view with name 'index'
    2008-01-03 17:18:12,812 DEBUG [org.springframework.web.servlet.view.JstlView] - Added model object 'employees' of type [java.util.ArrayList] to request in view with name 'index'
    I have another question if youre up to it. I'm having trouble binding other employee fields. what is the best way to bind to a hidden field. I want to display the name of the employee and bind to the name as a hidden field. I've got this:

    Code:
    <spring:bind path="employee.name">
    	<td><c:out value="${empl.name}"/><input type="hidden"  name="<c:out value="${status.expression}"/>" value="<c:out value="${empl.name}"/>" /></td>
     </spring:bind>
    when i add the spring:bind and input tags shown above the submit on the form doesn't function anymore

  4. #4
    Join Date
    Oct 2007
    Posts
    4

    Default Question on your design

    Hi,

    I see you are using <spring:bind> tags instead of <form:form> tags.

    Why? Because of the limitations and workaround issues? (like with <form:select> needing custom property editor, etc..)

    Thanks.

    - S

  5. #5
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

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

    Default

    Quote Originally Posted by mattrobert View Post
    what I did was add the referenceData method and trashed the show form method
    It was indeed the showForm() method since it broke Spring's setup of the model - and your own was wrong. You configured the command name to be "employee" but added the model as "model". That's why Spring could not find the command object.

    Joerg
    This post can contain insufficient information.

  7. #7
    Join Date
    Jan 2008
    Posts
    3

    Default

    im not sure why im using the bind vs the form...haha. ill look into that, im still figuring out the ins and outs of this. I appreciate the heads up

Posting Permissions

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