wood burning stoves 2.0*
The moose likes Other Application Frameworks and the fly likes Capturing Checkbox value using spring framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Capturing Checkbox value using spring framework" Watch "Capturing Checkbox value using spring framework" New topic
Author

Capturing Checkbox value using spring framework

s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
Hello All,

In one of my UI, there is a checkbox. Based on the selection on the checkbox a value is stored in the database. "Y" if checked else "N".



If I do not specify value="true", I am getting bind errors, saying it cannot convert String to Boolean (The data type of the command object property corresponding to this field is of boolean datatype.

When I give value="true", irrespective of checkbox being selected or not, the value set is always true.

I also tried to set the value as "true" when selected and "false" when unselected using javascript onSelect event. But this seems not working. I always see value as true.

Can some one please help me in solving my problem. I am really kind of tired trying various options. I am not sure what is different with spring framework in this case. When I set false from javascript, false should be set to the command property aswell. But I am not sure Why it functioning differently.

Your help will be greatly greatly appreciated.

Thank you
[ April 12, 2005: Message edited by: s penumudi ]
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
My question might seems to be very silly but I really need help..

Please suggest something
[ April 12, 2005: Message edited by: s penumudi ]
Craig Walls
author
Ranch Hand

Joined: Sep 19, 2003
Posts: 301
I'm not entirely sure what your problem is, but here are some things that caught my eye...

The first thing to keep in mind is that the browser will not send the field in the request at all if the checkbox isn't checked. That means that if a checkbox named "foobar" is not checked, then the browser will not send foobar's value at all. In other words, foobar will either be "true" or not sent.

Originally posted by s penumudi:
If I do not specify value="true", I am getting bind errors, saying it cannot convert String to Boolean (The data type of the command object property corresponding to this field is of boolean datatype.


I tried recreating your problem in a simple app. If my command object has a boolean property, then the value is correctly set to either true or false, depending on whether the checkbox is set. This makes sense because if the checkbox is checked, then the value gets mapped to a boolean true. If the checkbox isn't checked, then the value isn't sent and so the default value (false) is kept on the command.

If I used a Boolean property, then I'd either get true for a checked checkbox or null for an unchecked checkbox.

Originally posted by s penumudi:

When I give value="true", irrespective of checkbox being selected or not, the value set is always true.


I couldn't recreate this behavior exactly, but I did notice that you have an onClick event that checks the checkbox anytime that it is clicked. This means that once you've checked the checkbox, it is virtually impossible to uncheck it. Perhaps that was the cause of it always being true?

Originally posted by s penumudi:

I also tried to set the value as "true" when selected and "false" when unselected using javascript onSelect event. But this seems not working. I always see value as true.


Again, if the checkbox isn't checked, the value won't be sent to the server at all. So, even if you explicitly set the value to "false" when the checkbox is unchecked, the value won't be sent.

I know that there's not a firm answer to your problem here, but perhaps I've given you a few clues that will help you solve it.


Spring in Action - Unleash POJO power in your applications!
Modular Java - Discover the secret weapon to modularity on the Java platform!
XDoclet in Action - Your complete guide to code generation with XDoclet.
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
Thank you very much Craig for replying to my post.

All I want is the value to be set either true or false based on the selection of the checkbox.



Error
Failed to convert property value of type [java.lang.String] to required type [boolean] for property 'dispo5.serviceAccountDeduction'; nested exception is java.lang.IllegalArgumentException: on;Failed to convert property value of type [java.lang.String] to required type [boolean] for property 'dispo8.serviceAccountDeduction'

serviceAccountDeduction is of type boolean. If I specify either value="true" or value="false" in the input then I don't get this error but, the value always being submitted is the value that I have specified.

I even tried using value="" but even it did not work. Can you please suggest me what I should be doing. Where I am making a huge mistake.

OnClik even on the input is to check another checknox in the same form. So, if one can check it then they also uncheck it.

Thank you
[ April 12, 2005: Message edited by: s penumudi ]
Ken Krebs
Ranch Hand

Joined: Nov 27, 2002
Posts: 451
When using SpringMVC and I have a checkbox on a form, I manually bind it by overriding onBindAndValidate() because as Craig points out, there will be nothing to bind automatically if the checkbox is not checked. Here are some snippets from some off my code to look at.

In my JSP:


In my SimpleFormController derivative:


Hope that helps.
[ April 12, 2005: Message edited by: Ken Krebs ]

kktec<br />SCJP, SCWCD, SCJD<br />"What we observe is not nature itself, but nature exposed to our method of questioning." - Werner Heisenberg
s penumudi
Ranch Hand

Joined: Nov 17, 2004
Posts: 113
Thank you verymuch Ken krebs,
It worked.

Can you suggest me good book for Spring?.
I could not decide which one to buy between Pro Spring and Spring in Action.

I could not see more books in stores.

I would appreciate if you could send me any links you have on Spring framework.

Thank you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Capturing Checkbox value using spring framework
 
Similar Threads
Display tag and Struts multibox tag
Html:checkbox
problem with EL inside c:if test attribute="${x}"
JSTL and c:if problem
Checking Checkbox values in indexed form