版权声明:本文为博主chszs的原创文章,未经博主允许不得转载。 https://blog.csdn.net/chszs/article/details/1769864
在Spring Beans中的JSF
http://blog.rainer.eschen.name/2007/08/21/jsf-on-spring-beans/
以前,我写过在混合依赖注入环境中怎样使用backing beans。我的blog目录描述了怎样在JSF和Spring中写POJOs,以及在基于JSF的表现层使用它们。但是,Web容器的配置有时是missing的。
1)Configuration(配置)
其配置是非常简单的。我们需要一个指定的listener,叫RequestContextListener,和一个resolver,名为DelegatingVariableResolver。两者都来自Spring的配置,当backing bean识别符被映射到一个bean时,起到调派(dispatching)作用。这个配置允许把所有指定的JSF定义移动到Spring context。
管理backing beans更好的方法时把应用程序上下文(application context)文件分成多个文件。要识别上下文,需要用“spring-”给文件名加上前缀。
下面是web.xml的代码,在packages有一些beans,“security”,“commons”,“context”。
<
context-param
>
<
param-name
>
contextConfigLocation
</
param-name
>
<
param-value
>
/WEB-INF/spring-security.xml,
/WEB-INF/spring-commons.xml,
/WEB-INF/spring-context.xml
</
param-value
>
</
context-param
>

<
listener
>
<
listener-class
>
org.springframework.web.context.ContextLoaderListener
</
listener-class
>
</
listener
>

<
listener
>
<
listener-class
>
org.springframework.web.context.request.RequestContextListener
</
listener-class
>
</
listener
>
下面是faces-config.xml文件:
<
application
>
<
variable-resolver
>
org.springframework.web.jsf.DelegatingVariableResolver
</
variable-resolver
>
</
application
>
2)Maven建立
如果使用Maven2,你必须增加一些Spring的依赖。依赖于你使用的其它框架,如Acegi,你还可以增加更多的依赖。如下:
<
dependency
>
<
groupId
>
org.springframework
</
groupId
>
<
artifactId
>
spring
</
artifactId
>
<
version
>
2.0.6
</
version
>
</
dependency
>

<
dependency
>
<
groupId
>
cglib
</
groupId
>
<
artifactId
>
cglib
</
artifactId
>
<
version
>
2.1_3
</
version
>
</
dependency
>

<
dependency
>
<
groupId
>
asm
</
groupId
>
<
artifactId
>
asm
</
artifactId
>
<
version
>
1.5.3
</
version
>
</
dependency
>

<
dependency
>
<
groupId
>
asm
</
groupId
>
<
artifactId
>
asm-attrs
</
artifactId
>
<
version
>
1.5.3
</
version
>
</
dependency
>