浏览 3982 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2007-08-17
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; } } 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> .... chain-config.xml .... <!--command className="org.apache.struts.chain.commands.servlet.CreateAction"/--> <command className="struts.demo.command.ComponentAutowireInterceptor"/><!--替换原先的CreateAction--> .... Struts的Action无需实现任何接口,也不需要特别的配置,就可以自动实现spring bean的注入。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |