?
Solved

Capturing Checkbox value using Spring Framework

Posted on 2005-04-12
10
Medium Priority
?
1,629 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:vsaritha_9
  • 5
  • 5
10 Comments
 
LVL 12

Expert Comment

by:jessegivy
ID: 13767610
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.
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 13767639
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.
0
 

Author Comment

by:vsaritha_9
ID: 13773318
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.

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:jessegivy
ID: 13774424
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???
0
 

Author Comment

by:vsaritha_9
ID: 13782219
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
0
 

Author Comment

by:vsaritha_9
ID: 13782291
Administrators,
How can i close this question?
I don't want to loose my points
0
 
LVL 12

Accepted Solution

by:
jessegivy earned 100 total points
ID: 13783809
oh common just give me the points.  I told you about the null value if not checked.  ; )
0
 

Author Comment

by:vsaritha_9
ID: 13784571
Ok jessegivy,

I will give you those points.

Thanks a lot for responding to my post.
0
 
LVL 12

Expert Comment

by:jessegivy
ID: 13785236
Thank you Saritha,

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

Author Comment

by:vsaritha_9
ID: 14943594

Hey Jessegivy,

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Tech giants such as Amazon and Google have sold Alexa and Echo to such an extent that they have become household names. And soon they are expected to be used by commoners in their homes, ordering takeout, picking out a song, answering trivia questio…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question