`

struts1.2和spring的整合几种方式

阅读更多
  使用了struts和spring一段时间.但是对其中他们的整合也用了好几次.就这次机会总结下经验并整理下思绪.
  整合方式1:
最原始而易懂的方式:
Action继承spring提供的类org.springframework.web.struts.MappingDispatchActionSupport
Action中的代码:
public class UserAction extends MappingDispatchActionSupport {
public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uname = request.getParameter("uname");
		String upass = request.getParameter("upass");
		//使用其自带的一个方法实例化ApplicationContext对象
		ApplicationContext context = this.getWebApplicationContext();
		userService=(UserService)context.getBean("userService");
		User user = userService.findByName(uname, upass);
		if (user==null) {
			request.setAttribute("error", "对不起,您输入的用户名或者密码错误!");
			
			return mapping.findForward("failure");
		} else {
			request.getSession().setAttribute("user", user);
			return mapping.findForward("success");
			
		}
	}
}

struts-config.xml代码:
<action path="/login" parameter="login"
			type="com.addresslist.action.UserAction" scope="request">
			<forward name="success" path="/page/index.htm"></forward>			
		</action>

你会发现使用这种方法的话.直接可以保持你原先struts的配置.只需要改变一下你相应的Action类继承MappingDispatchActionSupport.
其中缺点就是:你的Action将会和Spring耦合在一起.当你有多个Action类都继承MappingDispatchActionSupport的话你将会每次都需要调用getWebApplicationContext()获取ApplicationContext的实例.这样如果你想放弃使用spring的话.所要修改的代码量将非常大

整合方式2:
启动Spring再在相应的Action类中实例化ApplicationContext
spring有三种启动方式,使用ContextLoaderServlet,ContextLoaderListener和ContextLoaderPlugIn.
我的测试使用的ContextLoaderListener
web.xml的配置:
<!--  
	//可以选择使用ContextLoaderServle
	<servlet>
  <servlet-name>springInitServlet</servlet-name>
  	<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
        // 如果使用要注意这里设置开启的优先级要比Struts的ActionServlet高
  	<load-on-startup>2</load-on-startup>
  </servlet>
	-->
	<context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <!-- 使用ContextLoaderListener -->
	<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

与整合1主要的变化是使用了 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

形成spring的环境
struts-config.xml代码:
<action path="/login" parameter="login"
			type="com.addresslist.action.UserAction" scope="request">
			<forward name="success" path="/page/index.htm"></forward>			
		</action>
<!-- 
		可以选择使用
		<plug-in
		className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation"
			value="/WEB-INF/applicationContext.xml" />
	</plug-in>
 -->

Action的代码:
public class UserAction extends MappingDispatchAction {
public UserService getFileService(){
//这里使用了WebApplicationContextUtils工具类实例化ApplicationContext 
		ApplicationContext ac=WebApplicationContextUtils.getWebApplicationContext(this.getServlet().getServletContext());
		return (UserService) ac.getBean("userService");
	}

	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uname = request.getParameter("uname");
		String upass = request.getParameter("upass");
		
		User user = getFileService().findByName(uname, upass);
		if (user==null) {
			request.setAttribute("error", "对不起,您输入的用户名或者密码错误!");
			
			return mapping.findForward("failure");
		} else {
			request.getSession().setAttribute("user", user);
			return mapping.findForward("success");
			
		}
	}
}

WebApplicationContextUtils(参考于http://tech.ddvip.com/2007-08/118764954132510_8.html)

  当 Web 应用集成 Spring 容器后,代表 Spring 容器的EebApplicationContext 对象将以

  WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键存放在 ServletContext 属性列表中。您当然可以直接通过以下语句获取 WebApplicationContext:

  WebApplicationContext wac = (WebApplicationContext)servletContext.

  getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

  但通过位于 org.springframework.web.context.support 包中的WebApplicationContextUtils 工具类获取 WebApplicationContext 更方便:

  WebApplicationContext wac =WebApplicationContextUtils.

  getWebApplicationContext(servletContext);

  当 ServletContext 属性列表中不存在 WebApplicationContext 时,getWebApplicationContext() 方法不会抛出异常,它简单地返回 null。如果后续代码直接访问返回的结果将引发一个 NullPointerException 异常,而 WebApplicationContextUtils 另一个 getRequiredWebApplicationContext(ServletContext sc) 方法要求 ServletContext 属性列表中一定要包含一个有效的 WebApplicationContext 对象,否则马上抛出一个 IllegalStateException 异常。我们推荐使用后者,因为它能提前发现错误的时间,强制开发者搭建好必备的基础设施。

通过该方式整合我们只需要进行启动Spring和在相应的Action类获取WebApplicationContext .这样子对Spring的耦合降低


整合方式3:将动作管理委托给 Spring
将Struts的动作管理委托给Spring.使用Struts-config.xml的动态映射中注册一个代理.
代理负责在Spring中找到Struts的动作.由于动作在Spring的控制之下.所以Spring可以对Action进行javaBean的注入和Spring的一些Aop使用
需要发生改变的配置有:
Struts-config.xml:
<struts-config>
	<data-sources />
	<form-beans />
	<global-exceptions />
	<global-forwards />
	<action-mappings>
         <!--这里使用了org.springframework.web.struts.DelegatingAction-->
	<action path="/login" parameter="login" type="org.springframework.web.struts.DelegatingActionProxy">
	<forward name="success" path="/page/index.htm"/>
	<forward name="failure" path="/page/fail.htm"/>
	</action>
	</action-mappings>
	

	<message-resources
		parameter="com.addresslist.properties.ApplicationResources" />
<!--这里使用ContextLoaderPlugIn建立Spring的环境-->
	<plug-in
		className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
			value="/WEB-INF/applicationContext.xml" />
</plug-in>

Action中的代码:
public class UserAction extends MappingDispatchAction {
//使用普遍依赖注入方式
UserService userService;
public void setUserService(UserService userService) {
		this.userService = userService;
}
public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String uname = request.getParameter("uname");
		String upass = request.getParameter("upass");
		//直接使用userService
		User user = userService.findByName(uname, upass);
		if (user==null) {
			request.setAttribute("error", "对不起,您输入的用户名或者密码错误!");
			
			return mapping.findForward("failure");
		} else {
			request.getSession().setAttribute("user", user);
			return mapping.findForward("success");
			
		}
	}
}

这个配置只于传统的struts-config的配置.这里使用的是Spring的注册代理.而不是直接使用相应Action类的完整类名.
这里使用的ContextLoaderPlugIn.声明的环境。通过DelegatingActionProxy使用动作映射名/login在Spring环境中找到相应的Action.所以我们需要在Spring环境中注册一个Struts的动作:
<bean name="/login" class="com.addresslist.action">
<property name="userService" ref="userService"></property>
</bean>

这个方法可以说是多数人认可最好的方法:
这个方法的使用只在配置文件中着手不需要对源码进行修改.将struts动作放置到Spring中使得其享受了Spring提供的各种好处.一旦让 Spring 控制您的 Struts 动作,您就可以使用 Spring 给动作补充更强的活力。例如,没有 Spring 的话,所有的 Struts 动作都必须是线程安全的。如果您设置 <bean> 标记的 singleton 属性为“false”,那么不管用何种方法,您的应用程序都将在每一个请求上有一个新生成的动作对象。您也可以利用 Spring 的生命周期方法。例如,当实例化 Struts 动作时,<bean> 标记的 init-method 属性被用于运行一个方法。类似地,在从容器中删除 bean 之前,destroy-method 属性执行一个方法.

整合方式4:
使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts 的 RequestProcessor 处理程序
只需要在struts-config.xml中配置:
<action-mappings>
	<action path="/login" parameter="login" type="com.addresslist.action.UserAction">
	<forward name="success" path="/page/index.htm"/>
	<forward name="failure" path="/page/fail.htm"/>
	</action>
<!--下面controller配置是核心-->
<controller processorClass="org.springframework.web.struts. 
   DelegatingRequestProcessor"/> 
<plug-in
		className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
			value="/WEB-INF/applicationContext.xml" />
</plug-in>

Struts中的action配置则无需配置type属性(即使配置了type属性也不起任何作用,除非在spring的配置文件里找不到对应的name属性值)
其他的配置和类使用和整合方式3一样.
这个设计方式是使用Spring提供的一个RequestProcessor来进行工作.这里如果对struts工作流程比较熟悉的话就指定struts1.2真正工作的核心是RequestProecssor和Action.
这里使用Spring的DelegatingRequestProcessor替换了struts的RequestProcessor.可以使得struts享受到Spring的控制容器.网上人都不建议使用这种方法.因为考虑到效率和方法使用寿命的问题.所以我也比较少用.
总结语:
通过以上的总结。大概了解了struts和Spring整合的几种方式.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics