Announcement Announcement Module
Collapse
No announcement yet.
Using an array with form:input and c:forEach Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using an array with form:input and c:forEach

    Hi all,

    I have searched the forum on this and can't find an answer, forgive me if there is one.

    I have a form with a Double[] amount which I want to display and submit from a jsp.

    If I use amout[i] like below:

    Code:
    <c:forEach items="${form.amount}" var="amnt" varStatus="counter">
    <form:input cssClass="decimal" path="form.amount[${counter.index}]" />
    </c:forEach>
    This displays the values correctly, but when edit a value and submit I get the following:

    2010-03-10 13:43:14,187 ERROR [HTTPThreadGroup-54] uk.gov.lsc.maat.web.exception.WebExceptionResolver - ERROR -
    org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'amount[0]' of bean class [uk.co.home-grown.web.spring.form.ReviewForm]: Cannot access indexed value in property referenced in indexed property path 'amount[0]': returned null
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:681)
    at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:651)

    If I change the jsp not to have array index as follows:

    Code:
    <c:forEach items="${form.amount}" var="amnt" varStatus="counter">
    <form:input cssClass="decimal" path="form.amount" />
    </c:forEach>
    This submits correctly and saves, but in each input field it displays the complete array like {1234.0, 4321.0}

    I have half of this working one way and the other half another way, how do I get it working correctly?

    Any help on this would be greatly appreciated as I am tearing my hair out with this.

    Thanks in advance.

    Phil Maskell
    Last edited by maskellp; Mar 10th, 2010, 08:18 AM.

  • #2
    Read the stacktrace...

    You aren't using the SAME object instance again for binding, you are using a new one, with an empty array. The element at index 0 is null and spring cannot bind to null values, hence your exception.

    Either retrieve the object before binding @ModelAttribute or store it in the session @SessionAttribute, both approaches are explained in the reference guide.

    Comment


    • #3
      Marten,

      Thanks for getting back to me so quickly on this.

      I have one question about what you have said, if I am binding a different object how does it work with no [index]? Also none array fields on the form work fine, surely if their were an issue with the object being bound these wouldn't work too?

      Thanks

      Phil

      Comment


      • #4
        Normal parameters work (primitives, strings) but not collections and arrays.

        Comment


        • #5
          Yes, normal parameters like String, Date, Double are fine.

          The array works if I remove the [index] from the form:input on the jsp, but it displays the complete array in each input box in the loop as {1234.0, 4321.0}
          It is as though Spring sees the array[0] and using the bind mechanism tries to call the getter/setter for this, which of course doesn't exist.

          If it didn't work at all it would make more sense.

          Thanks for your help

          Phil

          Comment


          • #6
            As this doesn't make any sense I have taken an example project from:

            http://maestric.com/doc/java/spring/form_validation

            I added a Double[] like the code I am working on, added this to the page and the submit code. It works fine.

            I can not see any differences!!!

            Any ideas of what could be causing this would be greatly appreciated as it is getting to the point were I may have to abandon this approach and look for a different solution.

            Thanks in advance.

            Phil

            Comment


            • #7
              If you want to bind an array or collection the array has to be initialized if it isn't it isn't going to work...

              Comment


              • #8
                Hi,

                Thanks for all the help, I have solved the issue.

                Turns out we currently have 2 controllers for the page, a select controller with the formBackingObject method and a save controller with the onSubmit.

                If they are merged like in the simple example it all works.

                Marten you probably were correct as the formBackingObject was in a different contorller the save controllers object would have been null.

                Thanks

                Phil

                Comment

                Working...
                X