Struts1.3与Spring以底侵入方式集成

以前都是用webwork,最近因为遗留项目的维护,捣腾起struts1.x,原先的代码到处充斥着ApplicationContext.getBean的代码,实在没法容忍,上网找了struts和spring集成的例子,基本上都是struts1.3以前版本的代码,很不优雅。struts1.3和之前版本相比,使用command-chain来处理请求,有点类似于webwork的拦截器,下面介绍下整个实现思路,欢迎批评指正。

1、首先需要一个ServletContextListener,用于获取Spring的ApplicationContext实例(使用Quake Wang的jert的代码),具体请参考附件源码

2、定义ComponentAutowireInterceptor实现ActionCommon接口,以实现spring bean的注入,这里为了方便,直接从CreateAction继承:

Java代码 复制代码
  1. /**  
  2.  * $Revision: 1.0 $  
  3.  * Created: 2007-8-17  
  4.  * $Date: 2007-8-17 $  
  5.  *   
  6.  * Author: Keven Chen  
  7.  */  
  8. package struts.demo.command;   
  9.   
  10. import org.apache.struts.action.Action;   
  11. import org.apache.struts.chain.commands.servlet.CreateAction;   
  12. import org.apache.struts.chain.contexts.ActionContext;   
  13. import org.springframework.context.ApplicationContext;   
  14. import org.springframework.web.context.support.WebApplicationContextUtils;   
  15.   
  16. import com.comwave.webui.core.container.Application;   
  17.   
  18. /**  
  19.  * @author Keven Chen  
  20.  * @version $Revision 1.0 $  
  21.  *  
  22.  */  
  23. public class ComponentAutowireInterceptor extends CreateAction {   
  24.        
  25.     protected Action createAction(ActionContext context, String type) throws Exception {   
  26.         Action action = super.createAction(context, type);   
  27.         if(action !=null){   
  28.             Application.getInstance().getContainer().autowireComponent(action);   
  29.             System.out.println("装配Action");   
  30.         }   
  31.         return action;   
  32.     }   
  33.   
  34. }  
/**
 * $Revision: 1.0 $
 * Created: 2007-8-17
 * $Date: 2007-8-17 $
 * 
 * Author: Keven Chen
 */
package struts.demo.command;

import org.apache.struts.action.Action;
import org.apache.struts.chain.commands.servlet.CreateAction;
import org.apache.struts.chain.contexts.ActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.comwave.webui.core.container.Application;

/**
 * @author Keven Chen
 * @version $Revision 1.0 $
 *
 */
public class ComponentAutowireInterceptor extends CreateAction {
	
	protected Action createAction(ActionContext context, String type) throws Exception {
		Action action = super.createAction(context, type);
		if(action !=null){
			Application.getInstance().getContainer().autowireComponent(action);
			System.out.println("装配Action");
		}
		return action;
	}

}



3、配置web.xml和chain-config.xml配置片段(完整配置请参考附件代码)

web.xml

Java代码 复制代码
  1. ....   
  2.     <context-param>   
  3.         <param-name>contextConfigLocation</param-name>   
  4.         <param-value>classpath:spring/applicationContext*.xml</param-value>   
  5.     </context-param>   
  6.        
  7.     <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
  8.     </listener>   
  9.   
  10.     <listener>        <listener-class>com.comwave.webui.core.web.listener.DefaultApplicationSetupListener</listener-class>   
  11.     </listener>   
  12.        
  13.     <servlet>   
  14.         <servlet-name>action</servlet-name>   
  15.         <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>   
  16.         <init-param>   
  17.             <param-name>config</param-name>   
  18.             <param-value>struts/struts-config.xml</param-value>   
  19.         </init-param>   
  20.         <init-param>   
  21.             <param-name>chainConfig</param-name>   
  22.             <param-value>struts/chain-config.xml</param-value>   
  23.         </init-param>   
  24.         <load-on-startup>2</load-on-startup>   
  25.     </servlet>   
  26.   
  27.     <servlet-mapping>   
  28.         <servlet-name>action</servlet-name>   
  29.         <url-pattern>*.do</url-pattern>   
  30.     </servlet-mapping>   
  31. ....  
....
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext*.xml</param-value>
	</context-param>
	
	<listener>		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<listener>		<listener-class>com.comwave.webui.core.web.listener.DefaultApplicationSetupListener</listener-class>
	</listener>
	
	<servlet>
		<servlet-name>action</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>struts/struts-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>chainConfig</param-name>
			<param-value>struts/chain-config.xml</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
....


chain-config.xml

Java代码 复制代码
  1. ....   
  2.      <!--command className="org.apache.struts.chain.commands.servlet.CreateAction"/-->   
  3.   
  4.      <command className="struts.demo.command.ComponentAutowireInterceptor"/><!--替换原先的CreateAction-->   
  5. ....  
....
     <!--command className="org.apache.struts.chain.commands.servlet.CreateAction"/-->

     <command className="struts.demo.command.ComponentAutowireInterceptor"/><!--替换原先的CreateAction-->
....



Struts的Action无需实现任何接口,也不需要特别的配置,就可以自动实现spring bean的注入。

 

原文摘自:http://balaschen.iteye.com/blog/113507

 

你可能感兴趣的:(apache,spring,Web,struts,servlet)