tags:

views:

21

answers:

1

In a Screen to be rendered by a JSF Implementation, I had to show a static drop down or list box (which means the values are not changing ), So I decided to use a list of select Items and in the getter of the List , I am populating all the select Items as this

List.add(new SelectItem(VALUE,TEXT)) and so on.. 

If I used this way - What are the pitfalls? I made this List static since this will be common for all the Request Scoped Beans the JSF Implementation creates. Is this okay to do?

Sometimes , the items are added twice if multiple requests are fired.Is there an Application Scope ? or Whats the standard way of doing these stuff?

I'm also looking into API.Sometimes SO is quicker.

I'm using JSF Apace My Faces 1.2 without Tomahawk or any extra libs

Thanks,

+2  A: 

Sometimes , the items are added twice if multiple requests are fired.

This indicates that you're adding the items in the getter method like follows

public class Bean {
    private List<SelectItem> items = new ArrayList<SelectItem>();

    public List<SelectItem> getItems() {
        items.add(new SelectItem("value1", "label1"));
        items.add(new SelectItem("value2", "label2"));
        items.add(new SelectItem("value3", "label3"));
        return items;
    }
}

This is not good. A javabean getter should have the sole purpose to return a bean property or at highest do lazy loading, not to do some business stuff. You should create and fill the list during bean construction

public class Bean {
    private List<SelectItem> items;

    public Bean() {
        items = new ArrayList<SelectItem>();
        items.add(new SelectItem("value1", "label1"));
        items.add(new SelectItem("value2", "label2"));
        items.add(new SelectItem("value3", "label3"));
    }
}

or initialization

public class Bean {
    private List<SelectItem> items;

    {
        items = new ArrayList<SelectItem>();
        items.add(new SelectItem("value1", "label1"));
        items.add(new SelectItem("value2", "label2"));
        items.add(new SelectItem("value3", "label3"));
    }
}

or @PostConstruct

public class Bean {
    private List<SelectItem> items;

    @PostConstruct
    public void init() {
        items = new ArrayList<SelectItem>();
        items.add(new SelectItem("value1", "label1"));
        items.add(new SelectItem("value2", "label2"));
        items.add(new SelectItem("value3", "label3"));
    }
}

Is there an Application Scope ?

There is. Just use <managed-bean-scope>application</managed-bean-scope>.

or Whats the standard way of doing these stuff?

An application scoped bean is the way to go.

See also:

BalusC
This indicates that you're adding the items in the getter method or are using a session scoped bean and only clearing/constructing the list during property declaration or bean construction. This is not good. You should create and fill the list during bean construction or initialization or @PostConstruct.-- Are the above statements are not contracdictory ? I do not understand!!!
gurupriyan.e
Hi BalusC, Did you mean to say to do this in a Constructor ?
gurupriyan.e
In Addition , I can also <f:selectItems value="${somebean.someList}" which will not set the values again in the bean.
gurupriyan.e
I translated text into code.
BalusC
hm, I'd guess the OP has the right to vote.. +1 ;)
Bozho