Originally posted by peter parker:
I really enjoy using struts, but find it CHALLENGING sometimes to work within the framework. Here is a challenge I need help with:
I have a html:multibox with the days of the week (Sun, Mon...) The user may select any number of days and save to the database(mySQL)
When posting multiple checkboxes (html:multibox)to the Form, the Form requires me to use a String[] to accept multiple selections. In the Action, I loop through the String[] and save them to the database. However, when I'm creating my view I'm looping through a resultSet and storing Form beans in an ArrayList (for logic:iterate).
The ArrayList would not let me iterate through the collection with a String[] of days. So I had to create an ArrayList of days to compensate.
How do I set the user selected days (multibox values) in the view from logic:iterate ? Is there a better way to do this ?
Thanks,
PP
I'm a little confused over your implementation, particularly where your view is iterating over an ArrayList that contains Form beans. If you could elaborate on this it would be helpful.
Is the cruxt of your problem that you want to set the checkboxes correctly? Are the values for these checkboxes generated dynamically somehow or are they static? Let's assume for the moment they are static.
Your ActionForm should have a String[] that represents your checkboxes, let's call the property "selections" for now. So you have:
String[] selections
Your form bean should have the following methods:
public String[] getSelections()
public String getSelections(int index)
public void setSelections(String[] values)
public void setSelections(int index, String value)
additionally your reset() method should have the following:
selections = new String[0];
In your action, simply pass a String[] containing the values you want to your form's setSelections(String[]) method. When your
jsp renders, any checkbox whose value corresponds to an element in selections will be checked.
For example, in my action:
String[] newSelections = {"apple", "banana"};
myForm.setSelections(newSelections);
If my jsp has the following:
... the check boxes labeled apple and banana will be checked.
Have I come remotely close or am I just totaly missing what you are trying to say?