Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: how to Binding with checkbox

  1. #1
    Join Date
    Apr 2005
    Posts
    6

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    1,905

    Default 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".

  3. #3
    Join Date
    Oct 2004
    Posts
    207

    Default

    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"

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

    Default

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

    This works for all properties, not just checkboxes

  5. #5
    Join Date
    Jul 2005
    Posts
    12

    Default

    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

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

    Default

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

  7. #7
    Join Date
    Jul 2005
    Posts
    12

    Default

    Quote 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.

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

    Default

    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

  9. #9
    Join Date
    Jul 2005
    Posts
    12

    Default

    Quote 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.

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

    Default

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

Similar Threads

  1. Replies: 8
    Last Post: Jul 27th, 2006, 02:47 AM
  2. Replies: 7
    Last Post: Sep 13th, 2005, 01:45 AM
  3. Replies: 4
    Last Post: May 13th, 2005, 03:33 AM
  4. binding to checkbox
    By mia in forum Web
    Replies: 3
    Last Post: Apr 25th, 2005, 08:33 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
  •