JSF-Springの統合
Springは、JSFとSpringをシームレスに統合する特別なクラスDelegatingVariableResolverを提供します。
JSFにSpring Dependency Injection(IOC)機能を統合するには、次の手順が必要です。
ステップ1:DelegatingVariableResolverを追加する
faces-config.xmlにvariable-resolverエントリを追加して、Springクラス DelegatingVariableResolver をポイントします。
ステップ2:コンテキストリスナーを追加する
web.xmlのspringフレームワークによって提供される ContextLoaderListener および RequestContextListener リスナーを追加します。
ステップ3:依存関係を定義する
管理対象Beanの依存関係として使用されるapplicationContext.xmlでBeanを定義します。
ステップ4:依存関係を追加する
ここでは、スプリングベースの依存関係としてmessageServiceを注入しました。
ステップ5:依存関係を使用する
応用例
テストJSFアプリケーションを作成して、スプリング統合をテストしましょう。
Step | Description |
---|---|
1 | Create a project with a name helloworld under a package com.finddevguides.test as explained in the JSF - First Application chapter. |
2 | Modify pom.xml as explained below. |
3 | Create faces-config.xml in WEB-INF folder as explained below. |
4 | Modify web.xml as explained below. |
5 | Create applicationContext.xml in WEB-INF folder as explained below. |
6 | Create MessageService.java under package com.finddevguides.test as explained below. |
7 | Create MessageServiceImpl.java under package com.finddevguides.test as explained below. |
8 | Create UserData.java under package com.finddevguides.test as explained below. |
9 | Modify home.xhtml as explained below. Keep the rest of the files unchanged. |
10 | Compile and run the application to make sure the business logic is working as per the requirements. |
11 | Finally, build the application in the form of war file and deploy it in Apache Tomcat Webserver. |
12 | Launch your web application using appropriate URL as explained below in the last step. |
pom.xml
faces-config.xml
web.xml
applicationContext.xml
MessageService.java
MessageServiceImpl.java
UserData.java
home.xhtml
すべての変更を完了したら、JSF-最初のアプリケーションの章で行ったようにアプリケーションをコンパイルして実行します。 すべてがアプリケーションで問題ない場合、次の結果が生成されます。