- 浏览: 303337 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
ae6623:
ae6623 写道大哥,你是怎么知道它对临时文件有限制的,我也 ...
导出excel2007 poi3.8 -
ae6623:
大哥,你是怎么知道它对临时文件有限制的,我也发现这个bug了, ...
导出excel2007 poi3.8 -
coralandbill:
下载不了啊 能不能给我发一个simpleProj.war包啊 ...
jqgrid使用步骤及说明 -
maojin:
这是jqgrid几?那个电话号码校验的函数能调到吗?
jqgrid使用步骤及说明 -
qingyezhu:
请问,用poi3.8中的wordtohtmlconver类将d ...
导出excel2007 poi3.8
spring2.0与struts1.x 整合三种方法!
2008-09-03 10:20
1. Struts
Struts 是 应用最广的 Java Web 开发框架,主要是因为它是最先发行的几个框架之一(2001年6月)。这个框架由 Craig McClanahan 开发完成,现在作为 Apache 软件基金会的一个开源项目。 当时,它极大地简化了 JSP/Servlet 编程范例并且赢得了 大多数正在使用私人框架的开发人员的亲睐。它简化了编程模型,它是开源的,它具有一个庞大的社区,这些都使得这个项目快速成长,同时变得越来越流行。
要将 Struts 与 Spring 集成,你有两个选择:
一:配置 Spring 将 Action 作为 bean 托管,使用 ContextLoaderPlugin, 并且在 Spring context中设置依赖关系。
二:继承 Spring 的 ActionSupport 类并且 使用getWebApplicationContext() 方法获取 Spring 管理的 bean。
1.1. ContextLoaderPlugin
ContextLoaderPlugin 是 Struts 1.1+ 的插件,用来为 Struts 的 ActionServlet 加载 Spring context文件。 这个context引用 WebApplicationContext (由 ContextLoaderListener 加载) 作为它的父类。默认的context文件是映射的 Servlet 的名字,加上 -servlet.xml后缀。 如果 ActionServlet 在 web.xml 里面的定义是 <servlet-name>action</servlet-name>, 那么默认的文件就是 /WEB-INF/action-servlet.xml。
要配置这个插件,请把下面的 XML 贴到 struts-config.xml 文件中 plug-ins 部分的底端:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
context配置文件的位置可以通过 contextConfigLocation属性来自定义。
Xml代码 复制代码
1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
2. <set-property property="contextConfigLocation"
3. value="/WEB-INF/action-servlet.xml.xml,/WEB-INF/applicationContext.xml"/>
4. </plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>
你也可以使用这个插件加载所有的配置文件,这在使用测试工具(例如 StrutsTestCase)的时候特别有用。 StrutsTestCase 的 MockStrutsTestCase 不会在启动的时候初始化 Listener,将你所有的配置文件放在plug-in里面是一种解决方案。(有个 已记录的 bug 就是针对这个问题的,但是已经被标记为“无须改正”)。
在 struts-config.xml 中配置好插件以后,你可以配置Sping来管理 Action。Spring (1.1.3以后的版本) 提供下面两种方式:
一:用 Spring 的DelegatingRequestProcessor重载 Struts 默认的 RequestProcessor 。
二:将 <action-mapping> 的 type 属性设为 DelegatingActionProxy。
这两种方法都允许你在 action-context.xml 文件中管理你的 Action 以及依赖关系。 连接 struts-config.xml 和 action-servlet.xml 中的 Action 的桥梁 是 action-mapping 的“path”和 bean 的“name”。如果你在 struts-config.xml 文件中有如下配置:
<action path="/users" .../>
你必须在 action-servlet.xml 中将 Action bean 的名字定义为 “/users”:
<bean name="/users" .../>
1.1.1. DelegatingRequestProcessor
为了在 struts-config.xml 文件中配置 DelegatingRequestProcessor,你需要重载 <controller> 元素的 “processorClass” 属性。 下面的几行应该放在 <action-mapping> 元素的后面。
Xml代码 复制代码
1. <controller>
2. <set-property property="processorClass"
3. value="org.springframework.web.struts.DelegatingRequestProcessor"/>
4. </controller>
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
增加这些设置之后,不管你查询任何类型的 Action,Sping都自动在它的context配置文件中寻找。 实际上,你甚至不需要指定类型。下面两个代码片断都可以工作:
<action path="/user" type="com.whatever.struts.UserAction"/>
<action path="/user"/>
如果你使用 Struts 的 modules 特性,你的 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为 <action path="/user"/>,而且它的 module 前缀为“admin”, 那么它应该对应名为 <bean name="/admin/user"/> 的 bean。
Note
如果你在 Struts 应用中使用了 Tiles,你需要配置 <controller> 为 DelegatingTilesRequestProcessor。
1.1.2. DelegatingActionProxy
如果你有一个自定义的 RequestProcessor 并且不能够使用 DelegatingRequestProcessor 或者 DelegatingTilesRequestProcessor,你可以使用 DelegatingActionProxy 作为你 action-mapping 中的类型。
Xml代码 复制代码
1. <action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
2. name="userForm" scope="request" validate="false" parameter="method">
3. <forward name="list" path="/userList.jsp"/>
4. <forward name="edit" path="/userForm.jsp"/>
5. </action>
<action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
name="userForm" scope="request" validate="false" parameter="method">
<forward name="list" path="/userList.jsp"/>
<forward name="edit" path="/userForm.jsp"/>
</action>
action-servlet.xml 文件中的bean定义依然不变,不管你使用了自定义的 RequestProcessor 还是 DelegatingActionProxy。
如果你把 Action 定义在Spring的context文件里,那么 Spring bean 容器的所有特性都可用了:比如,依赖注入,再比如,为每个请求初始化一个新的 Action 实例。 如果要使用这个特性, Action bean 定义中需要声明scope="prototype"。
Xml代码 复制代码
1. <bean name="/user" scope="prototype" autowire="byName"
2. class="org.example.web.UserAction"/>
<bean name="/user" scope="prototype" autowire="byName"
class="org.example.web.UserAction"/>
1.2. ActionSupport 类
需要在web.xml里面加载spring配置文件如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
</context-param>
正如前面提到的,你可以使用 WebApplicationContextUtils 类从 ServletContext 中获得 WebApplicationContext 。 另一个简单的办法是继承 Spring 的 Action 类。举个例子,除了继承 Struts 的 Action 之外,你也可以继承 Spring 的 ActionSupport 类。
ActionSupport 类提供了一些便利的方法,例如 getWebApplicationContext()。 下面的例子展示了如何在 Action 中使用它:
Java代码 复制代码
1. public class UserAction extends DispatchActionSupport {
2.
3. public ActionForward execute(ActionMapping mapping,
4. ActionForm form,
5. HttpServletRequest request,
6. HttpServletResponse response) throws Exception {
7. if (log.isDebugEnabled()) {
8. log.debug("entering 'delete' method...");
9. }
10. WebApplicationContext ctx = getWebApplicationContext();
11. UserManager mgr = (UserManager) ctx.getBean("userManager");
12. // talk to manager for business logic
13. return mapping.findForward("success");
14. }
15. }
public class UserAction extends DispatchActionSupport {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'delete' method...");
}
WebApplicationContext ctx = getWebApplicationContext();
UserManager mgr = (UserManager) ctx.getBean("userManager");
// talk to manager for business logic
return mapping.findForward("success");
}
}
Spring 包含了所有标准 Struts Action 的子类 - Spring 版本在类名末尾附加了 Support:
ActionSupport,
DispatchActionSupport,
LookupDispatchActionSupport
MappingDispatchActionSupport
你应该选择最适合你项目的集成方式。继承使得你的代码更可靠,并且你确切地知道依赖关系是如何被解析的。 另一方面,使用 ContextLoaderPlugin 允许你方便地在context XML 文件里面增加新的 依赖关系。这两种集成方法,不管哪一种 Spring 都提供了相当好用的选项。
2008-09-03 10:20
1. Struts
Struts 是 应用最广的 Java Web 开发框架,主要是因为它是最先发行的几个框架之一(2001年6月)。这个框架由 Craig McClanahan 开发完成,现在作为 Apache 软件基金会的一个开源项目。 当时,它极大地简化了 JSP/Servlet 编程范例并且赢得了 大多数正在使用私人框架的开发人员的亲睐。它简化了编程模型,它是开源的,它具有一个庞大的社区,这些都使得这个项目快速成长,同时变得越来越流行。
要将 Struts 与 Spring 集成,你有两个选择:
一:配置 Spring 将 Action 作为 bean 托管,使用 ContextLoaderPlugin, 并且在 Spring context中设置依赖关系。
二:继承 Spring 的 ActionSupport 类并且 使用getWebApplicationContext() 方法获取 Spring 管理的 bean。
1.1. ContextLoaderPlugin
ContextLoaderPlugin 是 Struts 1.1+ 的插件,用来为 Struts 的 ActionServlet 加载 Spring context文件。 这个context引用 WebApplicationContext (由 ContextLoaderListener 加载) 作为它的父类。默认的context文件是映射的 Servlet 的名字,加上 -servlet.xml后缀。 如果 ActionServlet 在 web.xml 里面的定义是 <servlet-name>action</servlet-name>, 那么默认的文件就是 /WEB-INF/action-servlet.xml。
要配置这个插件,请把下面的 XML 贴到 struts-config.xml 文件中 plug-ins 部分的底端:
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>
context配置文件的位置可以通过 contextConfigLocation属性来自定义。
Xml代码 复制代码
1. <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
2. <set-property property="contextConfigLocation"
3. value="/WEB-INF/action-servlet.xml.xml,/WEB-INF/applicationContext.xml"/>
4. </plug-in>
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="/WEB-INF/action-servlet.xml.xml,/WEB-INF/applicationContext.xml"/>
</plug-in>
你也可以使用这个插件加载所有的配置文件,这在使用测试工具(例如 StrutsTestCase)的时候特别有用。 StrutsTestCase 的 MockStrutsTestCase 不会在启动的时候初始化 Listener,将你所有的配置文件放在plug-in里面是一种解决方案。(有个 已记录的 bug 就是针对这个问题的,但是已经被标记为“无须改正”)。
在 struts-config.xml 中配置好插件以后,你可以配置Sping来管理 Action。Spring (1.1.3以后的版本) 提供下面两种方式:
一:用 Spring 的DelegatingRequestProcessor重载 Struts 默认的 RequestProcessor 。
二:将 <action-mapping> 的 type 属性设为 DelegatingActionProxy。
这两种方法都允许你在 action-context.xml 文件中管理你的 Action 以及依赖关系。 连接 struts-config.xml 和 action-servlet.xml 中的 Action 的桥梁 是 action-mapping 的“path”和 bean 的“name”。如果你在 struts-config.xml 文件中有如下配置:
<action path="/users" .../>
你必须在 action-servlet.xml 中将 Action bean 的名字定义为 “/users”:
<bean name="/users" .../>
1.1.1. DelegatingRequestProcessor
为了在 struts-config.xml 文件中配置 DelegatingRequestProcessor,你需要重载 <controller> 元素的 “processorClass” 属性。 下面的几行应该放在 <action-mapping> 元素的后面。
Xml代码 复制代码
1. <controller>
2. <set-property property="processorClass"
3. value="org.springframework.web.struts.DelegatingRequestProcessor"/>
4. </controller>
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
增加这些设置之后,不管你查询任何类型的 Action,Sping都自动在它的context配置文件中寻找。 实际上,你甚至不需要指定类型。下面两个代码片断都可以工作:
<action path="/user" type="com.whatever.struts.UserAction"/>
<action path="/user"/>
如果你使用 Struts 的 modules 特性,你的 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为 <action path="/user"/>,而且它的 module 前缀为“admin”, 那么它应该对应名为 <bean name="/admin/user"/> 的 bean。
Note
如果你在 Struts 应用中使用了 Tiles,你需要配置 <controller> 为 DelegatingTilesRequestProcessor。
1.1.2. DelegatingActionProxy
如果你有一个自定义的 RequestProcessor 并且不能够使用 DelegatingRequestProcessor 或者 DelegatingTilesRequestProcessor,你可以使用 DelegatingActionProxy 作为你 action-mapping 中的类型。
Xml代码 复制代码
1. <action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
2. name="userForm" scope="request" validate="false" parameter="method">
3. <forward name="list" path="/userList.jsp"/>
4. <forward name="edit" path="/userForm.jsp"/>
5. </action>
<action path="/user" type="org.springframework.web.struts.DelegatingActionProxy"
name="userForm" scope="request" validate="false" parameter="method">
<forward name="list" path="/userList.jsp"/>
<forward name="edit" path="/userForm.jsp"/>
</action>
action-servlet.xml 文件中的bean定义依然不变,不管你使用了自定义的 RequestProcessor 还是 DelegatingActionProxy。
如果你把 Action 定义在Spring的context文件里,那么 Spring bean 容器的所有特性都可用了:比如,依赖注入,再比如,为每个请求初始化一个新的 Action 实例。 如果要使用这个特性, Action bean 定义中需要声明scope="prototype"。
Xml代码 复制代码
1. <bean name="/user" scope="prototype" autowire="byName"
2. class="org.example.web.UserAction"/>
<bean name="/user" scope="prototype" autowire="byName"
class="org.example.web.UserAction"/>
1.2. ActionSupport 类
需要在web.xml里面加载spring配置文件如:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext*.xml</param-value>
</context-param>
正如前面提到的,你可以使用 WebApplicationContextUtils 类从 ServletContext 中获得 WebApplicationContext 。 另一个简单的办法是继承 Spring 的 Action 类。举个例子,除了继承 Struts 的 Action 之外,你也可以继承 Spring 的 ActionSupport 类。
ActionSupport 类提供了一些便利的方法,例如 getWebApplicationContext()。 下面的例子展示了如何在 Action 中使用它:
Java代码 复制代码
1. public class UserAction extends DispatchActionSupport {
2.
3. public ActionForward execute(ActionMapping mapping,
4. ActionForm form,
5. HttpServletRequest request,
6. HttpServletResponse response) throws Exception {
7. if (log.isDebugEnabled()) {
8. log.debug("entering 'delete' method...");
9. }
10. WebApplicationContext ctx = getWebApplicationContext();
11. UserManager mgr = (UserManager) ctx.getBean("userManager");
12. // talk to manager for business logic
13. return mapping.findForward("success");
14. }
15. }
public class UserAction extends DispatchActionSupport {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'delete' method...");
}
WebApplicationContext ctx = getWebApplicationContext();
UserManager mgr = (UserManager) ctx.getBean("userManager");
// talk to manager for business logic
return mapping.findForward("success");
}
}
Spring 包含了所有标准 Struts Action 的子类 - Spring 版本在类名末尾附加了 Support:
ActionSupport,
DispatchActionSupport,
LookupDispatchActionSupport
MappingDispatchActionSupport
你应该选择最适合你项目的集成方式。继承使得你的代码更可靠,并且你确切地知道依赖关系是如何被解析的。 另一方面,使用 ContextLoaderPlugin 允许你方便地在context XML 文件里面增加新的 依赖关系。这两种集成方法,不管哪一种 Spring 都提供了相当好用的选项。
发表评论
-
JVM配置详解及调优
2012-06-21 14:13 1083堆大小设置 JVM 中最大 ... -
WABACUS框架介绍
2012-05-17 10:07 2113... -
JAVA开发性能调优
2012-05-09 11:41 977下面是参考网络资源总结的一些在Java编程中尽可能要做到 ... -
使用WFetch查看HTTP请求、响应报文
2012-05-07 10:36 1310WFetch界面比较简陋,但是已经满足我的一般需求了。想 ... -
java命令行指定JDK运行环境
2011-12-14 22:05 2737set path=C:/Program Files/J ... -
ibaitis 级联查询
2011-04-12 13:10 2484使用iBatis开发已经有一年了,这一年来不管愿意不愿意,喜欢 ... -
关于struts2 遍历Map最全的总结
2011-04-06 16:44 964Struts2 中iterator标签遍 ... -
Struts2+JSON+jQuery实现异步交互数据时选择要序列化的属性(一注解方式)
2011-03-29 00:59 1583在使用Struts2的JSON插件,实现Action中的属性序 ... -
struts2 + jquery struts2 处理json
2011-03-28 14:10 2437<package name="default& ... -
cascade 与 inverse区别
2011-03-15 09:58 1017一、cascade 1.概述 ... -
hibernate fetch的select 和 join的区别
2011-03-11 17:15 3621join 查询的时候,是用一条语句查处所有记录,包括关联表记录 ... -
深入struts2的配置文件 struts2标签解释
2011-03-08 22:59 1708[color=darkred][/color]深入struts ... -
ajax访问SSH2返回connect异常的处理
2011-03-08 22:38 1506在struts2 pojo action中有对象属性的,并被h ... -
SSH2整合设计 事务管理
2011-03-08 22:27 1541ssh2整合SSH2整合 2009-12-02 09:31:4 ... -
ssh dao设计
2011-03-07 23:45 1149spring和hibernate 泛型:http://lbxh ... -
ldap查询条件
2010-08-30 09:19 2592定义查询过滤器(Search Filter Definitio ... -
在CXF中用JAXB数据绑定支持HashMap类型
2010-07-22 10:00 1638在CXF中用JAXB数据绑定支持HashMap类型 ... -
java.lang.NoSuchMethodError: com.sun.xml.ws.api.server.WSEndpoint.getPolicyMap()
2010-07-20 16:42 4520在使用CXF 和spring发布WEB服务时出现了java.l ... -
jax ws 注释详解
2010-07-09 10:38 2697基于 XML 的 Web Service 的 Java A ... -
Rest 和 soap比较
2010-07-07 15:48 1145我有这样一个推断,在计算机世界中,但凡那些让开发人员记住的重要 ...
相关推荐
博文链接:https://qqwyy.iteye.com/blog/181991
Spring2.0对Struts2.0的整合,内含一个小示例,带清晰注释
Spring2.5.3+Struts2.0.11.1+Hibernate3.2.6整合备忘 (转载)
Struts2.Hibernate3.2.Spring2.0整合 功能很齐全
Struts2.Hibernate3.2.Spring2.0整合项目代码(包括增删查改功能) jar在:(Struts2.Hibernate3.2.Spring2.0整合项目代码的jar包) 里面下载
Struts2.Hibernate3.2.Spring2.0整合项目代码的jar包 Struts2.Hibernate3.2.Spring2.0整合项目代码的jar包
对spring 2.0 + struts 2.0 + hibernate 3.2 进行了系统的整合。
Struts 2.0 + Hibernate 3.2 + Spring 2.0 框架整合.doc
总结,该例示注释非常详细,演示Spring托管Hibernate和Struts的Action, 以及Spring的事务声明(包括1.x与2.x的用法)与非事务声明的使用方式。 使用DWR可以方便的使用Spring托管的持久层功能。 目的:希望广大Java...
Struts2 Hibernate3.2 Spring2.0 整合 J2EE SSH
以前整合大多数为struts1.x hibernate spring 现在struts2.x也出来一阵子了,大多公司开始使用struts2.x 所以这就造就了整合问题,此系统能帮助初学都迅速进入状态,迅速上手
struts2.0jar包以及struts2.0与spring的整合jar包
spring2.0整合struts1.2和hibernate3开发小例子,并配有AOP事务管理,所需要的支持包还需要根据指导手册导入,供大家参考。
Struts2.0-Hibernate3.2-Spring2.0整合.doc
struts2.0 hibernate spring 整合S2SH
spring2.5.6 整合struts2.0 spring2.5.6 整合struts2.0 spring2.5.6 整合struts2.0