【问题标题】:Target Unreachable, identifier 'userBean' resolved to null [duplicate]目标无法访问,标识符“userBean”解析为空 [重复]
【发布时间】:2018-05-09 07:49:12
【问题描述】:
  • 有两种配置托管bean的方法,一种是使用“faces-config.xml”文件,另一种是使用“注解”。
  • 所以在这个演示中,我想在 MyEclipse 中使用注解配置 bean,但是没有成功。
  • 这里是代码:

1.UserBean.java

public class UserBean {
String userid;
String password;

@Named("userBean")
@RequestScoped
public String getUserid() {
    return userid;
}
public void setUserid(String userid) {
    this.userid = userid;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}}

2.Login.xhtml the page users enter the id and password

3.Welcome.xhtml when user click the submit button, the page comes

4.faces-config.xml faces-config.xml


如您所见,我没有在“faces-config.xml”文件中配置托管bean,我只是在“UserBean.java”中使用“@Named("userBean")”和“@RequestScoped” " 文件来配置 bean。


1.我在网站上打开login.xhtml

http://localhost:8080/JSF/


2.当我点击按钮提交数据时,它会出现这个页面:

After click the submit button


我这几天开始学习JSF,有很多困惑的地方需要弄清楚,如果你能给我一些关于这个问题的笔记或指导,非常感谢^_^


(Ps.这是我在stackoverflow上问的第一个问题,所以我无法直接上传图片,如果您无法通过hperlinks看到图片,请告诉我。谢谢!)

【问题讨论】:

    标签: jsf cdi managed-bean propertynotfoundexception


    【解决方案1】:

    您需要将@Named bean 注解设置为类而不是方法。 该错误基本上表示服务器未能找到托管 bean 类。所以你的代码应该是这样的:

    @Named("userBean")
    @RequestScoped 
    public class UserBean {
        String userid;
        String password;
    
        public String getUserid() {
            return userid;
        }
    

    我看到了您的 Welcome.xhtml。您应该使用# 而不是$。 所以你的欢迎页面应该是这样的

    <h:outputLabel value="#{userBean.password}" />
    

    【讨论】:

    • 感谢您的回复,我更改了您指出的代码,但仍然出现错误页面,即使是同样的错误:目标无法访问,标识符'userBean'解析为null。
    • 您是否在 web.xml 中指定了 faces servlet 条目?
    • Login.xhtmlFaces Servlet-name> javax.faces.webapp.FacesServlet1Faces Servlet*.xhtmljavax.faces.PROJECT_STAGE开发
    • 我认为错误在于表达式语言。尝试在每个地方都使用"#{userBean.VALUE}" 而不是 "${userBean.VALUE}"
    • 我试过了,一直没用。无论如何,非常感谢你,我明天可能会问我的老师。GN。
    【解决方案2】:
    • 我想我不能使用annotation bean的原因是我没有在我的应用程序中配置CDI,因为Tomcat本身不支持CDI,你应该手动添加一些外部jar文件使其支持。所以这是我配置它的步骤。

    1. 下载weld-servlet.jar文件,这里是我下载的链接,你也可以从网上下载。 http://www.jsf2.com/using-cdi-and-jsf-2.2-faces-flow-in-tomcat/samples/weld-servlet.jar
    2. 将此jar文件添加到目录“/WEB-INF/lib”(最好建路径)
    3. 在“/WEB-INF”下创建beans.xml文件,将beans.xml文件已有代码替换为如下代码sn-p:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"></beans>

    4. 还有一点,你必须实现 Serializble 接口。


    • 给你我的整个程序大纲。 image

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 2017-07-02
      • 1970-01-01
      • 2019-05-10
      • 2012-10-17
      • 2012-02-22
      • 2014-06-12
      • 2016-07-01
      • 2012-04-06
      相关资源
      最近更新 更多