aspose file tools*
The moose likes Struts and the fly likes get the value of checkbox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "get the value of checkbox" Watch "get the value of checkbox" New topic
Author

get the value of checkbox

johny sil
Greenhorn

Joined: Jul 11, 2006
Posts: 13
Hi,

I have a checkbox in my jsp page,

When i click this ActionForm takes the value true otherwise it will take false,

How it is in struts,

Help me,

Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
When i click this ActionForm takes the value true otherwise it will take false,

That's exactly how it works in Struts.

One thing you do need to do, though, is override the reset() method in your ActionForm.

You must have your reset() method set any properties in your ActionForm controlled by a checkbox to false. You need to do this because if the checkbox is not checked, no data will be sent to the server for the property, and consequently the property's setter will not get called. By setting it to false in the reset method, you make sure that if it was true previously and the user unchecks the box, it will be false after the form is submitted.


Merrill
Consultant, Sima Solutions
johny sil
Greenhorn

Joined: Jul 11, 2006
Posts: 13
Thanks for reply,

If the checkboxes multiple with same name ,

then how to define in ActionForm and reset method;

help me,

Thanks in advance
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In this case, definie the property in your ActionForm as type String[]. Here's what your ActionForm would look like:

Also, make sure you use <html:multibox> instead of <html:checkbox>
[ July 20, 2006: Message edited by: Merrill Higginson ]
johny sil
Greenhorn

Joined: Jul 11, 2006
Posts: 13
Thanks for reply

in my jsp proerties are like this

itemcode itemname checkbox ADDMORE

when i click addmore one more row added in jsp using javascript,

itemcode itemname checkbox
itemcode itemname checkbox like this

checkbox define like this
html:checkbox property="percent" value="true"

my Action form

public String[] getPercent() {
return percent;
}

public void setPercent(String[] percent) {
this.percent=percent;

}

public String[] percent;

public void reset(ActionMapping mapping, HttpServletRequest request)
{
}

when the checkbox is checked it takes the value true, it works fine,

when it is not cheked i want to take false,

how it is,keep in mind it is array,

ex may be it will come true,false,false,true,false like this

Help me

Thanks in advance
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
This is a totally different situation. The solution I described before would work for a case where the value of the box is not boolean, and you just want a list of the values. It would not work for this situation

In your case, I think a JavaScript solution might be better. Try this:

Immediately before the form is submitted, use JavaScript to iterate over all the checkboxes, and for each one of them, perform the following logic:


By changing the value to false and checking the box, you are making sure that a value will be sent back to the server, and that the value will be false.
johny sil
Greenhorn

Joined: Jul 11, 2006
Posts: 13
Thank u very much Higgi,

This script code is work fine
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: get the value of checkbox
 
Similar Threads
Check Box
two values with one checkbox
dynamic Checkboxe value retrieval
Retaining value of checkbox when returning back from a servlet
How to get value from innerHTML