【问题标题】:javax.el.PropertyNotFoundException: /demo.xhtml @24,55 value="#{UserBean.favYear3}": Target Unreachable, identifier 'UserBean' resolved to null [duplicate]javax.el.PropertyNotFoundException:/demo.xhtml @24,55 value="#{UserBean.favYear3}":目标无法访问,标识符“UserBean”解析为 null [重复]
【发布时间】:2012-06-16 02:54:35
【问题描述】:

我想在 JSF 中有一个列表框。我写了一个简单的代码,但它不起作用。在演示页面中,我看到一个没有列表的空框,在用户页面中出现错误。

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)

怎么了?

【问题讨论】:

    标签: java eclipse jsf jakarta-ee jsf-2


    【解决方案1】:

    您在 EL 中的托管 bean 名称错误。您已将 bean 声明如下:

    @ManagedBean
    @SessionScoped   
    public class UserBean implements Serializable{
    

    当您没有指定@ManagedBeanname 属性时,它将符合Javabeans 命名约定默认为第一个字符小写的类名,如userBean,但您仍在尝试引用它们具有确切的类名#{UserBean}。您需要将此名称相应地修复为#{userBean}

    faces-config.xml 注册对于 JSF 2.x 来说是不必要的。删除它。

    【讨论】:

      【解决方案2】:

      您必须使您的 bean 可访问/可管理。为此,您可以

      使用 CDI (@Named) 或 JSF (@ManagedBean) 注释对其进行注释:

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

      或在faces-config.xml 中将其描述为managed-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>
      

      【讨论】:

      • ,我以前做过,我编辑了帖子。
      • 我将您的代码添加到我的面部配置中。现在我没有错误,但它不起作用。我的列表是空的,我无法选择
      • @Sami:如果答案没有解决您的具体问题,那么您不应该立即将其标记为已接受。这只会让其他读者感到困惑。只有当答案真正解决了具体问题时才标记为接受。
      猜你喜欢
      • 2018-05-09
      • 2014-02-10
      • 2013-09-26
      • 2015-05-18
      • 2013-10-16
      • 1970-01-01
      • 2017-07-02
      • 1970-01-01
      • 2019-05-10
      相关资源
      最近更新 更多