Hello all,
I want to get started with JSF 2.0 and use maven and tomcat 6. I tried a simple tutorial at http://maxheapsize.com/2009/07/03/getting-started-with-jsf-2-and-maven/ and cannot get it working.
When I declare a @ManagedBean, the container can't seem to find it. If I declare an identical bean manually in faces-config, my code otherwise works.
Is there a step to getting the container to scan a package or class (like in Spring 2.5)? Can someone please give me some insight into what I'm doing wrong? If not, can they recommend a good simple maven + JSF2 tutorial?
Here's my dependencies from my pom
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0.0-RC</version>
</dependency>
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.0.0-RC</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>
<scope>provided</scope>
</dependency>
Here's my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
metadata-complete="true" version="2.5">
<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>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
</web-app>
Here's the two beans:
package com.b16.web;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class HelloWorldBean implements Serializable {
private static final long serialVersionUID = 1L;
private String name = "";
@ManagedProperty("#{demoService}")
private Service service;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setService(Service service) {
this.service = service;
}
public String getReverseName() {
return service.reverse(name);
}
}
package com.b16.web;
import java.io.Serializable;
import javax.faces.bean.*;
@ManagedBean(name = "demoService")
@ApplicationScoped
public class Service implements Serializable {
public String reverse(String name) {
return new StringBuffer(name).reverse().toString().toLowerCase();
}
private static final long serialVersionUID = 1L;
}
Here's my UI code:
<!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:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>JSF Demo</title>
</h:head>
<h:body>
<h:outputScript name="jsf.js" library="javax.faces" target="body">
</h:outputScript>
<h1>Test with annotated Managed Bean</h1>
<h:form>
<h:inputText id="name" value="#{helloWorldBean.name}">
<f:ajax render="result" />
</h:inputText>
<h:commandButton value="Say reverse Hi via Ajax">
<f:ajax execute="name" render="reverseName" />
</h:commandButton>
<h:outputText id="result" />
<h:outputText id="reverseName" value="#{helloWorldBean.reverseName}" />
</h:form>
<h1>Test with bean declared in face-config.xml</h1>
<h:form>
<h:inputText id="name" value="#{helloWorldBean2.name}">
<f:ajax render="result" />
</h:inputText>
<h:commandButton value="Say reverse Hi via Ajax">
<f:ajax execute="name" render="reverseName" />
</h:commandButton>
<h:outputText id="result" />
<h:outputText id="reverseName" value="#{helloWorldBean2.reverseName}" />
</h:form>
</h:body>
</html>
Here's my faces config. HelloWorldBean2 is just HelloWorldBean copied and pasted with the annotations removed. In HelloWorldBean2, the Service class is manually constructed instead of injected.
<?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">
<managed-bean>
<managed-bean-name>helloWorldBean2</managed-bean-name>
<managed-bean-class>com.b16.web.HelloWorldBean2</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
</faces-config>
The 2nd form, using the bean declared in the faces-config.xml, JSF-1.2-style, works fine. The first form cannot find helloWorldBean. The JSF debug error screen shows helloWorldBean2 in the session scope, but no other beans in any context.
Any help would be greatly appreciated.
Steven
Harvard Children's Hospital