zoukankan      html  css  js  c++  java
  • spring 整合 Struts1.X [转]

    这篇博客摘自[http://blog.csdn.net/chendc201/article/details/8464008],

    其中也有一些是自己增加的部分 .

    第一步, 需要为 Struts 装载 Spring 应用上下文环境。有以下三种方式:

    1) 在 struts-config.xml 中使用 Struts Plugin

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

    2) 在 web.xml 中使用 ContextLoaderListener

    <context-param> 
          <param-name>contextConfigLocation</param-name> 
          <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
    </context-param> 
    <listener> 
          <listener-class> 
               org.springframework.web.context.ContextLoaderListener 
          </listener-class> 
    </listener>

    3) 在 web.xml 中使用 ContextLoaderServlet

    <context-param> 
          <param-name>contextConfigLocation</param-name> 
          <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
    </context-param> 
    <servlet> 
         <servlet-name>SpringContextServlet</servlet-name> 
         <servlet-class> 
             org.springframework.web.context.ContextLoaderServlet 
         </servlet-class> 
         <load-on-startup>1</load-on-startup> 
    </servlet>

    需要注意的是:

           使用 Struts PlugIn 的方式加载 Spring 配置文件有可能导致 DWR 无法取得 Spring 中定义的 bean,因为 DWR 有可能先于 Struts 被访问使用,而此时 Struts 配置文件还未加载!因此,在 Spring、Struts 和 DWR 集成时,应该在 web.xml 中通过 ContextLoaderLisenter 或 ContextLoaderServlet 加载 Spring 配置文件。

          最佳实践是使用 Struts PlugIn 的方式加载 Struts Action 配置文件 /WEB-INF/action-servlet.xml,而使用 ContextLoaderLisenter 或 ContextLoaderServlet 方式加载 Spring 配置文件 applicationContext.xml,通过两次加载完成 Spring所有配置文件的加载。

    第二步, 整合Struts1 和 Spring, 也就是管理 action 的 method

    使用 DelegatingActionProxy 将 Struts Action 管理权委托给 Spring

    <form-beans>
              <form-bean name="loginForm" type="com.xxx.yyy.struts.LoginForm" />
              <!-- 省略很多-->
    </form-beans>
    <action-mappings>
        <action
              attribute="loginForm"
              name="loginForm"
              parameter="method"
              path="/login"
              scope="request"
              type="org.springframework.web.struts.DelegatingActionProxy">
              <forward name="login" path="/login.jsp"/>
              <forward name="main" path="/main.jsp"/>
        <!-- 省略很多-->
        </action>
    </action-mappings>

          上面的 loginForm 我理解为一个 POJO , 和数据库中映射出来的实体类有一定的区别, formbean具有的属性会比较少, 用于封装表单数据, 便于传递 .

    对应的在 Spring 配置文件中应该就有 action 的实现类, 注意 name 属性, 是带有斜杠的, 对应 action 配置的 path 属性.

    <bean name="/login" class="com.xxx.yyy.struts.LoginAction">
            <property name="xxxService">
                <ref bean="xxxService"/>
            </property>
            <property name="yyyService">
                <ref bean="yyyService"/>
            </property>
            <property name="zzzService">
                <ref bean="zzzService"/>
            </property>
    </bean>

          action 中 method 的编写和 Struts2 差不多, 只不过不是返回一个逻辑视图的 String , 而是一个包含了 String 的 ActionForward对象, 道理还是一样的 , 也就是逻辑视图 , 然后再去对应的 action 配置中寻找物理视图.

    public class LoginAction extends BaseAction {
          public ActionForward login(ActionMapping mapping, ActionForm form,
                      HttpServletRequest request, HttpServletResponse response){
                //处理逻辑
    
               return mapping.findForward("main");
          }
    }

    上面的 BaseAction继承自 Struts 核心控制器 DispatchAction, 并且注入必须的业务层的 bean.

     

     

    转自[http://blog.csdn.net/chendc201/article/details/8464008]

  • 相关阅读:
    Oracle体系结构详细图解
    了解Oracle体系结构(超详细)
    Oracle 12C DataGuard部署以及维护
    RAC上,对于buffer cache的全局锁,称为PCM锁,当然,对于enq,lc的锁,称为non-PCM锁
    oracle锁与死锁概念,阻塞产生的原因以及解决方案
    Oracle 多粒度锁机制介绍
    Master Note For Oracle Database Upgrades and Migrations (Doc ID 1152016.1)
    Oracle 12c和18c中的MGMTDB(下)
    AIX系统性能管理之Oracle案例分析
    Web安全从入门到“放弃”之暴力破解
  • 原文地址:https://www.cnblogs.com/daimajun/p/7448152.html
Copyright © 2011-2022 走看看