Announcement Announcement Module
Collapse
No announcement yet.
newbie: binding problems / indexed properties / checkbox Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • newbie: binding problems / indexed properties / checkbox

    hello,

    I'm very new to spring and webflow and stumble over my first problem.
    I hope you have a minute or two and can help me :-)

    My form object has a collection filled with CodedText items

    pseudo code:
    class CodedText {
    code
    text
    }

    At the (JSP) view I display this list:

    <c:forEach var="type" items="${order.types}" varStatus="s">
    <tr><td>
    <spring:bind path="order.types[${s.index}].code">
    <input type="checkbox"
    name="${status.expression}"
    value="${status.value}"/>
    </spring:bind>
    </td>
    <td>
    <c:out value="${type.text}"/> (<c:out value="${type.code}"/>)
    </td>
    </tr>
    </c:forEach>

    This list is prefixed with a checkbox. I want to collect to users choice, but can't get the binding to work proberly :cry:

    Questions:
    - How should I bind the users choice ?
    - How could I redisplay the page and show the current users choice ?

    TIA, leif

  • #2
    another day, another way :lol:

    I fixed my problem :!:

    I created an object that 'holds' my object (item) and has an isSelected flag.

    Next, I had the problem that unchecked checkboxes are not submitted by HTML. To solve this I found a thread and a good comment from user yatesco

    WebDataBinder sets all propertys to empty for which there is a parameter called "_[propertyName]".

    This works for all properties, not just checkboxes
    So here is my JSP code:

    Code:
    <c&#58;forEach var="type" items="$&#123;order.test&#125;" varStatus="s">
    <tr>
        <td>
        <spring&#58;bind path="order.test&#91;$&#123;s.index&#125;&#93;.selected">
             <input type="checkbox" name="$&#123;status.expression&#125;" value="true" <c&#58;if test="$&#123;status.value&#125;">checked="checked"</c&#58;if> />
             <input type="hidden" name="_<c&#58;out value="$&#123;status.expression&#125;"/>"> 
        </spring&#58;bind>
        </td>
        <td><c&#58;out value="$&#123;type.item.text&#125;"/> &#40;<c&#58;out value="$&#123;type.item.code&#125;"/>&#41;</td>
    </tr>
    </c&#58;forEach>

    If you have better ideas, let me know.
    Thanks in advance, Leif

    Comment


    • #3
      For a java.util.Set

      Hi, I am having the same issue.

      I guess your solution would work fine if your FormBackingObject has a List (or any other indexed collection...) as the backing property, what can you do if you have a java.util.Set? Or is that actually possible at all (since the Set is not indexed)?

      Any workarounds?

      Code:
      Invalid property 'serviceLines[0]' of bean class [domain.ProjectCredential]: Cannot get element with index 0 from Set of size 0, accessed using property path 'serviceLines[0]'
      Nicolas

      Comment


      • #4
        Yes you can use a set apparantely. Do a search for http://www.springframework.org/docs/...ionEditor.html
        on these forums. A couple of people have asked about this in the past few weeks.

        Comment


        • #5
          Hi Yatesco,

          I guess I was one of them :-). As you correctly pointed out, the submission works fine, however, if any validation happens, it doesn't "remember" the checked-checkboxes... (I was actually going through this testing to answer your question on this post: http://forum.springframework.org/sho...=20503&page=3).

          The code that is posted here would definitely not work with a Set as it is not indexed.

          Code:
          <c:forEach var="type" items="${order.test}" varStatus="s">
          <tr>
              <td>
              <spring:bind path="order.test[${s.index}].selected">
                   <input type="checkbox" name="${status.expression}" value="true" <c:if test="${status.value}">checked="checked"</c:if> />
                   <input type="hidden" name="_<c:out value="${status.expression}"/>"> 
              </spring:bind>
              </td>
              <td><c:out value="${type.item.text}"/> (<c:out value="${type.item.code}"/>)</td>
          </tr>
          </c:forEach>
          I foolishly used Set's since they're widely used in the Hibernate examples. I was wondering whether somebody had actually done it and if it was possible, since there's no index on a Set.

          Comment


          • #6
            Answered in http://forum.springframework.org/sho...ed=1#post48854.

            Comment


            • #7
              Nicolas,

              Maybe, you could try this code, and use your CustomCollectionEditor. This assume that your PropertyEditor will handle correctly a serie of inputs in your form named identicaly. It also assume that the test myArchiDomain == domain is correctly evaluated by JSTL when the objects are not primitives.
              This is far better to use List, as you will be able to use ProperyEditor for elements of your list, which is not the case here.

              Code:
              <spring:bind path="project.architectureDomains">
              <c:forEach items="${domains}" var="domain" varStatus="loopStatus">
                     	   <input type="hidden" name="_<c:out value="${status.expression}"/>">
              	   <input type="checkbox" name="<c:out value="${status.expression}"/>" value="<c:out value='${domain.id}' />"
                   <c:forEach items="${project.architectureDomains}" var="myArchiDomain">  
                          <c:if test="${myArchiDomain == domain}">checked</c:if>
                   </c:forEach>
                   <c:out value="${domain.name}" /><br>
              </c:forEach>
              </spring:bind>

              Comment


              • #8
                Binding Multiple value with CheckBox

                Hi all,

                I got some information about binding list values when using Checkbox.
                But in the below mentioned code, can please anyone tell me,
                how to create checked property when binding the list with spring

                <c:forEach items="${myDataModel.drivers}" var="driver" varStatus="loopStatus">

                <spring:bind path="myDataModel.drivers[${loopStatus.index}].checked">
                <input class="checkbox" type="checkbox"
                name ="<c:out values = "_${status.expression}"/>"
                value = "true" <c:if test="${status.value}">CHECKED</c:if> >
                </spring:bind>
                </c:forEach>

                Please help me
                Thanks in advance
                Shilpa

                Comment


                • #9
                  Binding multiple value with checkbox

                  Originally posted by strug
                  hello,

                  I'm very new to spring and webflow and stumble over my first problem.
                  I hope you have a minute or two and can help me :-)

                  My form object has a collection filled with CodedText items

                  pseudo code:
                  class CodedText {
                  code
                  text
                  }

                  At the (JSP) view I display this list:

                  <c:forEach var="type" items="${order.types}" varStatus="s">
                  <tr><td>
                  <spring:bind path="order.types[${s.index}].code">
                  <input type="checkbox"
                  name="${status.expression}"
                  value="${status.value}"/>
                  </spring:bind>
                  </td>
                  <td>
                  <c:out value="${type.text}"/> (<c:out value="${type.code}"/>)
                  </td>
                  </tr>
                  </c:forEach>

                  This list is prefixed with a checkbox. I want to collect to users choice, but can't get the binding to work proberly :cry:

                  Questions:
                  - How should I bind the users choice ?
                  - How could I redisplay the page and show the current users choice ?

                  TIA, leif
                  In the above code, can you please tell me where i need to create code code when binding list object.

                  Thanks
                  Shilpa

                  Comment

                  Working...
                  X