jackyrong

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

  整合spring和hibernate的三种方式,小结之.

 

1) 在struts中使用webapplicationcontext调用spring
    声明web.xml,声明一个contextloadlistener,让在启动时执行该listener,读spring的配置文件

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

  再增加一个contextConfigLocation
   <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext*.xml</param-value>
 </context-param>

 WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

  UserBean userbean=(UserBean)ctx.getBean("userbean");

 

2) 将struts的action托管给spring
       这也是很经常用的.用法是
    在struts-config.xml中,加载contextloaderplugin插件,加载spring配置

 

<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />


  <plug-in
  className="org.springframework.web.struts.ContextLoaderPlugIn">
  <set-property property="contextConfigLocation"
  value="/WEB-INF/applicationContext.xml" />
  </plug-in>

  这样的话,把struts的action完全托管给spring了,在struts-config.xml中

    <action path="/user/"..>,这里不需要再用class了,

但在application-context.xml中,则要有
   <bean name="/user"..../>了.

 

3 继承spring的actionsupport类
      比如

   public class aaaa extends DispatchActionSupport

 {

        ......

 

        WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

  UserBean userbean=(UserBean)ctx.getBean("userbean");

 

 

 

   }
   

 


   

posted on 2008-08-27 21:22  jackyrong的世界  阅读(559)  评论(0编辑  收藏  举报