【问题标题】:JSF managed bean in Maven project: Target Unreachable, identifier 'bean' resolved to nullMaven 项目中的 JSF 托管 bean:目标无法访问,标识符“bean”解析为 null
【发布时间】:2015-09-14 16:57:01
【问题描述】:

Target unreachable 错误即将到来,不知何故我的 bean 没有初始化并且 bean 的值变为 null。我的 helloBean 引用即将为空。

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0    http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.rout.sagar</groupId>
 <artifactId>DemoMavenJAVAEE</artifactId>
 <packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>DemoMavenJAVAEE Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-api</artifactId>
    <version>2.1.7</version>
</dependency>
<dependency>
    <groupId>com.sun.faces</groupId>
    <artifactId>jsf-impl</artifactId>
    <version>2.1.7</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
    </dependency>
            <!-- Tomcat 6 need this -->
    <dependency>
        <groupId>com.sun.el</groupId>
        <artifactId>el-ri</artifactId>
        <version>1.0</version>
    </dependency>

 </dependencies>
 <build>
<finalName>DemoMavenJAVAEE</finalName>

    </build>
</project>

HelloBean.java

package com.rout.sagar;

   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 HelloBean hello ;

private String name ; 
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>DemoMavenJAVAEE</display-name>

<!-- Change to "Production" when you are ready to deploy -->



<context-param>
<param-name>org.apache.myfaces.annotation.SCAN_PACKAGES</param-name>
 <param-value>com.rout.sagar.HelloBean</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
    <welcome-file>faces/hello.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>

faces-config.xml

<?xml version="1.0"?>
    <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">

    <navigation-rule>
<from-view-id>/hello.xhtml</from-view-id>
<navigation-case>
    <from-outcome>success</from-outcome>
    <to-view-id>/welcome.xhtml</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

你好.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="welcome"></h:commandButton>
</h:form>
</h:body>
 </html>

Welcome.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>JSF2.0 hello World    </title>
</h:head>     
  <h:body bgcolor = "red">
  <h2>JSF 2.0 Hello World Example welcome.xhtml</h2>
  <h2>Welcome #{helloBean.name}</h2>
  </h:body>
  </html>

【问题讨论】:

    标签: maven jakarta-ee jsf-2 managed-bean


    【解决方案1】:

    您将支持 bean 的 Java 源文件放在 src/main/resources 中,而不是 src/main/java 中。所以在构建WAR文件的过程中,Java源文件不会被编译成/WEB-INF/lib中的.class文件。

    修复错误的 Maven 项目结构。我认为项目图标上的红叉也暗示了项目结构中的问题。

    另见:


    与具体问题无关:您的 pom 不应该让 webapp 提供目标服务器本身已经提供的库,例如 Tomcat 的 JSP、Servlet 和 EL。而且,那个 JSF impl 版本很古老。

    【讨论】:

    • 是的,它确实有效。我尝试了您在第一个链接中提到的所有内容,但我不知道 maven 的结构是如何工作的。
    猜你喜欢
    • 2019-05-10
    • 2018-04-17
    • 2014-12-11
    • 1970-01-01
    • 2012-10-17
    • 2013-07-20
    • 2016-03-09
    • 2022-12-12
    • 2013-10-16
    相关资源
    最近更新 更多