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]

posted @ 2017-08-29 14:41  码上猿梦  阅读(1097)  评论(0编辑  收藏  举报