Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: binding to arraylist of objects in my form

  1. #1

    Default binding to arraylist of objects in my form

    how to you bind to a ArrayList object, because i cant make my <spring:bind> work.

    example:
    Code:
    ...formBackingObject&#40;...&#41;&#123;
    ArrayList<MyObject> objects = new ArrayList<MyObject>&#40;&#41;;
    MyObject obj1 = new MyObject&#40;&#41;;
    obj1.setName&#40;"name1"&#41;;
    MyObject obj2 = new MyObject&#40;&#41;;
    obj1.setName&#40;"name2"&#41;;
    MyObject obj3 = new MyObject&#40;&#41;;
    obj1.setName&#40;"name3"&#41;;
    objects.add&#40;obj1&#41;;
    objects.add&#40;obj2&#41;;
    objects.add&#40;obj3&#41;;
    return objects;
    &#125;
    
    ...onSubmit&#40;Object object&#41;&#123;
    ArrayList<MyObject> objects = ArrayList<MyObject> object;
    //retrieve object
    &#125;
    and my JSP is

    Code:
    <c&#58;forEach var='obj' value='$&#123;command&#125;'>
              <spring&#58;bind path="obj.name">
                         <input type="text" name="name"
                              value="<c&#58;out value='$&#123;obj.name&#125;'/>"
                         </input>
              <spring&#58;bind
    </c&#58;forEach>
    the page displays but when i submit i get the following error:

    Code:
    Internal error
    
    ** Root cause is&#58; Neither Errors instance nor plain target object for bean name 'command' available as request attribute javax.servlet.jsp.JspTagException&#58; Neither Errors instance nor plain target object for bean name 'command' available as request attribute at org.springframework.web.servlet.tags.BindTag.doStartTagInternal&#40;BindTag.java&#58;118&#41; at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag&#40;RequestContextAwareTag.java&#58;68&#41; at _jsp._web_22dinf._jsp._process_0billing__jsp._jspService&#40;_process_0billing__jsp.java&#58;44&#41; at com.caucho.jsp.JavaPage.service&#40;JavaPage.java&#58;61&#41; at com.caucho.jsp.Page.pageservice&#40;Page.java&#58;569&#41; at com.caucho.server.dispatch.PageFilterChain.doFilter&#40;PageFilterChain.java&#58;149&#41; at com.caucho.server.webapp.DispatchFilterChain.doFilter&#40;DispatchFilterChain.java&#58;115&#41; at com.caucho.server.dispatch.ServletInvocation.service&#40;ServletInvocation.java&#58;208&#41; at com.caucho.server.webapp.RequestDispatcherImpl.forward&#40;RequestDispatcherImpl.java&#58;263&#41; at com.caucho.server.webapp.RequestDispatcherImpl.forward&#40;RequestDispatcherImpl.java&#58;101&#41; at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel&#40;InternalResourceView.java&#58;97&#41; at org.springframework.web.servlet.view.AbstractView.render&#40;AbstractView.java&#58;250&#41; at org.springframework.web.servlet.DispatcherServlet.render&#40;DispatcherServlet.java&#58;928&#41; at org.springframework.web.servlet.DispatcherServlet.doDispatch&#40;DispatcherServlet.java&#58;705&#41; at org.springframework.web.servlet.DispatcherServlet.doService&#40;DispatcherServlet.java&#58;625&#41; at org.springframework.web.servlet.FrameworkServlet.serviceWrapper&#40;FrameworkServlet.java&#58;386&#41; at org.springframework.web.servlet.FrameworkServlet.doPost&#40;FrameworkServlet.java&#58;355&#41; at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;152&#41; at javax.servlet.http.HttpServlet.service&#40;HttpServlet.java&#58;90&#41; at com.caucho.server.dispatch.ServletFilterChain.doFilter&#40;ServletFilterChain.java&#58;99&#41; at com.caucho.server.webapp.WebAppFilterChain.doFilter&#40;WebAppFilterChain.java&#58;163&#41; at com.caucho.server.dispatch.ServletInvocation.service&#40;ServletInvocation.java&#58;208&#41; at com.caucho.server.http.HttpRequest.handleRequest&#40;HttpRequest.java&#58;259&#41; at com.caucho.server.port.TcpConnection.run&#40;TcpConnection.java&#58;332&#41; at com.caucho.util.ThreadPool.runTasks&#40;ThreadPool.java&#58;457&#41; at com.caucho.util.ThreadPool.run&#40;ThreadPool.java&#58;398&#41; at java.lang.Thread.run&#40;Thread.java&#58;595&#41; Cookies&#58; JSESSIONID=&#91;wZz-5tSSLWhZJnR0&#93;
    how do you bind to an ArrayList object? thank you very much in advance

  2. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    It has nothing to do with binding.

    The problem is that the Spring bind requires a properly configured model to work. You usually get this if you go straight to the jsp instead of going via the controller.

    Do a search in this forum for "Neither Errors instance nor plain target". It is covered about a thousand times

  3. #3

    Default

    i did came from the controller.

    as you can see, the populated arraylist can be displayed from my view, my problem is i dont know how do i do a edit for the list of objects w/c is from an ArrayList. because although i can display the objects from the arraylist, i dont how to bind the values to a particular index in the arralist.

    help...

    thanks for any suggestions or ideas.

  4. #4
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Not according to the error message

    Not sure if this helps, but if the list is static, and you want to bind to properties of the objects in the list (not the list itself) then you can simply index them, i.e.:
    Code:
    command.list&#91;0&#93;.property
    HTH.

  5. #5

    Default

    i know i can do that but how to do it actually is the question.

    assuming this is my jsp:

    Code:
    //FROM controller
    List mylist = new ArrayList&#40;&#41;;
    return mylist;
    
    //JSP
    <c&#58;forEach var="item" items="&#123;command&#125;">
    
          <spring&#58;bind path="command???">
          <input type="text" name="to" 
          value="<c&#58;out value="items.to"/>"/>
         </spring&#58;bind>
    
          <spring&#58;bind path="command???">
          <input type="text" name="from" 
          value="<c&#58;out value="items.from"/>"/>
          </spring&#58;bind>
    
    </c&#58;forEach>
    how to do i put the index into the command.

    question, how can i bind to an arraylist?

    thanks...

  6. #6
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Assume:

    Code:
      class MyCommand &#123;
        public ArrayList getItems&#40;&#41; &#123;
          return new ArrayList&#40;&#123;new MyObject&#40;&#41;, new MyObject&#40;&#41;&#125;&#41;;
        &#125;
      &#125;
    then in jsp:

    Code:
      <c&#58;forEach items="$&#123;command.items&#125;" var="item" varStatus="counter">
        <spring&#58;bind path="$&#123;command.items&#91;counter.index&#93;.name&#125;">
          <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
        </spring&#58;bind>
      <c&#58;forEach>
    HTH.

  7. #7

    Default

    Code:
    <spring&#58;bind path="$&#123;command.items&#91;counter.index&#93;.name&#125;">
          <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
        </spring&#58;bind>
    can i really use ${} in the spring bind parameters, because it seems it will bind to a path named the value of ${command.items[counter.index].name}, and not the actual command object in index[] and setName() method.

    help anyone please...

  8. #8
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    Did you try it

    Code:
    $&#123;command.items&#91;counter.index&#93;.name&#125;
    is an expression that the jstl engine will convert into
    Code:
    command.items&#91;0&#93;.name
    .

    This when submitted to spring will be introspected (during the binding process) to the name property on the first element in the items array.

  9. #9

    Default

    my command object is of type ArrayList

    using this on my tag

    <spring:bind path="${command.items[counter.index].from}">

    produces:

    Code:
    java.lang.NullPointerException at org.springframework.web.servlet.support.BindStatus.&#40;BindStatus.java&#58;85&#41; at org.springframework.web.servlet.tags.BindTag.doStartTagInternal&#40;BindTag.java&#58;115&#41; at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag&#40;RequestContextAwareTag.java&#58;68&#41; at _jsp._web_22dinf._jsp._billing_220cycle__jsp._jspService&#40;_billing_220cycle__jsp.java&#58;128&#41; at com.caucho.jsp.JavaPage.service&#40;JavaPage.java&#58;75&#41; at

  10. #10
    Join Date
    Aug 2004
    Posts
    1,905

    Default

    OK, in the examples I gave you command object *contains* a list. Your command is actually a list so you will need to change the jsp:

    Code:
      <c&#58;forEach items="$&#123;command&#125;" var="item" varStatus="counter">
        <spring&#58;bind path="$&#123;command&#91;counter.index&#93;.name&#125;">
          <input type="text" name="$&#123;status.expression&#125;" value="$&#123;status.value&#125;"/>
        </spring&#58;bind>
      <c&#58;forEach>
    Note, you *will* get a NPE if you try and access an element that doesn't exist, i.e. command[5] if there are only 5 items.

    HTH.

Similar Threads

  1. Replies: 3
    Last Post: Jun 8th, 2010, 03:27 AM
  2. Replies: 1
    Last Post: Jun 24th, 2007, 10:58 AM
  3. Replies: 5
    Last Post: Aug 8th, 2006, 05:52 PM
  4. Replies: 0
    Last Post: Jan 6th, 2005, 08:19 AM
  5. Replies: 2
    Last Post: Aug 17th, 2004, 04:16 PM

Posting Permissions

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