You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Milan Milanovic <mi...@yahoo.com> on 2008/05/12 16:34:09 UTC

[Struts 2] Combo box question

Hi,
I have a simple action class, for example like this:
public class comboboxTag extends ActionSupport{
  
  private List<Fruit> fruits;
    private FruitService service;
public String execute()throws Exception{
    fruits = service.getFruits();
    return SUCCESS;

  }
}
fruits field should be connected to combobox in my jsp page. I have two questions, the first is list
for combobox should be only List<String> type or it can be List<Fruit> ?
The second question is that I don't want every time to call my service to repopulate fruits field (and combo box),
when one work in this page, I want to do this just once (in first start of the page) ? I ask this because I will have
5 combo boxes on a master/detail jsp page.
--
Thx, Milan Milanovic


      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ

Re: [Struts 2] Combo box question

Posted by Laurie Harper <la...@holoweb.net>.
Milan Milanovic wrote:
> Hi,
> I have a simple action class, for example like this:
> public class comboboxTag extends ActionSupport{
>   
>   private List<Fruit> fruits;
>     private FruitService service;
> public String execute()throws Exception{
>     fruits = service.getFruits();
>     return SUCCESS;
> 
>   }
> }
> fruits field should be connected to combobox in my jsp page. I have two questions, the first is list
> for combobox should be only List<String> type or it can be List<Fruit> ?

It can be List<Fruit>. Use the listKey/listValue attributes of s:select 
to control which properties of Fruit are used in populating the select 
options.

> The second question is that I don't want every time to call my service to repopulate fruits field (and combo box),
> when one work in this page, I want to do this just once (in first start of the page) ? I ask this because I will have
> 5 combo boxes on a master/detail jsp page.

You can achieve that in any number of ways, but essentially you'll need 
to 'remember' the answer from your service method, i.e. you need to 
cache that data somewhere. Whether you choose to do that in static 
fields in the action class itself, by putting the data into session or 
application scope, or by handling the caching in the service or database 
layer will depend on your particular requirements. Your strategy will 
depend on how often the data changes (if ever), how critical it is for 
your select lists to be up-to-date when the underlying data set has 
changed, how the data is updated, etc.

That's a long way of saying that you can avoid calling the service every 
time, but it's entirely up to you how. My recommendation would be, don't 
worry about it; just call the service as needed. If it becomes a 
performance issue later on, address it then. Don't optimize until you 
have a demonstrable, quantifiable need to do so.

L.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org