You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Spencer Smith <sp...@newdestiny.net> on 2001/03/03 00:46:26 UTC

Has anyone sucessfully implemented the multiple="true" using struts?  
<html:select multiple="true" property="fieldName"/ >

I read the Struts docs and it says to make the getter and setter an Array

ex. 

protected String[] fieldName;

The problem is that I don't know the size of the Array because it is dynamically being created through fields in the DB.

Any ideas?






Re: Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Spencer Smith wrote:

>  Has anyone sucessfully implemented the multiple="true" using struts?
>
> <html:select multiple="true" property="fieldName"/ >
>
> I read the Struts docs and it says to make the getter and setter an
> Array
>
> ex.
>
> protected String[] fieldName;
>
> The problem is that I don't know the size of the Array because it is
> dynamically being created through fields in the DB.
>
> Any ideas?
>

The struts-test.war application includes a page that exercises the
"multiple" option.

Having an array whose size you do not know ahead of time is no problem,
because Java lets you create an array of whatever size you want.  So,
your bean would have the following method signatures:

    public String[] getFieldName();
    public void setFieldName(String[] fieldName);

In order to initialize this property from a database, the following
technique is commonly implemented:

    ArrayList list = new ArrayList();
    ResultSet rs = conn.executeQuery("... select statement ...");
    while (rs.next()) {
        list.add(rs.getString("value"));
    }
    String values[] = new String[list.size()];
    values = list.toArray(values);
    bean.setFieldName(values);

In other words, first you accumulate the required values into an
ArrayList (which has no predefined size).  Then, once you know how many
elements are needed, convert this to a String array and set the bean
property.

Craig McClanahan