• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Capturing Checkbox value using spring framework

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question might seems to be very silly but I really need help..

Please suggest something
[ April 12, 2005: Message edited by: s penumudi ]
 
author
Posts: 422
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
s penumudi
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
s penumudi
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic