的facelet代码:JSF F:使用列表错误selectItems的
<h:selectOneMenu id = "country" label = "country" value = "#{beanController.countryResidence}">
<f:selectItems value = "#{countries.countries}" />
</h:selectOneMenu>
bean代码:
@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;
}
}
的错误
严重:异常而加载的应用程序
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.
我跟着一些教程一步一步,但我不断收到此错误。我尝试使列表静态并初始化静态块中的值,但我得到相同的错误。
编辑:
新Bean代码
@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;
}
}
这似乎是工作,但我觉得很奇怪,我可以用非静态方法访问静态属性。如果我删除getCOuntries()方法错误说没有国家属性存在被抛出。
当发现异常时,请始终查看堆栈跟踪的最底部。你有一个'NullPointerException'这是非常自我解释。 – BalusC 2011-12-31 20:40:13