tags:

views:

135

answers:

2

Hi All,

i have the following code .

     <td> <html:checkbox name="userForm" property="isActive" /></td>

and in Form is have property called isActive of type char ,how would i get the checked value of it ,i am getting some symbols like 'o' if checked.

I am using userform.getIsActive() ,where i am going wrong .I want 'y' or 'n' values

+2  A: 

By default, when an HTML form containing a checkbox is submitted, if the checkbox is checked the value that gets sent is the string "on", and if it's not checked there is no field for that checkbox at all in the form data. So you test for the presence of a value, or the absense of one.

You can change what value gets sent when the checkbox is checked by using the value attribute (which works both on a standard HTML tag and on a Struts html:checkbox tag, according to the Struts docs). You can't, with standard HTML, specify that a value should be sent if the checkbox is not checked.

I'm a bit confused at your saying you're getting "o" back. The Struts docs say the corresponding property will be a boolean, not a String.

T.J. Crowder
But isgetActive is returning me just 'o' not on .
sarah
@sarah: That's very strange, the docs even reiterate the default value of `value`: http://struts.apache.org/1.x/struts-taglib/tlddoc/html/checkbox.html But shouldn't it be coming back as a boolean, not a String, anyway? That's what the docs say. (I'm not a Struts person. In fact, at first I missed that this related to Struts.)
T.J. Crowder
A: 

If the isActive property on the form is of type char, it can hold only one character. Perhaps that's why the "on" value is being truncated to "o" in your case.

In any case, I'd advise you to change the property datatype to boolean on the form. It's just easier and more logical that way. Whenever you want to use the value from the form, you can very easily do (form.getIsActive()?"y":"n") or something like that in your Java code, if you must get the value in that exact format. Hope this helps.

Tommi