This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts Form Beans & Multicheckboxes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Form Beans & Multicheckboxes" Watch "Struts Form Beans & Multicheckboxes" New topic

Struts Form Beans & Multicheckboxes

Joe Wolf
Ranch Hand

Joined: Jul 05, 2001
Posts: 42
Hi all, I have multiple checkboxes with the same name but with different values in a JSP page that I want to initially display as checked. I can do this using a javascript function but I would like to be able to do this through Struts. Namely, I'd like the Struts Form Bean to contain something like this:
private String[] myCheckboxes = null;
public String[] getMyCheckboxes()
public void setMyCheckboxes(String[] myCheckboxes)
so that whenever a combination of checkboxes are checked, their values are added to the String array. And, by setting myCheckboxes before displaying the page, Struts will check the checkboxes for me. Does anyone know a way to set up struts-config.xml to do this, and if this is the best approach when dealing with Struts and multiple checkboxes?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

moved from servlets -> Frameworks
Ravi Veera
Ranch Hand

Joined: Jun 23, 2001
Posts: 127
You need not make any changes to the strutsconfig.xml file.
This is what you need in your JSP File

Struts will then populate your string array with the checked values.
For struts to set certain values to true you need to initialize the String array with the appropriate values.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17269

I've not worked with that feature, but I'd have thought it would be like the other controls - you preset the default value in the Form Bean's reset method with an option to override it in the Action code before it goes out.
Also, if the fields have the same name, I'd consider using Strut's ability to use indexing on names - an amenity straight HTML doesn't offer.

An IDE is no substitute for an Intelligent Developer.
Ravi Veera
Ranch Hand

Joined: Jun 23, 2001
Posts: 127
You can treat it like other controls and preset the values in the reset method and override it in the Action method. But the key is in how HTML treats checkboxes.
The way HTML would process checkboxes is that it sends in the parameter and a value *only* if it is checked.
E.g say you had a form with
textbox name=text1
checkbox name=check1 value="abc".
OnSubmit Button =
Case 1. text in text1 ="sss"
OnSubmit would generate
Case 2. text in text1 =""
check1= not checked
OnSubmit would generate
Note that it dosen't send in the check1 parameter.
You could have multiple checkboxes in HTML having the same name. It would just repeat the values in the URL.
Say you had 3 checkboxes all named check1 with value=1,2,3. Assume all 3 are checked
On form submit you would have this URL generated.
If only the first 2 were checked the URL would be
and so on.
Struts just collects these and puts it into an array. On the view sides it compares the values in the array to the values of the boxes and checks the appropriate boxes.
subject: Struts Form Beans & Multicheckboxes
jQuery in Action, 3rd edition