首页 > SSH > spring集成struts1.x

spring集成struts1.x

此文是学习时记录的,还未实践验证,任何不妥之处,请指正。此文来自于spring-reference文档http://static.springsource.org/spring/docs/2.5.x/reference/web-integration.html
本文未写完,逐步完善中….

要将struts1.x集成到spring中,有两种方式

    1、配置spring,把你的Actions当作 beans来对待,办法就是使用ContextLoaderPlugin,并且在spring的上下文中设置依赖关系。
    2、继承spring的ActionSupport 类,使用getWebApplicationContext()方法来获取由spring管理的beans。

下面详细说第一种
配置spring,把你的Actions当作 beans来对待,办法就是使用ContextLoaderPlugin,并且在spring的上下文中设置依赖关系。

1.1、ContextLoaderPlugin
ContextLoaderPlugin是struts 1.1+ 的一个插件,用来为struts的ActionServlet加载spring的上下文文件,此上下文文件引用了WebApplicationContext(由ContextLoaderListener加载)来做为他的上级,此上下文文件的默认名称是 servlet的映射文件名加上 -servlet.xml。如果ActionServlet在web.xml定义为 action,则上下文的默认名称为/WEB-INF/action-servlet.xml。

要配置此plug-in,将下面的代码复制到 struts-config.xml文件中的plug-ins部分(接近文件的底部)

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/>

上下文文件的位置可以通过contextConfigLocation属性来自己定义。比如

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

在struts-config.xml中配置好插件之后,你可以通过配置来让spring管理 Action。
Spring(1.1.3+)提供了两种方式来完成此任务。
1.1.1使用spring的DelegatingRequestProcessor来覆盖struts的默认的RequestProcessor
1.1.2在你的的 type 属性中使用 DelegatingActionProxy类
这两种方式都允许你在action-servlet.xml文件中管理你的Actions和他们的依赖。
struts-config.xml中的Action和action-servlet.xml之间的桥梁就是action-mapping的”path”和bean的”name”
例如:如果你在struts-config.xml文件中有如下设置

<action path="/users" .../>

那么你必须在action-servlet.xml文件中将action的bean名称设置为 “/users”

<bean name="/users" .../>

1.1.1.1DelegatingRequestProcessor
在struts-config.xml文件中配置DelegatingRequestProcessor,办法是:覆盖controller元素的processorClass属性
如下:

<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>

设置好之后,不管你的Action类型是什么,都会在spring的上下文文件中自动查找你的Action.实际上,你甚至可以不指定action的类型。下面的两种方式都可以正常工作。

<action path="/user" type="com.whatever.struts.UserAction"/>
<action path="/user"/>

1.1.2.1 DelegatingActionProxy
如果你有自定义的RequestProcessor 并且不能使用DelegatingRequestProcessor 或者 DelegatingTilesRequestProcessor approaches,则此时就要使用DelegatingActionProxy。此种方式我认为使用的很少,你需要用到时,再查找相应的文档就可以。在这里就不细说了。

下面详细说第二种
继承spring的ActionSupport 类,使用getWebApplicationContext()方法来获取由spring管理的beans
你可以调用WebApplicationContextUtils类的方法来获取WebApplicationContext,

WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

不过更加简单的方式就是继承spring为struts提供的Action类。
例如,你可以继承spring的ActionSupport类,而不是struts的Action类.
ActionSupport类提供了一些其他的方便的方法,比如getWebApplicationContext().
例如:

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");
}
}

String提供了所有标准Struts的action类的子类,Spring的struts子类只是在名称后面加上了Support
• ActionSupport,
• DispatchActionSupport,
• LookupDispatchActionSupport
• MappingDispatchActionSupport.

总结:
spring提供了两种方式来集成 struts,至于选择哪种方式,要取决于哪种方式更加适合你的项目。
1、子类的方式能使你的代码更加具有可读性,并且精确的知道依赖关系。
2、而使用ContextLoaderPlugin插件的方式,允许你方便的在上下文xml文件中添加新的依赖。

分类: SSH 标签:
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.