eclipse javax.el.PropertyNotFoundException: /demo.xhtml @24,55 value="#{UserBean.favYear3}": Target Unreachable, identifier 'Use

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11009974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:21:38  来源:igfitidea点击:

javax.el.PropertyNotFoundException: /demo.xhtml @24,55 value="#{UserBean.favYear3}": Target Unreachable, identifier 'UserBean' resolved to null

javaeclipsejsfjakarta-eejsf-2

提问by samira

I want to have a list box in JSF. I have written a simple code but it does not work. In demo page I see an empty box with out list and in user page I have error.

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

UserBean.java

用户Bean.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

演示.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

用户.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>

My problem: in demo page I have an empty box. in user page the error is:

我的问题:在演示页面中,我有一个空框。在用户页面中,错误是:

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)

What is wrong?

怎么了?

采纳答案by kostja

You have to make your bean reachable/managed. For this, you can either

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

annotate it with a CDI (@Named) or a JSF (@ManagedBean) annotation:

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

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

or describe it in the faces-config.xmlas a managed-beanlike this:

或将其描述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>

回答by BalusC

Your managed bean name in EL is wrong. You've declared the bean as follows:

您在 EL 中的托管 bean 名称是错误的。您已按如下方式声明了 bean:

@ManagedBean
@SessionScoped   
public class UserBean implements Serializable{

When you don't specify the nameattribute of the @ManagedBean, then it will conform the Javabeans Naming Conventions default to the classname with 1st character lowercased like so userBean, but yet you're trying to reference them with the exact classname #{UserBean}. You need to fix this name accordingly as #{userBean}.

当您不指定 的name属性时@ManagedBean,它将遵循 Javabeans 命名约定默认为第一个字符小写的类名userBean,但您试图使用确切的类名引用它们#{UserBean}。您需要相应地将此名称修复为#{userBean}.

The faces-config.xmlregistration is unnecessaryfor JSF 2.x. Remove it.

faces-config.xml注册是不必要对JSF 2.x的 去掉它。