aspose file tools*
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
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts Form Beans & Multicheckboxes" Watch "Struts Form Beans & Multicheckboxes" New topic
Author

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.
Ravi
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16070
    
  21

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
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 = someAction.do.
Case 1. text in text1 ="sss"
check1=checked
OnSubmit would generate
SomeAction.do?text1=abc&check1=abc
Case 2. text in text1 =""
check1= not checked
OnSubmit would generate
SomeAction.do?text1=
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.
-SomeAction.do?check1=1&check1=2&check1=3
If only the first 2 were checked the URL would be
-SomeAction.do?check1=1&check1=2
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.
HTH
Ravi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Struts Form Beans & Multicheckboxes