2012-06-13 46 views
2

我想在JSF中有一個列表框。我寫了一個簡單的代碼,但它不起作用。在演示頁面中,我看到一個帶有列表和用戶頁面的空框,我有錯誤。javax.el.PropertyNotFoundException:/[email protected],55 value =「#{UserBean.favYear3}」:目標無法訪問,標識符'UserBean'解析爲空

UserBean.java

@ManagedBean 
@SessionScoped 
public class UserBean implements Serializable{ 
    public String favYear3;//list box 

    public String getFavYear3() { 
     return favYear3; 
    } 

    public void setFavYear3(String favYear3) { 
     this.favYear3 = favYear3; 
    } 
    public static class Year{ 
     public String yearLabel; 
     public String yearValue; 

     public Year(String yearLabel, String yearValue){ 
      this.yearLabel = yearLabel; 
      this.yearValue = yearValue; 
     } 

     public String getYearLabel(){ 
      return yearLabel; 
     } 

     public String getYearValue(){ 
      return yearValue; 
     } 

    } 

    public Year[] year3List; 

    public Year[] getFavYear3Value() { 

     year3List = new Year[3]; 
     year3List[0] = new Year("Year3 - 2000", "2000"); 
     year3List[1] = new Year("Year3 - 2010", "2010"); 
     year3List[2] = new Year("Year3 - 2020", "2020"); 

     return year3List; 
    } 

} 

demo.xhtml

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <title>first jsf page</title> 
</head> 
<h:body> 
    <h1>JSF 2 check example</h1> 

    <h:form> 
     <h:selectOneListbox value="#{UserBean.favYear3}"> 
      <f:selectItems value="#{UserBean.favYear3Value}" var="y" 
       itemLabel="#{y.yearLabel}" itemValue="#{y.yearValue}" /> 
     </h:selectOneListbox> 
    </h:form> 

</h:body> 
</html> 

user.xhtml

<?xml version="1.0" encoding="utf-8" ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:h="http://java.sun.com/jsf/html"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>second jsf page</title> 
</head> 
<h:body> 
    <h:outputText value="#{UserBean.favYear3}"/> 
</h:body> 
</html> 

我的問題:在演示頁面我有一個空箱子。 在用戶頁面錯誤是:

type Exception report 

message 

description The server encountered an internal error() that prevented it from fulfilling this request. 

exception 

javax.servlet.ServletException: javax.el.PropertyNotFoundException: /demo.xhtml @24,55 value="#{UserBean.favYear3}": Target Unreachable, identifier 'UserBean' resolved to null 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:321) 


root cause 

javax.faces.component.UpdateModelException: javax.el.PropertyNotFoundException: /demo.xhtml @24,55 value="#{UserBean.favYear3}": Target Unreachable, identifier 'UserBean' resolved to null 
    javax.faces.component.UIInput.updateModel(UIInput.java:848) 
    javax.faces.component.UIInput.processUpdates(UIInput.java:730) 
    javax.faces.component.UIForm.processUpdates(UIForm.java:268) 
    javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1109) 
    javax.faces.component.UIComponentBase.processUpdates(UIComponentBase.java:1109) 
    javax.faces.component.UIViewRoot.processUpdates(UIViewRoot.java:1218) 
    com.sun.faces.lifecycle.UpdateModelValuesPhase.execute(UpdateModelValuesPhase.java:74) 
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) 
    com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) 

什麼是錯?

回答

1

你必須使你的bean可達/管理。對於這一點,您可以

與CDI(@Named)或JSF(@ManagedBean)註釋標註爲:

@Named 
@SessionScoped 
public class UserBean implements Serializable{...} 

或描述它在faces-config.xmlmanaged-bean這樣的:

<managed-bean> 
    <managed-bean-name>userBean</managed-bean-name> 
    <managed-bean-class>com.example.UserBean</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
</managed-bean> 
+0

,我以前做過,我編輯的職位。 – samira

+0

我將你的代碼添加到我的face-config中。現在我沒有錯誤,但它不起作用。我的列表是空的,我不能選擇 – samira

+2

@Sami:如果答案沒有*不*解決您的具體問題,那麼你不應該立即將其標記接受。這隻會讓其他讀者感到困惑。只有在真正*解決了具體問題時,才接受接受的答案。 – BalusC

4

在EL你的託管bean的名字是錯的。你已經聲明瞭豆如下:

@ManagedBean 
@SessionScoped 
public class UserBean implements Serializable{ 

當你不指定@ManagedBeanname屬性,那麼它將符合JavaBeans的命名約定默認與第1個字符的類名小寫像這樣userBean,但但你試圖用精確的類名#{UserBean}引用它們。您需要相應地修復此名稱爲#{userBean}

faces-config.xml註冊爲對於JSF 2.x不必要的。去掉它。

相關問題