JSF没有加载托管bean

时间:2014-10-30 09:34:08

标签: jsf jsf-2 embedded-jetty

我有一个带嵌入式Jetty和JSF(Primefaces)的简单演示项目。问题是我的bean根本没有加载。

/src/main/java/HelloBean.java

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

@ManagedBean(eager=true)
@ApplicationScoped
public class HelloBean implements Serializable {

    private static final long serialVersionUID = 1L;
    ... 

    @PreDestroy
    public void preDestroy(){
        System.out.println("PRE DESTROY");
    }

    @PostConstruct
    public void postConstruct(){
        System.out.println("POST CONSTRUCT");
    }

    public HelloBean() {
        System.out.println("Hello Bean instantiated");
    }
    ...
}

/src/main/webapp/index.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"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Conti Diagnose</title>
</h:head>
<h:body>
    <h:outputText value="#{helloBean.name}"/>
...

/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">

    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>dark-hive</param-value>
    </context-param>

    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>

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

    <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>

    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

</web-app>

/src/main/webapp/WEB-INF/faces-config.xml

<?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">
    <application>
        <locale-config>
            <default-locale>de</default-locale>
        </locale-config>
        <resource-bundle>
            <base-name>lang.lang</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>
</faces-config>

输出

2014-10-30 10:13:11.525:INFO::main: Logging initialized @164ms
2014-10-30 10:13:11.633:INFO:oejs.Server:main: jetty-9.3.0.M0
Okt 30, 2014 10:13:11 AM com.sun.faces.config.ConfigureListener contextInitialized
INFORMATION: Mojarra 2.2.8-02 ( 20140915-1602 https://svn.java.net/svn/mojarra~svn/tags/2.2.8-02@13678) für Kontext '/ui' wird initialisiert.
Okt 30, 2014 10:13:12 AM com.sun.faces.spi.InjectionProviderFactory createInstance
INFORMATION: JSF1048: PostConstruct/PreDestroy-Annotationen vorhanden.  Verwaltete Bean-Methoden, die mit diesen Annotationen markiert sind, lassen die entsprechenden Annotationen verarbeiten.
Okt 30, 2014 10:13:13 AM com.sun.faces.config.ConfigureListener$WebConfigResourceMonitor$Monitor <init>
INFORMATION: Monitoring file:/C:/.../src/main/webapp/WEB-INF/faces-config.xml for modifications
Okt 30, 2014 10:13:13 AM org.primefaces.webapp.PostConstructApplicationEventListener processEvent
INFORMATION: Running on PrimeFaces 5.1
2014-10-30 10:13:13.157:INFO:oejsh.ContextHandler:main: Started o.e.j.w.WebAppContext@538540a3{/ui,[file:///.../target/classes/../../src/main/webapp/, file:///.../target/],AVAILABLE}
2014-10-30 10:13:13.221:INFO:oejs.ServerConnector:main: Started ServerConnector@5aa85b18{HTTP/1.1}{localhost:8080}
2014-10-30 10:13:13.222:INFO:oejs.Server:main: Started @1864ms

启动嵌入式服务器的类是/src/main/JettyRunner.java。主要方法是:

InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 8080);
Server server = new Server(address);

WebAppContext wac = new WebAppContext();
wac.setContextPath("/ui");
wac.setWelcomeFiles(new String[] { "index.xhtml" });
String webappDir=JettyRunner.class.getClassLoader().getResource(".").toString();
wac.setBaseResource(new ResourceCollection(new String[] {
        webappDir+"../../src/main/webapp","./target" }));

server.setHandler(wac);
server.setStopAtShutdown(true);
server.start();
server.join();

postConstruct或Constructor中的消息根本不打印。在我的xhtml中访问这个bean的属性不会产生错误,只会打印任何内容。

即使我访问不存在的bean,也没有错误消息。

为什么我的bean没有加载?

2 个答案:

答案 0 :(得分:1)

三天后我终于找到了答案:

刚刚添加了一行

wac.setResourceAlias("/WEB-INF/classes/", "/classes/");

到我的跑步者班。现在一切都按预期工作了。

答案 1 :(得分:-3)

尝试将@Named注释添加到您的bean中。此批注使托管bean可通过EL访问。然后改变

<h:outputText value="HelloBean.name"/>

<h:outputText value="#{HelloBean.name}"/>

/src/main/webapp/index.xhtml