Announcement Announcement Module
Collapse
No announcement yet.
binding to arraylist of objects in my form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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

    Comment


    • #3
      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.

      Comment


      • #4
        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.

        Comment


        • #5
          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...

          Comment


          • #6
            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.

            Comment


            • #7
              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...

              Comment


              • #8
                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.

                Comment


                • #9
                  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

                  Comment


                  • #10
                    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.

                    Comment


                    • #11
                      Code:
                          	<spring&#58;bind path="$&#123;command&#91;counter.index&#93;.from&#125;">
                          	<input type="text" name="$&#123;status.expression&#125;" size="13" value="$&#123;status.value&#125;">
                          	</spring&#58;bind>
                      actually i did that alread but was getting still getting errors

                      Code:
                      javax.servlet.jsp.JspTagException&#58; Neither Errors instance nor plain target object for bean name 'date1' 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._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 com.caucho.jsp.Page.pageservice&#40;Page.java&#58;571&#41; at com.caucho.server.dispatch.PageFilterChain.doFilter&#40;PageFilterChain.java&#58;155&#41; at com.caucho.server.webapp.DispatchFilterChain.doFilter&#40;DispatchFilterChain.java&#58;129&#41; at
                      as you can see path="${command[counter.index].from}" resolve to the current value of from which is 'date1'.

                      i am really lost now....

                      Comment


                      • #12
                        Sorry, my bad Not enough coffee

                        You want the name to be rendered to

                        Code:
                        "command&#91;1&#93;.from"
                        so your jsp wants to be

                        Code:
                        <spring&#58;bind path="command&#91;$&#123;counter.index&#125;&#93;.from"

                        Comment


                        • #13
                          i did that but it take me back to one of my original exception:

                          Code:
                          javax.servlet.jsp.JspTagException&#58; Neither Errors instance nor plain target object for bean name 'command&#91;0&#93;' 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
                          may someone has a working snippet or small working demo just to show ArrayList binding. any help still be highly appreciated.

                          thanks a lot everyone.

                          Comment


                          • #14
                            As far as I know the form backing object cannot be a collection or array, create a new command class that wraps the list, and bind to that instead.

                            e.g.

                            Code:
                                public class Command &#123;
                                    private List<MyObject> list = new ArrayList<MyObject>&#40;&#41;;
                                    public List<MyObject> getList&#40;&#41; &#123;
                                        return list;
                                    &#125;
                                &#125;
                            <spring:bind path="command.list[${counter.index}].from"...

                            Comment


                            • #15
                              anyone from the spring developer camp reading this concur? :lol:

                              Comment

                              Working...
                              X