Hello Tharwat,

     Thank you very much for your help.

cheers,
Amar..

-----Original Message-----
From: Tharwat Abdul-Malik [mailto:[EMAIL PROTECTED]]
Sent: Saturday, April 14, 2001 3:19 PM
To: [EMAIL PROTECTED]
Subject: Re: select option


I'm a newbie to struts. In fact I just downloaded it yesterday (Friday) and
thought I'd get involved. I built a very simple application last night. But
I think the struts tags already selects the correct item. I use something
like the following to display the items in a list.

<html:select property="state">
    <html:options collection="stateList"  property="code"
labelProperty="description"/>
</html:select>

// Code Bean -- Basically encapsulates a name/value pair.
public class TypeCodeBean
{
    protected String code = null;
    protected String description = null;

    public TypeCodeBean(String code, String description)
    {
        this.code = code;
        this.description = description;
    }

    public String getCode()
    {
        return (code);
    }

    public String getDescription()
    {
        return (description);
    }
}

// TypeCode List -- Extends the FastArrayList (faster for reads).
public class StateList extends org.apache.struts.util.FastArrayList
{
    public StateList()
    {
        add(new TypeCodeBean("GA", "Georgia"));            // Good to have
this read from a database.
        add(new TypeCodeBean("FL", "Florida"));
    }
}

I use a tag that encapsulates all of the code for creating the list and
stuffing it into the request object. So at the top of my jsp I would have
something like the following:
        <app:typecode id="stateList" />

Which would instantiate the StateList object and insert in the request. It
does something like the following:
    if ("stateList".equals(id))
    {
        StateList stateList = new StateList();            // Build list
        request.setAttribute("stateList", stateList);    // Stuff in request
    }

You could just a well put the code in your jsp as a scriptlet...

As mentioned I'm new to struts so this is basically a hack. Over the next
day or so, I will rework the code as follows:

1. Create a TypeCode servlet that's loaded at startup.
2. The TypeCode servlet will implement the singleton pattern, it will
instantiate all of the code lists needed by the application (state code,
country code, etc). And store them in a Hash.
3. Modify the TypeCodeTag to use the singleton to get a list from the Hash
and then store it in the request object. This will prevent having to
instantiate the code lists for each request.
4. Modify the TypeCode class to get it's data from a database. This will
prevent having to hard code the code values. Useful for list that change
(for instance a list of referral codes).

Your are welcome to have any of the code, if you want it. Just shoot me an
email. I hope this helps.

Take care,
Tharwat





----- Original Message -----
From: "Nanduri, Amarnath" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, April 13, 2001 1:37 PM
Subject: RE: select option


> i have figured it out. Thankyou. Another quick question.
>
> I am updating my profile.
> I have bean A which contains the state i am in. I have an other bean which
> has the list of all states . Call it Bean B
>
> When the user initially comes to this page i have to show the state as
> "Virginia" in a select list.
> The user can modify the state to say "alabama"
> When the user submits information i want the 'state' variable to be set to
> 'alabama' instead of 'virginia'.
>
>
>   Is this feasible ?  Will this work ?
>
>    <html:select name="aBean" property="state" size="1" >
>         <html:options name="bBean" labelName="states"/>
>    </html:select>
>
>  class A  //session scope bean
>  {
>   private  String  state  = "virginia" ;
>
>   public String  getState()
>   {
>     return  state ;
>   }
>
>   public  void  setState(String value)
>   {
>     state  =  value  ;
>   }
>
>  }
>
>
>   class B   // application scope bean
>   {
>     private  ArrayList  states  = new ArrayList() ;
>
>     public B()
>     {
>       states.add("alabama");
>       states.add("Missisippi");
>       states.add("Tenesse");
>       states.add("virginia" ) ;
>       states.add("Vermount" ;
>
>     }
>
>
>     public  Iterator  getStates()
>     {
>        return  states.iterator() ;
>     }
>
>   }
>
>
>
>
>
> -----Original Message-----
> From: Nanduri, Amarnath [mailto:[EMAIL PROTECTED]]
> Sent: Friday, April 13, 2001 1:22 PM
> To: '[EMAIL PROTECTED]'
> Subject: select option
>
>
>
> Hi all,
>
>   How do i implement the <SELECT> option using struts. I want to be able
to
> show a drop-down list from which the user can select what he/she wants.
The
> struts-test war file comes with a <html:select> example. this example
shows
> the whole list instead of in a drop down list. I need a drop down list.
any
> suggestions are appreciated. Thanks.
>
> cheers,
> Amar..
>

Reply via email to