struts_spring

struts于spring整合方案:
 思路:
  1.让web工程启动的时候,直接初始化spring容器
  2.action对象池归属问题,应该由谁来进行处理
 步骤:
  1.引入struts能力,导入相关包
  2.引入spring能力,导入相关包:spring 3.0 core libraries,spring 3.0 web libraries(用于启动容器的listener在其中)
  3.(一)把spring提供的用于进行spring初始化的listener配置进入web.xml
   目的:让web容器初始化的时候直接加载spring容器
   在web.xml中加入:
     <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
     <context-param>
        <param-name>contextConfigLocation</param-name>
         <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
     </context-param>
   (二)在struts-config.xml中初始化spring容器
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
      <set-property property="contextConfigLocation"
          value="/WEB-INF/classes/applicationContext.xml"/>
    </plug-in>
  4.spring对action的处理有两种解决方案:
   方案一:
    在struts-config.xml中:
     action的整套配置不变
      <action-mappings>
       <action path="/a" type="action.AAction" ></action>
      </action-mappings>
     加入controllor屏蔽struts自身的action对象池(位置要求:在action-mappings标签和message-resources标签之间)
      <!-- 屏蔽了struts自身的Action对象池,当去的Action对象的时候.通过controllor逻辑,自动到spring对象池中查找对象 -->
         <controller>
          <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
         </controller>
      在spring配置文件applicationContext.xml中:
       <!-- 以下为service配置 -->
     <bean id="aService" class="service.AServiceImple"></bean>

     <!-- 以下为action配置,属性名为name,而不是id name = struts-config.xml中的path属性值-->
     <bean name="/a" class="action.AAction">
      <property name="aService" ref="aService"/>
     </bean>
   方案二:
    在struts-config.xml中:
     <!-- 所有的请求均交给spring提供的DelegatingActionProxy类.此类根据用户的请求名称xx.do,到spring容器中找对应的Action,调用其execute方法 -->
     <action path="/a" type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="success" path="success.jsp"></forward>
     </action>
    在spring配置文件applicationContext.xml中:
     <bean name="/a" class="action.AAction"></bean>
   方案三(不建议):
    在Action类中:
     WebApplicationContext wac = (WebApplicationContext) request.getSession().getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
     AService aService = (AService) wac.getBean("aService");
     aService.aMethod();

转载于:https://www.cnblogs.com/liaren/archive/2012/11/19/struts_spring.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值