转载:小电影: 用 MyEclipse 开发 Spring + Struts 的总结与操作视频(7分钟)

本文详细介绍了如何将Spring框架整合到Struts应用中,包括所需jar包配置、Struts配置文件修改、使用Spring管理Struts Action及依赖注入等内容。
原文: http://www.blogjava.net/beansoft/archive/2007/08/06/134571.html

http://beansoft.java-cn.org/download/Spring_Struts.swf
代码: 不完整版, 不带 Struts JAR 包, 只有 src 和 jsp 以及 MyEclipse 项目文件.
http://beansoft.java-cn.org/download/Spring_Struts.zip

 


Spring 整合 Strus 要点
beansoft@126.com
2007-8-5

1. 项目需要有 Struts 包和 Spring 的 core, aop, web 三个包(注意不是 Spring 自己的 Web MVC), 具体的 .classpath 文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
 <classpathentry kind="src" path="src"/>
 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
 <classpathentry kind="con" path="com.genuitec.eclipse.j2eedt.core.J2EE14_CONTAINER"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/antlr.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-beanutils.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-digester.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-fileupload.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-validator.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jakarta-oro.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/struts.jar"/>
 <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-3.1.11-bin.jar"/>
 <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.springframework.MYECLIPSE_SPRING20_CORE"/>
 <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.springframework.MYECLIPSE_SPRING20_AOP"/>
 <classpathentry kind="con" path="melibrary.com.genuitec.eclipse.springframework.MYECLIPSE_SPRING20_WEB"/>
 <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath>

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

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


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

 

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

3. 修改 Struts 的 struts-config.xml 中的Action配置

原:

<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="com.test.struts.action.LoginAction" />

改为:

<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
path="/login"
scope="request"
validate="true"
type="org.springframework.web.struts.DelegatingActionProxy" />

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


4. 配置spring 来实例化上一步中被删除的 Action 类.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<bean name="/login" class="com.test.struts.action.LoginAction"></bean>

</beans>

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

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

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

com.test.struts.action.LoginAction

private UserManager userManager;

public UserManager getUserManager()...
public void setUserManager(UserManager userMgmr) ...

public ActionForward execute(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response) {
 System.out.println("userManager=" + getUserManager());
 getUserManager().someBusinessMethods();
 .....
}
然后就可以配置:

<bean name="/login" class="com.test.struts.action.LoginAction">
  <property name="userManager">
   <ref bean="userManagerBean" />
  </property>
</bean>

<bean id="userManagerBean" class="manager.UserManager" />

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

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值