Spring与Struts的整合总结
来源:互联网 发布:中国宏观经济数据网 编辑:程序博客网 时间:2024/05/16 00:37
虽然使用Spring可以实现Srtuts所提供的功能,但由于市场上熟悉Struts的开发人员还是很多的,所以Spring也提供了对Struts的支持,开发人员可以使用Struts代替Spring的MVC实现。下面就Spring与Struts的整合的几种实现方式进行总结。
Spring与Struts的整合,主要有三种方式 :
l 通过Spring的ActionSupport类
l 通过Spring的DelegatingRequestProccessor
l 通过Spring的DelegatingActionProxy
不管Spring用什么方式实现与Struts的整合,那么Strtus到底以什么样的方式装载Spring的应用环境呢?方法就是用使用Sping提供的org.springframework.web.strtus.ContextLoaderPlugin,在Struts的配置文件中注册ContextLoaderPlugin插件即可.代码如下:
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
1. 通过Spring的ActionSupport类
1.1因为要用到Struts的MVC实
现,所以org.apache.struts.action.ActionServlet是一定会用到的.所以,基于这几种整合方式,WEB-INF下的web.xml还是不用更改的,(跟我们在以前不用Spring时的配置是一样的)
<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>
</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>
</servlet>
1.2.修改以前的的Action类,使其extends 自org.springframework.web.strtus.ActionSupport类
1.3在Action的execute方法中写Spring的代码以得到ApplicationContext,并跟据Spring配置文件中配置的Bean(依赖注入的方式)
ApplicationContext context = this.getWebApplicationContext();
HelloWorldService service = (HelloWorldService)
context.getBean("helloWorldService");
HelloWorldService service = (HelloWorldService)
context.getBean("helloWorldService");
注:这里我没有结合我的程序的业务进行讲解,你只需知道其中的原理然后自己通过实例来巩固一下!
总结:这样简单的两步就可以把Spring与Struts整合到一起了,但缺点是很明显的,这种方式使Spring与Struts的代码耦合在一起,并且Struts不在Spring的控制之中,这样如果更换框架,或是想使用Spring的AOP都是比较困难的,如果多个动作放在一个Actin中,则这种方式就无能为力了!
2. 通过Spring的DelegatingRequestProccessor
跟方式1相比,需要改动的代码处有几种,
2.1.在Struts中,担任控制器角色的是ActionServlet,当有讲求发送至ActionServlet时,ActionServlet的doGet()或doPost()方法会执行RequestProccessor的process()方法.
修改Struts的配置文件strtus-config.xml,还是像第一种方式那样注册Spring的ContextLoaderPlugIn插件,并使用Spring的DegatingRequestProcessor 代替Struts的RequestProcessor
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor"></controller>
<!-- 注册插件 -->
<message-resources parameter="com.struts.ApplicationResources" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
<!-- 注册插件 -->
<message-resources parameter="com.struts.ApplicationResources" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/applicationContext.xml" />
</plug-in>
2.2.将Struts中XXXAction 的超类还原成org.apache.struts.action.Action
2.3.在用户自定义的XXXAction类中没有了Spring的得到Bean的代码,那么我们怎么
与我们业务层取得联系呢?这里就可以通过依赖注入的方式,如:接口注入,setter注入,构造注入。
小结:这种方法就不需要在Action里编写Spring的代码了,这种方式就解决了使用ActionSupport时两种框架代码耦合的缺点.,但这种方式还是有缺点的,那就是开发人员可以自己定义RequestProcessor,这样如果没有使用Struts默认的RequestProcessor,则需要手工来整合Spring和Struts了.
3.通过Spring的DelegatingActionProxy
3.1.同上两种方式,使用DelegatingActionProxy方式,WEB-INF下的web.xml还是跟往常 做单一的Struts程序一样,另外还是加入ContextLoaderPlugIn插件
3.2.将用户自定义的XXXAction类还是继承自org.apache.struts.action.Action
3.3修改struts-config.xml文件,将<action-mapping/>节点里的用户<action>的class属性修改成:org.springframework.web.srtuts.DelegatingActionProxy
<action-mappings>
<action attribute="loginForm" input="/login.jsp"
name="loginForm" path="/login" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward path="/welcome.jsp" />
<forward path="/login.jsp" />
</action>
</action-mappings>
<action attribute="loginForm" input="/login.jsp"
name="loginForm" path="/login" scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward path="/welcome.jsp" />
<forward path="/login.jsp" />
</action>
</action-mappings>
总结:以上就Spring与Struts整合的三种方式进行了简单的讲解,由于代码量比较大,我并没有结合完整的程序进行讲解.如有任何疑问,请在博客里留言,我会尽快给予解释.
- Spring与Struts的整合总结
- Struts与Spring的整合
- struts与spring的整合
- struts与spring的整合
- spring与struts的整合
- spring与struts的整合
- 整合Struts 与Spring
- 整合Struts 与Spring
- 整合Struts 与Spring
- struts与spring整合
- spring与struts整合
- Spring与Struts整合
- Spring与Struts整合
- Spring与struts整合
- Spring与Struts整合
- spring 与struts 整合
- struts与spring整合
- Spring与Struts整合
- Programming Embedded Systems with C and GNU Development Tools, 2nd Edition (OReilly, 2006)的读书终于完成
- 搜索引擎中网络爬虫的设计分析::[Source]
- parent,::,$this,self的分别
- How to recognise a good programmer
- 深入Struts2
- Spring与Struts的整合总结
- 从菜鸟出发!征服高清详细评测全攻略分页索引
- 面向对象软件的测试
- 如此好男人
- 互联网公司赢利模式典型总结
- 自己写的一个JSP上传文件和下载文件的JavaBean 自己写的一个JSP上传文件和下载文件的JavaBean
- 女孩的第二次
- Asp.Net 2.0中的客户端脚本
- 读书终于完成2