strust与spring结合

   0.加载struts
   
   在web.xtm中
   <!-- Struts Action Mapping-->
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
 
   1.加载spring应用环境
   在web.xml用
    <!-- Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔此参数用于后面的Spring-Context loader -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:spring/*.xml</param-value>
  <!-- //如果不配置contextConfigLocation,会默认找/WEB-INF/applicationContext.xml文件
  <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-common.xml</param-value>
   -->
 </context-param>
 
 <!-- 配置spring上下文载入器,即Spring ApplicationContext 载入 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 来加载spring的应用环境
  或在struts-config.xml中注册Spring插件
    <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
      <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
    </plug-in>
    来注册Spring插件!
 
 
 2. Struts获得spring的中配置的bean的方式
 (1) 利用WebApplicationContextUtils的getRequiredWebApplicationContext获得spring的应用环境
     ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext);
  Object obj = ctx.getBean("beanName");
  
    (2)继承Spring的ActionSupport,然后用getRequiredWebApplicationContext获得spring的应用环境
        getRequiredWebApplicationContext().getBean("beanName");
   
    3.捆绑Spring中的动作,即用spring处理action,不用ActionSupport
    (1)在struts-config.xml中,用spring的DelegatingRequestProcessor代替struts默认的RequestProcessor
    <action-mappings>
      <action path="/adminLogin" className="org.springframework.web.struts.DelegatingRequestProcessor">
        <forward name="success" path="/pages/adminLogin.jsp"></forward>
      </action>
    </action-mappings>
      然后在applicationContext-action.xml中用
      <bean name="/adminLogin" class="com.company.mypro.AdminLoginAction">
     <property name="adminManager" ref="adminManger"/>
   </bean> 就行了.
     
     
    备注:  struts-config.xml中
    <controller>
  <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
 </controller>
 
 完整的web.xml
 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 
 <!-- Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔此参数用于后面的Spring-Context loader -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:spring/*.xml</param-value>
  <!-- //如果不配置contextConfigLocation,会默认找/WEB-INF/applicationContext.xml文件
  <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-common.xml</param-value>
   -->
 </context-param>
 
 <!-- 配置spring上下文载入器,即Spring ApplicationContext 载入 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 
 <!--  Character Encoding filter -->
 <filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
  </init-param>
 </filter>
 
 <filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>*.do</url-pattern>
 </filter-mapping>
 
 
 
 <!-- Struts Action Mapping-->
 <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml,/WEB-INF/modules/struts-config-admin.xml
   </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>
 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值