日期:2014-05-16  浏览次数:20319 次

在Tomcat7上配置JSF的CDI支持

在Tomcat7上添加JSF2.0的Contexts and Dependency Injection(CDI)支持



1. JavaBean中把@ManagedBean改为@Named,把Session改为javax.enterprise.context包下的,JavaBean 必须实现Serializable接口。如下:

?

import java.io.Serializable;
import javax.inject.Named; 
import javax.enterprise.context.SessionScoped; 
@Named
@SessionScoped




2. 在http://seamframework.org/ 下载CDI的weld实现。
添加\artifacts\weld\weld-servlet.jar 这个文件到Build Path中。

3. web.xml 中添加以下代码:

?

<listener> 
	<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class> 
</listener> 



4. 在WEB-INF目录下增加空文件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>



重启Tomcat7,配置完成!



参考文章:
http://www.theserverside.com/tutorial/Working-with-CDI-and-JSF-20-on-Tomcat-7-Configuring-Weld

?