
- JSF 教程
- JSF - 首頁
- JSF - 概述
- JSF - 環境搭建
- JSF - 架構
- JSF - 生命週期
- JSF - 第一個應用程式
- JSF - 託管Bean
- JSF - 頁面導航
- JSF - 基本標籤
- JSF - Facelet標籤
- JSF - 轉換器標籤
- JSF - 驗證器標籤
- JSF - DataTable
- JSF - 複合元件
- JSF - Ajax
- JSF - 事件處理
- JSF - JDBC整合
- JSF - Spring整合
- JSF - 表示式語言
- JSF - 國際化
- JSF 有用資源
- JSF - 快速指南
- JSF - 有用資源
- JSF - 討論
JSF - Spring整合
Spring 提供了一個特殊的類 `DelegatingVariableResolver`,以無縫的方式將 JSF 和 Spring 整合在一起。
將 Spring 依賴注入 (IOC) 功能整合到 JSF 中需要以下步驟。
步驟 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 | 建立一個名為 `helloworld` 的專案,放在 `com.tutorialspoint.test` 包下,如《JSF - 第一個應用程式》章節中所述。 |
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>
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> <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>
web.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>
applicationContext.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>
MessageService.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; } }
UserData.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(); } }
home.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 - 第一個應用程式》章節中那樣編譯並執行應用程式。如果應用程式一切正常,則會產生以下結果。

廣告