You should not populate the list in the setter method. In fact, for non-input components, the setter method is never invoked. The <f:selectItems>
is output-only and never invokes its setter. The setter is only invoked for input components when JSF needs to update the model values with submitted values. For example the value of <h:selectOneMenu>
itself.
Instead, you should prepopulate the model in the constructor, or better, the @PostConstruct
annotated method of the bean.
E.g.
public class Bean {
private Miesiac[] listaMiesiecy;
@PostConstruct
public void init() {
this.listaMiesiecy = new Miesiac[12];
this.listaMiesiecy[0] = new Miesiac(1, "Styczen", 31);
this.listaMiesiecy[1] = new Miesiac(2, "Luty", 28);
this.listaMiesiecy[2] = new Miesiac(3, "Marzec", 31);
this.listaMiesiecy[3] = new Miesiac(4, "Kwiecień", 30);
this.listaMiesiecy[4] = new Miesiac(5, "Maj", 31);
this.listaMiesiecy[5] = new Miesiac(6, "Czerwiec", 30);
this.listaMiesiecy[6] = new Miesiac(7, "Lipiec", 31);
this.listaMiesiecy[7] = new Miesiac(8, "Sierpień", 31);
this.listaMiesiecy[8] = new Miesiac(9, "Wrzesień", 30);
this.listaMiesiecy[9] = new Miesiac(10, "Październik", 31);
this.listaMiesiecy[10] = new Miesiac(11, "Listopad", 30);
this.listaMiesiecy[11] = new Miesiac(12, "Grudzień", 31);
}
public Miesiac[] getListaMiesiecy(){
return this.listaMiesiecy;
}
// No setter required.
}
The general rule is: do not put anything else in getter other than return property;
and in setter other then this.property = property;
. You should perform business logic in (post)constructor or (action)listener methods instead. If you need to populate the <f:selectItems>
depending on another value during a <f:ajax>
event, then use <f:ajax listener>
instead.
By the way, if the <f:selectItems value>
is that hardcoded, you could also put the property behind the value in a separate application scoped bean, so that it's created only once during application's lifetime instead of repeatedly on every single HTTP request.