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: