spring 与 struct2整合

  背景:   

      Structs2中的Action不再依赖于Http协议,进一步说就是不再依赖于表示层中的request,response等http协议中的对象了,这一改进让我们的Action可以是一个普通的JavaBean或叫POJO类;其次,它在配置文件的配置上简化了很多,配置很方便;再次就是,它是一个基于组件的框架,可整合JSF,Tapestry,DWR,FreeMark,JFreeChart,Spring等等,体现了它强大的可扩展性。
   那么,我现在谈下Spring与Structs2的整合。
   其整合中最重要的原理就是,我们的Structs2在默认情况下是由自己创建Action对象,进行请求处理和业务调度的。可是SSH整合的项目中,我们都知道,后台的业务处理和DAO都是由Spring进行横向管理的。这意味着,我们的请求要想得到后台业务对象的相关服务,就必须和Spring扯上关系,那就是把Structs2里面创建Action的权利交给Spring。

 步骤:
    第一,我们首先要把Structs2与Spring整合的支持架包导入工程,加入struts2-spring-plugin-2.0.9.jar到lib目录.
    第二,我们通过配置web.xml来通知工程在启动时候装载Spring上下文,具体配置如下:
在web.xml中添加如下配置。

<!-- Struts2配置 -->

<!-配置Structs2的中心控制器-->

<filter>

<filter-name>struts2</filter-name>

<filter-class>

org.apache.struts2.dispatcher.FilterDispatcher

</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!--设置上下文本地配置文件的路径-->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/classes/applicationContext.xml

</param-value>

</context-param>

<!-- 装载Spring上下文 -->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener> 


    第三,在struts.properties中加入: struts.objectFactory = spring或在struts.xml中加入: <constant name="struts.objectFactory" value="spring" />告知Structs2的Action由Spring创建和管理。
    第四,在Spring配置文件applicationContext.xml中加入相应Action的<bean />,
如:Structs2中有个Action为LoginAction,此Action的name属性为name="login",那么在Spring的配置文件的配置就是:
<bean id="login" class="org.struts2.action.LoginAction"></bean>
我们就可以在上面的bean中由Spring注入相应的业务对象了。     Ok,这样,Structs2就和Spring整合了。

你可能感兴趣的:(spring 与 struct2整合)