Link to home
Start Free TrialLog in
Avatar of vsaritha_9
vsaritha_9

asked on

Capturing Checkbox value using Spring Framework

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

code:
--------------------------------------------------------------------------------

 < input      type="checkbox" name="<c  ut value="${status.expression}"/>"         value="true" onClik="<c  ut value="${select}"/>.checked=true;"         <c:if test="${status.value}">checked</c:if>  />

--------------------------------------------------------------------------------

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
Avatar of jessegivy
jessegivy
Flag of United States of America image

Hi there,

Not familiar with the tool your using but shouldn't

test="${status.value}"

be

test=="${status.value}"

You're setting test to status.value in your if statement double equals tests for equality.  If test is a string then use the .equals()  method.

not clear on the onClick code, what does it do?:  onClik="<c  ut value="${select}"/>.checked=true;"
shouldn't the property name be onClick, not onClik  add a 'c' in there

Another note is that you don't have to keep track of wether the checkbox is checked or not, it's built in functionality.  A checkbox has a checked property that specifies the current selection value - you don't have to set it manually!!!  It knows when it is checked.
also the value is an integer not boolean, only used for arrays of checkboxes with same name so set it to "0" or remove it from your tag entirely.  Stick with the checked property and don't set it yourself unless there is some reason to.
Avatar of vsaritha_9
vsaritha_9

ASKER

jessgivy,

Thank you for your response,

Yes your right, the javascript function name should be onClick but not onclik. I posted same thing in javaranch also which does not allow you to use any javascript builtin functions to be used in post message. Hence I had to rename it.

onClick="<c:out value="${select}"/>.checked=true;">

This code is used to select another checkbox with name given to select varaible.

----------------------------------------------------------
Another note is that you don't have to keep track of wether the checkbox is checked or not, it's built in functionality.  A checkbox has a checked property that specifies the current selection value - you don't have to set it manually!!!  It knows when it is checked.
----------------------------------------------------------

I don't think with spring framework it does this. The value will not get submitted when checkbox is not set. and when I don't include "value=true" property i get JSP errors.

onClick="<c:out value="${select}"/>.checked=true;">

Still a little troubled by this code.  The onClick portion is an event procedure which belongs to the HTML tag not javascript.  This property is usually set to call a javascript function or execute some javascript code.  If you want to just execute code inline, ie set another checkboxes checked property to true then prefix the code with javascript: then the code you wish to execute. example:

onClick="javascript:window.close();

Also still not sure what ="<c:out value="${select}"/> is doing.  Is this outputing a string that has the name of another checkbox?

Also, you shouldn't be getting an error based on the value property.  If you don't specifiy a value the default is 'on'.  The value gets posted to the server when you submit the form only if the checkbox is checked.  otherwise no value is submitted and when you do request.getParameter("thecheckbox"); it returns null.  So when checking for the checkbox you should be able to see if it's null and if not that means it's checked.  However, it does seem that this spring framework is convaluting things a bit.  

What jsp errors are you getting without a value???
jessegivy,

I am abel to solve my problem. When a checkbox is submitted and if not checked then no value is returned in the request object. Hence onBind method in springController shall be overidded to check for null value and set respective flags for boolean parameter in command object.

<c:out va;ue="${select}"/> would return name of that check box. ideally, javascript: should be prefixed before setting that checkbox to true but even without this works.

When I don't sepcify value attribute, I get JSP errors saying "Cannot covert String to boolean"

Anyhow I could figure it out and able to make my program work.
Thank you very much for time and help

Saritha
Administrators,
How can i close this question?
I don't want to loose my points
ASKER CERTIFIED SOLUTION
Avatar of jessegivy
jessegivy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok jessegivy,

I will give you those points.

Thanks a lot for responding to my post.
Thank you Saritha,

Sorry I wasn't more help.  Where do you live?  I'm in Eugene Oregon

Hey Jessegivy,

Sorry I did not see your response.
I live in Pittsburgh PA.