`
zhiblin
  • 浏览: 56907 次
  • 性别: Icon_minigender_1
  • 来自: 厦门
社区版块
存档分类
最新评论

jsf与spring 整合

阅读更多

JSF与Spring整合的原理是获得彼此的上下文引用,以此进一步获得各自管理的bean,这是可能的,因为两者是Web应用框架都遵循Servlet规范,为二者整合提供了可能和基础。

在Spring中ApplicationContext是相当重要的类,对于web应用,它还包装了
Javax.servlet.ServletContext, 为web应用提供了所有可以利用的数据,
包括可管理bean,Faces中通过FacesContext类可以获得所有可以利用的资源,
同样包括JSF的可管理支持bean,它们都围绕着ServletContext提供了自己的
门面,通过各自的门面在Servlet容器的世界里彼此相通。

这边介绍两种整合的方法

一、利用自定义的类

编写一个实用类SpringFacesUtil

 

package com.ceun.util;
 
import org.springFramework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
public final class SpringFacesUtil
...{
    /** *//**
    * 从Spring中查找bean.
    * @param beanName String
    * @return Object
    */
    public static Object findBean(String beanName)...{
       ServletContext context = (ServletContext) FacesContext.getCurrentInstance().
        getExternalContext().getContext();
       ApplicationContext appctx = WebApplicationContextUtils.
       getRequiredWebApplicationContext(context);
       return appctx.getBean(beanName);
    }
}
经过编译后,就可以通过使用这个实用类的finsBean方法来,获得Spring管理的bean了

提示:运行前,确认你已经在应用配置文件(web.xml)中加入如下代码,并拷贝相应的类库存到应用WEB-INF/lib

目录

...
<context-param>
        <param-name>javax.faces.CONFIG_FILES</param-name>
        <param-value>/WEB-INF/faces-config.xml</param-value>
    </context-param>
<!--使用listenner,加载Spring 当然也可用Servlet方式-(具体见Spring相关配置)-->
<listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
<servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
...

 

二、使用第三方框架

这边介绍一种使用jsf-spring框架整合JSF与Spring的方法

1.首先从http://jsf-spring.sourceforge.net/下载jsf-spring类库

因为此例假设整合JSF1.1 Spring 1.2

所以下载jsf-spring-3.0.0.zip,下载解压后拷贝dist目录下的文件到你的应用WEB-INF/lib目录下

2.修改应用配置文件(web.xml)

在刚才添加的加载Spring的listener后面。再添加一个listener

<!--这个一定要插在Spring listener之后-->
<listener>
        <listener-class>
            de.mindmatters.faces.spring.context.ContextLoaderListener
        </listener-class>
    </listener>
3.假设你有个由JSF管理userBean bean,你现在想把userBean中的其中一个属性如userDAO,改成从Spring bean

中获得

那么你可以在faces-config.xml中作如下修改

<managed-bean>
        <managed-bean-name>userBean</managed-bean-name>
        <managed-bean-class>
            com.ceun.bean.UserBean        </managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
        <managed-property>
            <property-name>userDAO</property-name>
<!--通过#{userDAO}引用Spring管理的userDAO bean-->
            <value>#{userDAO}</value>
        </managed-property>
    </managed-bean>
4.重启服务器,在你的应用中使用Spring管理的bean

分享到:
评论
2 楼 zhiblin 2009-03-09  
spring 的那个版本
1 楼 seekgirl 2009-03-09  
spring本身就提供类来整合

<application>
        <variable-resolver>
            org.springframework.web.jsf.DelegatingVariableResolver
        </variable-resolver>
    </application>

相关推荐

Global site tag (gtag.js) - Google Analytics