• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts Form Beans & Multicheckboxes

 
Joe Wolf
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
moved from servlets -> Frameworks
 
Ravi Veera
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18302
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ravi Veera
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic