Announcement Announcement Module
Collapse
No announcement yet.
how to Binding with checkbox Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to Binding with checkbox

    Hi
    iam facing problem binding checkbox value(y/n). if check box is unchecked bind n if check box is checked bind y.the following binding syntax iam using for this.
    <spring:bind path="configUserRoleNew.roleCreatePermission">
    <tr>
    <td><span class="styleScrName">
    <fmt:message key="NGV_USERROLE.ROLECREATE"/>
    </span></td>
    <td><span class="styleEntrFld">
    <input type="checkbox" name="roleCreatePermission" value="<c:out value="${status.value}"/>">
    </span></td>
    </tr>
    </spring:bind>


    please help me.
    thanks
    Rasheed :o :? 8) :lol: :lol: :x :P :?:[/list]

  • #2
    Whats the problem?

    Not sure what the problem is.

    However, you will need to ensure that you clear the boolean value before introspection because for some *stupid* reason browsers do not submit empty checkboxes, in otherwords you can tick a checkbox, but you cannot "untick" it

    Well known workarounds are to set the boolean values to false before submission. If you are using a recent version of webflow, then you can have a hidden element on your form called "_" + fieldName, i.e. "_roleCreatePermission".

    Comment


    • #3
      Spring MVC manages this using a second hidden parameter...

      First, change your roleCreatePermission to use the status.expression value to make life easier...

      <input type="checkbox" name="<c:out value="${status.expression}"/>" value="<c:out value="${status.value}"/>">

      <input type="hidden" name="_<c:out value="${status.value}"/>">

      The _propertyName tells Spring (somehow) that it is the unchecked state of a checkbox more or less.

      Have a look at this...
      http://opensource.atlassian.com/conf...ith+Spring+MVC
      Look at the "Working with Checkboxes"

      Comment


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

        This works for all properties, not just checkboxes

        Comment


        • #5
          Hi everyone,

          Sorry to jack your thread...but thought this might help others and didn't want to create another thread. It might be a simple problem but I can't seem to find an answer.

          Well..

          I got my multiple checkbox to work using an array like so


          Code:
          <display&#58;column title="Check Out">
          	<spring&#58;bind path="library.checkOutBox&#91;$&#123;counterOut&#125;&#93;">
          		<input type="hidden" name="_<c&#58;out value="$&#123;status.expression&#125;"/>">
          		<input type="checkbox" name="<c&#58;out value="$&#123;status.expression&#125;"/>" value="<c&#58;out value="$&#123;bookList.bookid&#125;"/>"
          		<c&#58;if test="$&#123;status.value == bookList.bookid&#125;">checked</c&#58;if>
          	<c&#58;if test="$&#123;bookList.copiesAvail==0&#125;" var="noneAvail">disabled</c&#58;if>/>
          	<c&#58;set var="counterOut" value="$&#123;counterOut+1&#125;"/>
          </spring&#58;bind>
          </display&#58;column>

          It's pretty much like the one at http://opensource.atlassian.com/conf...ith+Spring+MVC
          but slightly modified.

          The problem that i'm having is that in my Library class, i have to specify the size of the array like:
          Code:
          private String&#91;&#93; checkOutBox = new String&#91;60&#93;
          This is all fine and dandy if my Library was static but my Library will be adding and deleting books so is there a way to make it dynamic?


          Thanks

          Comment


          • #6
            Why does it need to be static? My understanding is that spring will recreate the array, not bind to an existing array.

            Comment


            • #7
              Originally posted by yatesco
              Why does it need to be static? My understanding is that spring will recreate the array, not bind to an existing array.


              That's what i thought too.

              But i get this error when i run the previous code without making
              Code:
              String&#91;&#93; checkOutBox = new String&#91;60&#93;;
              in my Library class.

              i get this when when i have String[] checkOutBox;
              *ERROR on webpage*
              org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'checkOutBox[0]' of bean class [org.library.model.Library]: Cannot access indexed value of property referenced in indexed property path 'checkOutBox[0]': returned null

              *ERROR from Eclipse*
              ERROR - RequestContextAwareTag.doStartTag( 78 ) | Exception in RequestContextAwareTag
              org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'checkOutBox[0]' of bean class [org.library.model.Library]: Cannot access indexed value of property referenced in indexed property path 'checkOutBox[0]': returned null

              It works fine and dandy when i put in 60 because my library contains less than 60 books so far...but anything less or not declaring the array size, it gives me the same error.

              Comment


              • #8
                Yeah OK, if you are binding on them *before* they exist it will throw an exception, but if you want to capture a bunch of checkboxes, then spring will create them.

                Set management really does seem to be harder then it needs to be with HTML

                Comment


                • #9
                  Originally posted by yatesco
                  Yeah OK, if you are binding on them *before* they exist it will throw an exception, but if you want to capture a bunch of checkboxes, then spring will create them.

                  Set management really does seem to be harder then it needs to be with HTML



                  There's no way around this huh? Oh well.

                  I thought there might be a way to make it work with an array to make it a bit easier but i guess not. Guess i'll use a different method.

                  Thanks for all the help.

                  Comment


                  • #10
                    If you use a map instead, it will be easier to see if a checkbox should be selected when re-rendering.

                    Comment


                    • #11
                      set the checkbox to read only instead of disabled

                      Comment

                      Working...
                      X