or use something like;

Map<String, String> dropdownValues

then do a series of;

dropdownValues.put(companyId, companyName)

and then have

<s:select label="Select Company" name="company" list="dropDownValues" />

If you're not using a object-relation bridge of some kind (e.g. Hibernate), it's usually more efficient to get just the IDs and names as opposed to fetching all of the object properties just to display the id and name.

If you are using an ORB then it's usually frowned upon to go to the data directly, so the List solution makes the most sense.

----- Original Message ----- From: "Prashant Khanal" <[EMAIL PROTECTED]>
To: "Struts Users Mailing List" <user@struts.apache.org>
Sent: Thursday, February 14, 2008 4:59 AM
Subject: Re: Simple dropdown box population


Suppose you declare list of companies as:
private List<Company> companies = new ArrayList<Company>();

Now you got to have getter for this property as:

public List<Company> getCompanies(){
  return companies;
}

You can prepopulate your companies list implementing Preparable interface in your action. Upon implementing you will have to override prepare method as:
public void prepare() throws Exception {
      companies =//use data access methods or whatever you have to fetch
the companies
   }

you can use following tag to display the dropdown box:
<s:select label="Select Company"
       name="company" list="companies" listKey="companyId"
       listValue="companyName"/>

you can fetch the selected company as id declaring a property as the name of
the dropdown box as:
private Long company;
you got to have setter for this property as:
private void setCompany(Long id){
  company = id;
}

so the submitted value will be set in that property.

On 2/14/08, Jack Haynes <[EMAIL PROTECTED]> wrote:


Hi,

I have a list of objects in my action. Each object in the list represents
a Company.  I want to populate
my dropdown box, in my jsp, with company names.  When a companyName is
selected in the dropdown
box, the companyID (which corresponds to the companyName) will be sent
upon form submit.  For
example, if the user selects "companyA", in the combo box, "1" will be
submitted.

companyName-------->companyID
companyA-------------->1
companyB-------------->2

Any hints or sample code would be appreciated.

Thanks,
Jack
_________________________________________________________________




--
Thanks,
Prashant Khanal



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

Reply via email to