Reputation: 4073
I got a simple demo project with embedded Jetty and JSF (Primefaces). The problem is that my beans are not loaded at all.
/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>
The output is
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
The class to start the embedded server is /src/main/JettyRunner.java. The main method is:
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();
The messages in postConstruct or the Constructor are not printed at all. Accessing a property of this bean in my xhtml gives no error and just prints nothing.
Even if I access a bean that does not exist, there's no error message.
Why are my beans not loaded?
Upvotes: 1
Views: 3200
Reputation: 4073
After three days I finally found the answer myself:
Just added the line
wac.setResourceAlias("/WEB-INF/classes/", "/classes/");
to my runner class. Now everything works as expected.
Upvotes: 1
Reputation: 71
Try adding the @Named
annotation to your bean. This annotation makes the managed bean accessible through the EL. Then change
<h:outputText value="HelloBean.name"/>
to
<h:outputText value="#{HelloBean.name}"/>
in /src/main/webapp/index.xhtml
Upvotes: -3