五步搞定Spring整合Strus

开发 后端
本文向您介绍Spirng整合Strus的五个通用步骤,包括对配置文件的修改,Action的设置和测试等.

1. 项目需要有Struts包和Spring的core, aop, web 三个包(注意不是Spring自己的Web MVC), 将Spring整合Strus。具体的 .classpath 文件如下所示:

  1. ﹤?xml version="1.0" encoding="UTF-8"?﹥  
  2. ﹤classpath﹥  
  3.     ﹤classpathentry kind="src" path="src"/﹥  
  4.     ﹤classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/﹥  
  5.     ﹤classpathentry kind="con" path="com.genuitec.core.J2EE14_CONTAINER"/﹥  
  6.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr.jar"/﹥  
  7.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/beanutils.jar"/﹥  
  8.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/digester.jar"/﹥  
  9.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/fileupload.jar"/﹥  
  10.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/logging.jar"/﹥  
  11.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/validator.jar"/﹥  
  12.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jakarta-oro.jar"/﹥  
  13.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts.jar"/﹥  
  14.     ﹤classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java--bin.jar"/﹥  
  15.     ﹤classpathentry kind="con" path="melibrary.com.MYECLIPSE_SPRING20_CORE"/﹥  
  16.     ﹤classpathentry kind="con" path="melibrary.com.MYECLIPSE_SPRING20_AOP"/﹥  
  17.     ﹤classpathentry kind="con" path="melibrary.com.MYECLIPSE_SPRING20_WEB"/﹥  
  18.     ﹤classpathentry kind="output" path="WebRoot/WEB-INF/classes"/﹥  
  19. ﹤/classpath﹥  

2. 对Struts 配置文件做修改加入Spring 托管功能.

创建 Spring 配置文件,将文件放到src 目录下,文件名称为 applicationContext.xml, 编译后放到WEB-INF/classes/ 下.

配置struts-config.xml文件,添加 spring的插件, 位置在 struts-config 文件的最末尾.

  1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> 
  2. <set-property property="contextConfigLocation" 
  3. value="/WEB-INF/classes/applicationContext.xml" /> 
  4. plug-in>  

3. 修改 Struts 的 struts-config.xml 中的Action配置。原来的代码:

  1. <action 
  2. attribute="loginForm" 
  3. input="/login.jsp" 
  4. name="loginForm" 
  5. path="/login" 
  6. scope="request" 
  7. validate="true" 
  8. type="com.test.struts.action.LoginAction" />  

改变后的代码:

  1. <action 
  2. attribute="loginForm" 
  3. input="/login.jsp" 
  4. name="loginForm" 
  5. path="/login" 
  6. scope="request" 
  7. validate="true" 
  8. type="org.springframework.web.struts.DelegatingActionProxy" />  

type 部份为修改内容, 这里将使用spring的代理器来对Action进行控制. 当提交到/login.do是将控制权交给了spring,然后由spring来把它转回到struts的Action.

4. Spring整合Strus,需要配置spring来实例化上一步中被删除的 Action 类.

  1. xml version="1.0" encoding="UTF-8"?> 
  2. >   
  3.  
  4. <beans> 
  5. <bean name="/login" class="com.test.struts.action.LoginAction">bean>   
  6.  
  7. beans>   

Spring 通过 org.springframework.web.struts.DelegatingActionProxy 这个类, 然后根据 Struts 配置文件中的 和 Spring 配置文件中的 来将 Spring 管理下的 Struts Action 类和提交的路径匹配起来, 这些就是关于转交控制权的配置内容.

实践的过程中发现必须把 singleton="false"去掉才行, 否则就会无法初始化 Spring 框架, 不知道具体的原因是什么.

既然这个 Struts的Action 已经通过 Spring 来初始化, 所以就可以加入依赖注入, 整合Hibernate 的功能了. 例如典型的情况:

  1. com.test.struts.action.LoginAction   
  2.  
  3. private UserManager userManager;   
  4.  
  5. public UserManager getUserManager()...  
  6. public void setUserManager(UserManager userMgmr) ...   
  7.  
  8. public ActionForward execute(ActionMapping mapping, 
  9. ActionForm form,  
  10.     HttpServletRequest request, HttpServletResponse response) {  
  11.     System.out.println("userManager=" + getUserManager());  
  12.     getUserManager().someBusinessMethods();  
  13.     .....  
  14. }  

然后就可以配置:

  1. <bean name="/login" class="com.test.struts.action.LoginAction"> 
  2.   <property name="userManager"> 
  3.    <ref bean="userManagerBean" /> 
  4.   property> 
  5. bean>   
  6.  
  7. <bean id="userManagerBean" class="manager.UserManager" />   

同理 Spring整合Hibernate 和没Struts 的时候一样, 也可以在这个文件中进行配置即可.

5. Spring整合Strus最后一步, 测试, 只要能打印出来 userManager 不为空, 就说明整合成功了. 如果出现 404 action servlet 不可用的错误, 一般都是 Spring 配置文件出错导致的.

 

【编辑推荐】

  1. 实战解析:如何整合iBatis和Spring
  2. Struts2.0+Springframework2.5+ibatis2.3完美整合实例
  3. WebWork与Spring集成方法浅析
  4. Spring集成Struts方法简述
  5. 浅谈jBPM4与Spring整合的2种方式
责任编辑:佚名 来源: BlogJava
相关推荐

2015-03-10 15:51:23

公有云亚马逊CloudSeaCloudSearch

2010-03-31 10:24:15

服务器虚拟化

2023-08-01 19:16:01

Spring编程浏览器

2016-09-09 01:07:06

数据中心容量规划数据中心

2009-08-18 14:15:12

2017-08-22 18:34:24

WinLicense软件授权

2014-08-11 10:10:39

linux

2023-09-21 22:50:15

KubernetesSpring

2021-07-26 09:35:26

SQL数据库优化

2017-01-06 08:47:53

2009-12-11 13:31:31

策略路由配置

2016-12-14 09:03:34

springhibernate异常

2021-11-10 11:37:48

Spring整合 Mybatis

2009-06-18 15:24:08

Spring OSGi

2022-02-12 11:00:33

FTP网络协议文件传输

2009-06-19 10:00:37

Struts和Spri

2009-11-05 10:01:26

Visual Stud

2010-10-22 11:31:53

SQL Server自

2009-07-14 16:55:32

MyEclipse S

2009-06-01 10:28:03

SpringOSGi整合
点赞
收藏

51CTO技术栈公众号