【问题标题】:Java EE 6: Target Unreachable, identifier 'helloBean' resolved to null [duplicate]Java EE 6:目标无法访问,标识符“helloBean”解析为 null [重复]
【发布时间】:2012-04-06 06:15:14
【问题描述】:

我正在尝试制作一个简单的 JSF 2 教程示例。

我在 Eclipse 中使用动态 Web 项目并发布到 Glassfish 3 服务器(运行 -> 在服务器上运行)。第一个 index.xhtml 页面加载正确,但是当我必须访问托管 bean 时,显示以下错误:

/index.xhtml @14,48 value="#{helloBean.name}": Target Unreachable, identifier 'helloBean' resolved to null

我已经查看了有关此主题的各种其他讨论,但是解决方案似乎对我不起作用(例如,添加 beans.xml、为托管 bean 命名等,遵循命名约定)。

任何帮助将不胜感激。

这是我目前正在使用的代码:

索引.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:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body>
        <h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
        <h:form>
           <h:inputText value="#{helloBean.name}"></h:inputText>
           <h:commandButton value="Welcome Me" action="response"></h:commandButton>
        </h:form>
    </h:body>
</html>

response.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">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body bgcolor="white">
        <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
        <h4>Welcome #{helloBean.name}</h4>
    </h:body>
</html>

托管 bean:

package java.hello1;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.io.Serializable;


@ManagedBean
@SessionScoped
public class HelloBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name = "Ricardo";

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5">

  <display-name>JavaServerFaces</display-name>

  <!-- Change to "Production" when you are ready to deploy -->
  <context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
  </context-param>

  <!-- Welcome page -->
  <welcome-file-list>
    <welcome-file>faces/index.xhtml</welcome-file>
  </welcome-file-list>

  <!-- JSF mapping -->
  <servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <!-- Map these files with JSF -->
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>

</web-app>

【问题讨论】:

  • 如果您使用 maven 运行代码,请尝试使用 maven 目标 tomcat:run-war 而不是 tomcat:run 运行它。希望这有帮助。 :)
  • @stiv:那你和 OP 的问题不一样。你有没有在一个完全空白的游乐场项目中复制'n'paste'n'runned他的代码,一切都设置为默认值?
  • 我用 Intellij IDEA 创建了项目,添加了对 JSF 的支持,但它不想看到我的 bean。同时
  • @stiv:如果您完全使用相同的代码和环境设置(Eclipse + Glassfish 3),那么除了已经回答的问题之外,我没有看到其他原因。结论是您没有完全与OP相同的问题。

标签: java jakarta-ee jsf-2 java-ee-6 managed-bean


【解决方案1】:

您需要有一个符合 JSF 2.0 的 /WEB-INF/faces-config.xml 文件才能让 JSF 解释注释。

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    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/web-facesconfig_2_0.xsd"
    version="2.0">

    <!-- Config here. Can even be kept empty. -->

</faces-config>

如果您已经有一个或者如果这不能解决问题,如果您没有看到任何警告/错误,请注意服务器启动日志。

顺便说一句,您的/WEB-INF/web.xml 文件被声明为符合Servlet 2.5 规范。虽然这不一定有害,但如果您使用的是符合 Servlet 3.0 的容器,则没有任何意义。更新根声明如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app
    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/web-app_3_0.xsd" 
    version="3.0">

    <!-- Config here. -->

</web-app>

/WEB-INF/beans.xml 用于 CDI 注释,如 @Named@Inject 等。只需一个完全空的文件就足以打开它。它与 @ManagedBean@ManagedProperty 等 JSF 注释完全没有关系。它也不应该相互混淆/混合。

【讨论】:

    【解决方案2】:

    我被这样的问题困扰了半天。只有当我从 Eclipse 运行 WebApp 时,才会出现我的问题。 JSF 2 在 WEB-INF/classes 中查找带注释的 bean,但没有找到它们。为了解决这个问题,我将构建输出路径更改为 WebContent/WEB-INF/classes。下面是类似案例的详细解释:Jetty maven goal jetty:run does not work with JSF 2.0 @ManagedBean

    【讨论】:

    • 这是构建工具和/或服务器插件的错误。 Eclipse 在其默认修剪(没有 Maven 或任何自定义服务器插件)中正确地完成了这项工作。
    • 对于任何尝试 mkyong 示例的人(就像 2015 年一样)mkyong.com/jsf2/jsf-2-0-hello-world-example 这就是解决方案!
    【解决方案3】:

    在与 OP 完全相同的场景中(使用 Eclipse,使用运行发布到 Glassfish 3 服务器 -> 在服务器上运行)我得到完全相同的错误,直到从 Eclipse 项目名称中删除空格。只需删除任何空格即可解决问题。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,我尝试了所有方法。之后,我只需单击“单击并构建项目”按钮、“构建项目”按钮并重新启动 GlassFish 服务器。之后,它对我有用,现在:)

      【讨论】:

        【解决方案5】:

        请检查您的战争文件,看看您的课程是否在 WEB-INF/classes 文件夹下。 我遇到了同样的问题,发现没有类文件 WEB-INF/classes 文件夹。

        【讨论】:

          【解决方案6】:

          在你的 Controller/Bean 中包含 @Named..

          【讨论】:

          • 这确实是问题标题中描述的问题的可能答案之一,但这并不能回答问题正文中描述的问题。
          • 在我的 hello bean 上使用命名注释时出现错误。它通过使用@ManagedBean 注释得到解决。
          猜你喜欢
          • 2019-05-10
          • 1970-01-01
          • 2018-05-09
          • 2012-06-16
          • 2013-10-16
          • 1970-01-01
          • 2017-07-02
          • 2013-07-20
          • 2014-02-10
          相关资源
          最近更新 更多