Facelet code:
<h:selectOneMenu id = "country" label = "country" value = "#{beanController.countryResidence}">
<f:selectItems value = "#{countries.countries}" />
</h:selectOneMenu>
Bean Code:
@ManagedBean(eager=true, name = "countries")
@ApplicationScoped
public class CountriesConstants {
private List<SelectItem> countries;
public CountriesConstants(){
countries.add(new SelectItem("DE", "Germany"));
countries.add(new SelectItem("JA", "Japan"));
countries.add(new SelectItem("RU", "Russia"));
countries.add(new SelectItem("US", "United States"));
}
public List<SelectItem> getCountries() {
return countries;
}
public void setCountries(List<SelectItem> countries) {
this.countries = countries;
}
}
The Error
SEVERE: Exception while loading the app
SEVERE: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: java.lang.RuntimeException: com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.mysite.util.CountriesConstants.
I followed some tutorials step by step but I keep getting this error. I tried making the List static and initialise the values in a static block but I get the same error.
EDIT:
The new Bean Code
@ManagedBean(eager=true, name="constants")
@ApplicationScoped
public class Constants {
public static final String VALIDATE_DETAILED = "detailed";
public static final List<SelectItem> countries;
static{
countries = new ArrayList<SelectItem>();
countries.add(new SelectItem("DE", "Germany"));
countries.add(new SelectItem("JA", "Japan"));
countries.add(new SelectItem("RU", "Russia"));
countries.add(new SelectItem("US", "United States"));
}
public List<SelectItem> getCountries() {
return countries;
}
}
This seems to work but I find it weird that I can access a static attribute with a non static method. If I remove the getCOuntries() method an error saying that no countries attribute exists is thrown.
In your bean constructor, you must create your List first, try this:
Besides, your
<f:selectItems>tag should have more attribute. Something like this:UPDATE: suppose you have the following controller