Here is how I do it:

The jsp:
    <logic:iterate name="prodBeans" id="aBean" indexId="index">

         <%-- snip --%>

          <html:multibox name="prodBeans" property="selectedItems">
            <bean:write name="prodBeans" property="productCode"></bean:write>
          </html:multibox>
        </td>

       <%-- snip --%>

    </logic:iterate>

The bean 'aBean' is a form bean that implements Iterator:

private List products; // values set from db
public String getProductCode() { 
  return ((Product)products.get(currentIndex)).getCode(); 
}
private String[] selectedItems;  // initially set to new String [products.size()]
public String[] getSelectedItems() { return selectedItems; }
public void setSelectedItems(String[] x) { selectedItems = x; }
public boolean hasNext() { return currentIndex < products.size(); }
public Object next() { currentIndex++; return null; }

The idea is that as you iterate through the collection of products, the correct 
product code is displayed.  There will be a checkbox for each product code.  If the 
user selectes one or more items the value of the 'selectedItems' array will be set 
with those values.  Conversely, if the page is loaded with n values already in 
'selectedItems', the corresponding items will show as pre-selected.

I wasn't aware of the <nested> framework when I developed this page.  Things are  
*much* cleaner using it the framework.  Someday, I'll get around to cleaning it up.

Hope this helped

Sri


-----Original Message-----
From: Aidan Rourke [mailto:[EMAIL PROTECTED]] 
Sent: Sunday, August 11, 2002 1:41 PM
To: [EMAIL PROTECTED]
Subject: Dynamically Creating Checkboxes From Bean's Vector Attribute


Hello,

I am new to Struts so I apologize if this problem has
a simple answer but I couldn't find one in the
archive.

Let's say I have a bean that contains several
attributes, one of which is a Vector of Strings. This
Vector has been populated from a database table and I
want to present a checkbox for each item in the
Vector. On the HTML form the user will select the
items by checking the boxes they want. I want to do
some processing based on what checkbox items they
selected. 

Now my question: How do I get Struts to map the
selected checkboxes into the Vector of my bean? As I
understand it, Struts will use the get/set methods on
the bean to retrieve/populate the bean from the form.
But in this case I don't want to "set" the Vector, I
want to "add" to the Vector for each selected
checkbox.

Each of my checkboxes will, of course, have a separate
name but I definitely don't want to have a separate
attribute in my bean for each because this is
essentially look-up data that my end-user can modify
at will. We certainly can't be adding attributes to
our Java classes everytime the user adds a new entry
to the database.

I would assume that this is a common problem that
others have had. How have you solved it?

Aidan

__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to