JSF - Spring 集成
Spring 提供了特殊的 DelegatingVariableResolver 类,以无缝方式将 JSF 和 Spring 集成在一起。
在 JSF 中集成 Spring 依赖注入 (IOC) 功能需要执行以下步骤。
第 1 步:添加 DelegatingVariableResolver
在 faces-config.xml 中添加变量解析器条目以指向 spring 类DelegatingVariableResolver。
<faces-config> <application> <variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver> ... </faces-config>
第2步:添加上下文监听器
在web.xml中添加spring框架提供的ContextLoaderListener和RequestContextListener监听器。
<web-app> ... <!-- 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> ... </web-app>
第 3 步:定义依赖关系
在 applicationContext.xml 中定义 bean,它将用作托管 bean 中的依赖项。
<beans> <bean id = "messageService" class = "com.tutorialspoint.test.MessageServiceImpl"> <property name = "message" value = "Hello World!" /> </bean> </beans>
第 4 步:添加依赖项
DelegatingVariableResolver首先将值查找委托给 JSF 的默认解析器,然后委托给 Spring 的 WebApplicationContext。这使得人们可以轻松地将基于 spring 的依赖项注入到 JSF 管理的 bean 中。
我们在这里注入了 messageService 作为基于 spring 的依赖项。
<faces-config> ... <managed-bean> <managed-bean-name>userData</managed-bean-name> <managed-bean-class>com.tutorialspoint.test.UserData</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>messageService</property-name> <value>#{messageService}</value> </managed-property> </managed-bean> </faces-config>
第 5 步:使用依赖项
//jsf managed bean public class UserData { //spring managed dependency private MessageService messageService; public void setMessageService(MessageService messageService) { this.messageService = messageService; } public String getGreetingMessage() { return messageService.getGreetingMessage(); } }
应用示例
让我们创建一个测试 JSF 应用程序来测试 spring 集成。
步 | 描述 |
---|---|
1 | 按照JSF - 第一个应用程序章节中的说明,在com.tutorialspoint.test包下创建一个名为helloworld的项目。 |
2 | 按如下所述修改pom.xml 。 |
3 | 在WEB-INF文件夹中创建faces-config.xml,如下所述。 |
4 | 按如下所述修改web.xml 。 |
5 | 在WEB-INF文件夹中创建applicationContext.xml,如下所述。 |
6 | 在com.tutorialspoint.test包下创建MessageService.java,如下所述。 |
7 | 在com.tutorialspoint.test包下创建MessageServiceImpl.java,如下所述。 |
8 | 在com.tutorialspoint.test包下创建UserData.java,如下所述。 |
9 | 按如下所述修改home.xhtml 。其余文件保持不变。 |
10 | 编译并运行应用程序以确保业务逻辑按照要求运行。 |
11 | 最后,以war文件的形式构建应用程序并将其部署在Apache Tomcat Web服务器中。 |
12 | 使用适当的 URL 启动您的 Web 应用程序,如下面最后一步中所述。 |
pom.xml
<project xmlns = "http://maven.apache.org/POM/4.0.0" 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.tutorialspoint.test</groupId> <artifactId>helloworld</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>helloworld Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.1.7</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.1.2.RELEASE</version> </dependency> </dependencies> <build> <finalName>helloworld</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> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/helloworld/resources </outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
面孔配置.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> <variable-resolver> org.springframework.web.jsf.DelegatingVariableResolver </variable-resolver> </application> <managed-bean> <managed-bean-name>userData</managed-bean-name> <managed-bean-class>com.tutorialspoint.test.UserData</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>messageService</property-name> <value>#{messageService}</value> </managed-property> </managed-bean> </faces-config>
网络.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <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> <!-- 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> <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>*.jsf</url-pattern> </servlet-mapping> </web-app>
应用程序上下文.xml
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id = "messageService" class = "com.tutorialspoint.test.MessageServiceImpl"> <property name = "message" value = "Hello World!" /> </bean> </beans>
消息服务.java
package com.tutorialspoint.test; public interface MessageService { String getGreetingMessage(); }
MessageServiceImpl.java
package com.tutorialspoint.test; public class MessageServiceImpl implements MessageService { private String message; public String getGreetingMessage() { return message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
用户数据.java
package com.tutorialspoint.test; import java.io.Serializable; public class UserData implements Serializable { private static final long serialVersionUID = 1L; private MessageService messageService; public MessageService getMessageService() { return messageService; } public void setMessageService(MessageService messageService) { this.messageService = messageService; } public String getGreetingMessage() { return messageService.getGreetingMessage(); } }
首页.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"> <h:head> <title>JSF Tutorial!</title> </h:head> <h:body> <h2>Spring Integration Example</h2> #{userData.greetingMessage} </h:body> </html>
准备好完成所有更改后,让我们像在 JSF - 第一个应用程序章节中所做的那样编译并运行该应用程序。如果您的应用程序一切正常,这将产生以下结果。