的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