深入探讨 Spring 与 Struts 的集成方案(3)
来源:互联网 发布:淘宝助理没有下载宝贝 编辑:程序博客网 时间:2024/06/06 08:23
4.实现第二种集成方案:代理和委托Action.
private CourceService courceService;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Set allCources = courceService.getAllCources();
request.setAttribute("cources", allCources);
//..........the other statements.
return mapping.findForward("jspView");
}
/* 注入CourceService */
public void setCourceService(CourceService courceService) {
this.courceService = courceService;
}} 分析:大家可以看到,在这种方式之下,我们的Struts Action类和Spring是低耦合的,它仅仅依赖了Spring提供的反向控制(IoC)机制把CourceService注入到了我们的Action中.到此,大家肯定会有一个疑问:那就是Spring到底是如何提供IoC反向控制的呢?回答这个问题,我们需要完成两个步骤的配置: (1).在struts-config.xml中注册Struts Action.但要注意的是我们在这里注册的是代理Action.幸运的是,我们不必亲自编写这个类.因为Spring已经通过org.springframework.web.struts.DelegatingActionProxy提供了这个代理的Action.具体的配置方法如下:
< action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses">
(2)将真正的Struts Action作为一个Spring Bean并在Spring上下文配置文件中作为一个Bean注册之.并将Action所要引用的courceService注入给它.
< bean class="com.eRedCIP.web.CourceAction" name="/listCourses">
< property name="">
< ref bean="courseService">
< /property>
< /bean> 注意:name属性的值是非常重要的,它必须和struts-config.xml中的path属性完全一致.这是因为DelegatingActionProxy会使用path属性值在Spring上下文中查找真正的Action.使用DelegatingActionProxy的好处在于我们可以不使用任何Spring特定的类来编写Struts Action.同时,Struts动作能够利用IoC取得和他合作的对象.唯一不足之处就是不太直观,配置相对复杂.为了使action委托显得更为直观一些,我们可对这种集成方案做进一步的改进:使用请求委托.
这种集成方案要求我们编写一个Struts Action,但它只不过是一个包含在Spring应用上下文中的真正Struts Action的一个代理.该代理Action从Struts插件ContextLoaderPlugIn中获取应用上下文,从中查找真正的Struts Action,然后将处理委托给真正的Struts Action.这个方法的幽雅之处在于:只有代理action才会包含Spring特定的处理.真正的Action可以作为org.apache.struts.Action的子类来编写.
下面我们来看一段在之中集成方式下的Struts Action源代码:
public class CourceAction extends Action {private CourceService courceService;
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Set allCources = courceService.getAllCources();
request.setAttribute("cources", allCources);
//..........the other statements.
return mapping.findForward("jspView");
}
/* 注入CourceService */
public void setCourceService(CourceService courceService) {
this.courceService = courceService;
}} 分析:大家可以看到,在这种方式之下,我们的Struts Action类和Spring是低耦合的,它仅仅依赖了Spring提供的反向控制(IoC)机制把CourceService注入到了我们的Action中.到此,大家肯定会有一个疑问:那就是Spring到底是如何提供IoC反向控制的呢?回答这个问题,我们需要完成两个步骤的配置: (1).在struts-config.xml中注册Struts Action.但要注意的是我们在这里注册的是代理Action.幸运的是,我们不必亲自编写这个类.因为Spring已经通过org.springframework.web.struts.DelegatingActionProxy提供了这个代理的Action.具体的配置方法如下:
< action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses">
(2)将真正的Struts Action作为一个Spring Bean并在Spring上下文配置文件中作为一个Bean注册之.并将Action所要引用的courceService注入给它.
< bean class="com.eRedCIP.web.CourceAction" name="/listCourses">
< property name="">
< ref bean="courseService">
< /property>
< /bean> 注意:name属性的值是非常重要的,它必须和struts-config.xml中的path属性完全一致.这是因为DelegatingActionProxy会使用path属性值在Spring上下文中查找真正的Action.使用DelegatingActionProxy的好处在于我们可以不使用任何Spring特定的类来编写Struts Action.同时,Struts动作能够利用IoC取得和他合作的对象.唯一不足之处就是不太直观,配置相对复杂.为了使action委托显得更为直观一些,我们可对这种集成方案做进一步的改进:使用请求委托.
- 深入探讨Spring与Struts的集成方案深入探讨Spring与Struts的集成方案
- 深入探讨 Spring 与 Struts 的集成方案(3)
- 深入探讨Spring与Struts集成方案
- 深入探讨Spring与Struts集成方案
- 深入探讨Spring与Struts的集成方案
- 深入探讨 Spring 与 Struts 的集成方案
- 深入探讨 Spring 与 Struts 的集成方案(1)
- 深入探讨 Spring 与 Struts 的集成方案(2)
- 深入探讨 Spring 与 Struts 的集成方案(4)
- 深入探讨 Spring 与 Struts 的集成方案
- 深入探讨 Spring 与 Struts 的集成方案
- 深入探讨Spring与Struts的集成方案(转)
- 深入探讨Spring与Struts的集成方案
- 深入探讨 Spring 与 Struts 的集成方案
- 深入探讨 Spring 与 Struts 的集成方案
- 深入探讨 Spring 与 Struts 的集成方案
- 探讨Spring与Struts 的集成方案
- 实例探讨 Spring 与 Struts 的集成方案[转]
- 程序员网站资源宝库
- 比尔盖茨名言录
- Control Study -> Asp.net几个较复杂的显示数据控件(Repeater/DataList/DataGrid)用法 (示例代码下载)
- Control Study -> 检索浏览器性能
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- 深入探讨 Spring 与 Struts 的集成方案(3)
- 演示Session对象使用 范例(示例代码下载)
- 分别用(Windows/Form/Soap/WebService)实现用户身份验证(含示例代码下载)
- wap(Wireless Application Protocol)入门部署设置及整理搜集了好多好多资料
- 半夜听到黄家驹的声音, 忽醒,一身冷汗,哦,原来是计算机忘了关,还在播放Beyond演唱会.于是做了个较简单的自动关机程序(代码下载)
- 系统多种资源文件代码示例(例如一个系统同时具有中文简体,中文繁体,英文,日文等不同资源文件)(示例代码下载)
- (八)线程--借助 封装类 实现“线程调用带参方法”(示例下载)
- 深入探讨 Spring 与 Struts 的集成方案(4)
- (七)线程--管理线程(使线程中止,暂停,挂起等)(示例下载)