1.业务Action类继承ActionSupport类
业务Action中可以调用ActionSupport类中的getWebApplicationContext(),来获得wac,然后
调用wac.getBean("myservice")来获得需要的service对象
这种方法虽然可以,但是不推荐使用,原因如下:
1>业务Action类跟Spring的ActionSupport类藕合在一起不是很好
2>getWebApplicationContext().getBean("myservice")这种硬编码不是很好
3>这种方法Spring并没有托管Action对象
2.使用Spring的DelegatingRequestProcessor类来替代struts的RequestProcessor类
struts-config.xml
<controller locale= "true" processorClass= "org.springframework.web.struts.DelegatingRequestProcessor"> </controller>
DelegatingRequestProcessor类继承了RequestProcessor类,并重写了RequestProcessor的一些方法
DelegatingRequestProcessor.java
public class DelegatingRequestProcessor extends RequestProcessor { private WebApplicationContext webApplicationContext; public void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException { super.init(actionServlet, moduleConfig); if (actionServlet != null) { this.webApplicationContext = initWebApplicationContext(actionServlet, moduleConfig); } } protected WebApplicationContext initWebApplicationContext( ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException { return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig); } protected final WebApplicationContext getWebApplicationContext() { return this.webApplicationContext; } protected Action processActionCreate( HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws IOException { Action action = getDelegateAction(mapping); if (action != null) { return action; } return super.processActionCreate(request, response, mapping); } protected Action getDelegateAction(ActionMapping mapping) throws BeansException { String beanName = determineActionBeanName(mapping); if (!getWebApplicationContext().containsBean(beanName)) { return null; } return (Action) getWebApplicationContext().getBean(beanName, Action.class); } protected String determineActionBeanName(ActionMapping mapping) { return DelegatingActionUtils.determineActionBeanName(mapping); } }
1>调用init方法的时候,初始化并获得wac
2>调用processActionCreate方法的时候,先从wac中getBean("prefix+path"),如果找不到再从struts内部获取
3.所有的业务Action都配置成DelegatingActionProxy类
struts-config.xml
<action-mappings> <action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="userForm" scope="request"> <forward name="success" path="/WEB-INF/jsp/index.jsp"/> </action> </action-mappings>
DelegatingActionProxy.java
public class DelegatingActionProxy extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Action delegateAction = getDelegateAction(mapping); return delegateAction.execute(mapping, form, request, response); } protected Action getDelegateAction(ActionMapping mapping) throws BeansException { WebApplicationContext wac = getWebApplicationContext(getServlet(), mapping.getModuleConfig()); String beanName = determineActionBeanName(mapping); return (Action) wac.getBean(beanName, Action.class); } protected WebApplicationContext getWebApplicationContext( ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException { return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet, moduleConfig); } protected String determineActionBeanName(ActionMapping mapping) { return DelegatingActionUtils.determineActionBeanName(mapping); } }
DelegatingActionProxy重写了execute方法,当struts调用execute方法的时候,内部会调用wac.getBean("prefix+path")获取被Spring托管的Action对象,然后再调用业务Action对象的execute方法,其实就是利用了代理模式对Action进行代理
Spring的配置文件:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="loginService" class="prd.tidy.test.service.LoginService"> </bean> <bean name="/login" class="prd.tidy.test.action.LoginAction" scope="prototype"> <property name="loginService" ref="loginService"> </property> </bean> </beans>
相关推荐
12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java 12.6.5 编写登录控制器LoginAction.java 12.6.6 建立...
本文并不想介绍Struts,Spring,Hibernate的原理系统架构等,本文地目的是通过一个较复杂地实例介绍如何整合Struts,Spring,Hibernate,网上现有的例子虽然也能达到目的,但功能都比较单一,复杂的例子时会有...
比较流行的一本书,在公司内很受欢迎 比较实用 下半部分,需与上半部分放在一起解压。
Spring+Struts+Hibernate比较详细的整合配置方案
本文并不想介绍Struts,Spring,Hibernate的原理系统架构等,本文的目的是通过一个较复杂地实例介绍如何整合Struts,Spring,Hibernate,网上很多资料介绍的例子虽然也能达到目的,但功能都比较单一,复杂的例子时会...
SSH是 struts+spring+hibernate的一个集成框架,是目前比较流行的一种Web应用程序开源框架 现在的结构是,Struts负责显示层,Hibernate负责持久层,Spring负责中间的业务层,另外,由于Spring使用的依赖注射以及AOP...
12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java 12.6.5 编写登录控制器LoginAction.java 12.6.6 建立...
12.6.1 Struts、Spring和Hibernate的整合方式 12.6.2 编写用户注册画面regedit.jsp 12.6.3 编写用户登录画面login.jsp 12.6.4 编写注册控制器RegeditAction.java 12.6.5 编写登录控制器LoginAction.java 12.6.6 建立...
struts,spring,hibernate的整合基础,比较简洁明了
struts.spring.hibernate整合教程,该教程来源于网络,比较基础的POJO原理及应用讲解,适合初级的POJO开发人员阅读
基于HT T P 传输协议, 采用Struts2 框架整合Spring 框架技术对Web 中文件的上传下载进 行应用研究. 提出的技术整合方案结合了Struts2 的自身特点, 通过与Str ut1 框架实现模型的分析 比较, 展示了Struts2 在文件上传...
Java项目中通过Eclipse新建的Web项目,常包含Web.xml,通过该配置文件配置项目的必要信息,常做BS结构项目的人对Web.xml比较熟悉。一般情况下建立Web项目时,和Struts中的action请求向结合如下来配置,
struts2+spring整合的一个例子,实现查询功能,不需要数据库,代码很齐全,配置得比较专业,可以作为学习Spring 的很好的参考例子。
这是一个J2EE-Struts2-Spring-Hibernate整合的例子:添加,采用现比较流行的一些技术(struts+hibernate+spring)
比较流行的一本书,在公司内很受欢迎。 比较实用。 分上下两部分,放在一起解压。
Struts 具有如此大的推动力,以至于开发团队宁愿整合 Spring 框架的特性,而不愿意转换成 Spring MVC。没必要进行转换对您来说是一个好消息。Spring 架构允许您将 Struts 作为 Web 框架连接到基于 Spring 的业务和...
这个实际上是PDF格式 没有例子 但是里面说的比较详细 而且有代码说明。
应用于struts1.2,hibernate3.2,spring2.5的整合总结,比较全面
struts2.1.8 + spring2.5.6 + hibernate3.3.2整合,可以看一下,比较完全
struts spring hibernate 优缺点分析.docx