以前都是用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继承:
- /**
- * $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;
- }
- }
/**
* $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
- ....
- <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>
- ....
....
<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
- ....
- <!--command className="org.apache.struts.chain.commands.servlet.CreateAction"/-->
- <command className="struts.demo.command.ComponentAutowireInterceptor"/><!--替换原先的CreateAction-->
- ....
....
<!--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