1. Project Dependencies
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0 </modelVersion>
<groupId>com.mkyong.common </groupId>
<artifactId>JavaServerFaces </artifactId>
<packaging>war </packaging>
<version>1.0-SNAPSHOT </version>
<name>JavaServerFaces Maven Webapp </name>
<url>http://maven.apache.org </url>
<repositories>
<repository>
<id>java.net.m2 </id>
<name>java.net m2 repo </name>
<url>http://download.java.net/maven/2 </url>
</repository>
</repositories>
<dependencies>
<!-- Spring framework -->
<dependency>
<groupId>org.springframework </groupId>
<artifactId>spring </artifactId>
<version>2.5.6 </version>
</dependency>
<dependency>
<groupId>org.springframework </groupId>
<artifactId>spring-web </artifactId>
<version>2.5.6 </version>
</dependency>
<!-- For Java EE Application Server, uncomment this library
and comment the rest of the libraries
<dependency>
<groupId>javax.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
-->
<!-- For Servlet Container like Tomcat -->
<!-- http://download.java.net/maven/2 -->
<dependency>
<groupId>com.sun.faces </groupId>
<artifactId>jsf-api </artifactId>
<version>2.1.0-b03 </version>
</dependency>
<dependency>
<groupId>com.sun.faces </groupId>
<artifactId>jsf-impl </artifactId>
<version>2.1.0-b03 </version>
</dependency>
<!-- EL 2.2 to support method parameter in EL -->
<dependency>
<groupId>org.glassfish.web </groupId>
<artifactId>el-impl </artifactId>
<version>2.2 </version>
</dependency>
<!-- http://repo1.maven.org/maven2/ -->
<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>
</dependency>
<dependency>
<groupId>javax.servlet.jsp </groupId>
<artifactId>jsp-api </artifactId>
<version>2.1 </version>
</dependency>
</dependencies>
<build>
<finalName>JavaServerFaces </finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins </groupId>
<artifactId>maven-compiler-plugin </artifactId>
<version>2.3.1 </version>
<configuration>
<source>1.6 </source>
<target>1.6 </target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. Spring Bean
A Spring bean declaration, later uses Spring to DI this “userBo” into JSF 2.0 managed bean property.
File : UserBo.java
package com.mkyong.user.bo;
public interface UserBo{
public String getMessage();
}
File : UserBoImpl.java
package com.mkyong.user.bo.impl;
import com.mkyong.user.bo.UserBo;
public class UserBoImpl implements UserBo{
String name;
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return "JSF 2 + Spring Integration saying : " + name;
}
}
File : applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean id="userBo" class="com.mkyong.user.bo.impl.UserBoImpl"> <property name="name" value="mkyong.com" /> </bean> </beans>
3. JSF 2.0
A simple JSF 2.0 web application.
File : UserBean.java
package com.mkyong;
import java.io.Serializable;
import com.mkyong.user.bo.UserBo;
//DI via JSF managed bean
public class UserBean implements Serializable{
//DI via Spring
UserBo userBo;
public void setUserBo(UserBo userBo) {
this.userBo = userBo;
}
public String printMsgFromSpring() {
return userBo.getMessage();
}
}
File : default.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"
>
<h:body>
<h1>JSF 2.0 + Spring Example</h1>
#{user.printMsgFromSpring()}
</h:body>
</html>
JSF 2.0 + Spring Integration
The main concern is how to inject Spring “UserBo” bean into JSF managed bean “UserBean“.
File : UserBean.java
//DI via JSF managed bean
public class UserBean implements Serializable{
//DI via Spring
UserBo userBo;
1. Spring Listeners
Add following two Spring’s listeners into web.xml.
<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>
See full example of web.xml.
File : web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>JavaServerFaces</display-name> <!-- Add Support for Spring --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener> <!-- Change to "Production" when you are ready to deploy --> <context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param> <!-- Welcome page --> <welcome-file-list> <welcome-file>faces/default.xhtml</welcome-file> </welcome-file-list> <!-- JSF mapping --> <servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Map these files with JSF --> <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>*.jsf</url-pattern> </servlet-mapping> <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> </web-app>
2. SpringBeanFacesELResolver
Add SpringBeanFacesELResolver as el-resolver in faces-config.xml, and declare JSF managed bean like normal. Now, whenever JSF sees a bean name, it try JSF rules first, then Spring rules next. In this case, when JSF see #{userBo}, it will match it with Spring’s “userBo” bean.
File : 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> <el-resolver> org.springframework.web.jsf.el.SpringBeanFacesELResolver </el-resolver> </application> <managed-bean> <managed-bean-name>user</managed-bean-name> <managed-bean-class>com.mkyong.UserBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> <managed-property> <property-name>userBo</property-name> <value>#{userBo}</value> </managed-property> </managed-bean> </faces-config>
Alternatively, you can add
DelegatingVariableResolver in variable-resolver, but API variable-resolver is deprecated after JSF 1.1. You should use
el-resolver instead.
<application> <variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver> </application>
本文介绍如何将JavaServer Faces (JSF) 2.0与Spring框架进行集成,实现依赖注入和资源解析等功能。通过配置Spring监听器、使用Spring Bean Faces EL Resolver,演示了具体的集成步骤。
1000

被折叠的 条评论
为什么被折叠?



