• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

multibox , resultSet, logic:iterate

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic