Tomcat7下使用CDI and JSF 2.0
发现PrimeFaces(primefaces.org)DEMO不错,又让我想学学JSF2.0。看了
Core JavaServer Faces,3rd Edition 第一二章,使用ManagedBean在Tomcat
下使用是没问题的,但是CDI BEANS(@Named)是不能在非Java EE 6应用服务器
上跑的,这意味着我不能在Tomcat下使用CDI,这让我很不爽,而我又不想用
Glashfish,WebSphere...
书上也没详细的说明如何在TOMCAT下使用CDI,只说借助于JBOSS weld可以。看了
下官方文档,摸索成功。
1,新建MAVEN webapp(maven-archetyhpe-webapp)工程。(我使用STS2.9.1)
2,pom.xml文件增加依赖包:
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.1.9</version>
</dependency>
<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet</artifactId>
<version>1.1.8.Final</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
3,在WEB-INF目录下新增beans.xml和faces-config.xml.
beans.xml文件可以为空或者以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<beans 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/beans_1_0.xsd">
</beans>
faces-config.xml 文件如下内容
<?xml version="1.0"?>
<faces-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" >
</faces-config>
4.web.xml增加
<!-- Use CDI in Tomcat -->
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
5.JavaBean
package com.corejsf;
import java.io.Serializable;
//import javax.faces.bean.ManagedBean; //using in legacy application servers and standalone servlet runners
//import javax.faces.bean.SessionScoped;
//import javax.annotation.ManagedBean;wrong
import javax.inject.Named; //Using in a Java EE 6 compliant application server ,but using weld,we can use it in standalone servlet runners
import javax.enterprise.context.SessionScoped;
//@ManagedBean(name = "user")
@Named("user")
@SessionScoped
public class UserBean implements Serializable {
private static final long serialVersionUID = 4406234175180002994L;
private String name;
private String password;
... get set method..
}
6,JSF files
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:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Welcome</title>
</h:head>
<h:body>
<h:form prependId="false">
<h3>Please enter your name and password.</h3>
<table>
<tr>
<td>Name:</td>
<td><h:inputText value="#{user.name}" id="name" /></td>
</tr>
<tr>
<td>Password:</td>
<td><h:inputSecret value="#{user.password}" id="password"/></td>
</tr>
</table>
<p>
<!-- -h:commandButton value="Login" action="welcome" /-->
<h:commandButton value="Login">
<f:ajax execute="name password" render="out" />
</h:commandButton>
</p>
<h:outputText id="out" value="#{user.greeting}" />
</h:form>
</h:body>
</html>
7,web.xml增加JSF相关配置,
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value><!-- Development, UnitTest, SystemTest, and Production -->
</context-param>
8,STS集成的VMware vFabric tc Server Developer Edition带有TOMCAT7,发布
应用,运行成功。。。