This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes multibox , resultSet, logic:iterate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "multibox , resultSet, logic:iterate" Watch "multibox , resultSet, logic:iterate" New topic
Author

multibox , resultSet, logic:iterate

peter parker
Ranch Hand

Joined: Jul 11, 2001
Posts: 69
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
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
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?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
So, is "Peter Parker" actually your real name? FYI we do have a policy against "obviously fictitious" names. If it's not actually your name, please edit your display name to something a little more plausible. Thanks.


"I'm not back." - Bill Harding, Twister
Kevin Lwinmoe
Greenhorn

Joined: Jan 06, 2005
Posts: 1
Hi Jason,

Thanks for your tips...It sure help me in understanding about multibox better than Struts Documentation page from apache website..

Would you also show me sample code of generating checkboxes dynamically...

Sticking it with fruits example...

Let's say we have three table: Items, Fruits, Fruits_Items

Items table and Fruits table has many to many relationships.
Thus, Fruits_Items is used to store foreign key of Items and Fruits

So we would use Fruits_Items table to find out which items are marked with Fruits_id. Fruits table has apple, banana, grape, etc.

instead of this
String[] newSelections = {"apple", "banana"};
myForm.setSelections(newSelections);

how would you get the apple and banana into new Selections.

FYI, I am using hibernate and struts 1.1

P.S. This is not a database question. I am simply mentioned about database schema so that everyone know how I will be retrieving data from database.


K~
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multibox , resultSet, logic:iterate
 
Similar Threads
multibox - nullPointerException
multibox functionality in struts
howto settle multibox problem ?
Iterator and multibox
How to get unchecked multibox