One of the many rules which a JSF starter need to know:
- Don't do business logic in a getter method.
Once you try to fix that by keeping your getter methods true getter methods (i.e. do not do anything else than just return property;
) and performing the business logic in (post)constructor and/or action(listener) methods, then this particular problem shall disappear.
Here's a kickoff example:
<h:selectOneMenu value="#{bean.country}">
<f:selectItems value="#{bean.countries}" ... />
<f:ajax listener="#{bean.changeCountry}" render="cities" />
</h:selectOneMenu>
<h:selectOneMenu id="cities" value="#{bean.city}">
<f:selectItems value="#{bean.cities}" ... />
</h:selectOneMenu>
With something like this in a @ViewScoped
bean:
private Country country; // +getter+setter
private City city; // +getter+setter
private List<Countries> countries; // +getter
private List<Cities> cities; // +getter
@EJB
private SomeService service;
@PostConstruct
public void init() {
countries = service.getCountries();
}
public void changeCountry() {
cities = service.getCities(country);
}