`
wsql
  • 浏览: 11794712 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

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

 
阅读更多

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 配置文件出错导致的.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics