Spring 整合 struts1

xiaoxiao2024-07-27  28

使用Spring的ActionSupport 类整合Structs1.X,使用 Spring 的 DelegatingRequestProcessor覆盖Struts的RequestProcessor,将StrutsAction管理委托给Spring框架 -- 装载应用环境: 无论您使用哪种技术,都需要使用 Spring 的 ContextLoaderPlugin 为 Struts 的 ActionServlet 装载 Spring 应用程序环境 在struts-config.xml 文件尾处添加该插件: <plug-in className= "org.springframework.web.struts.ContextLoaderPlugIn"> <set-propertyproperty= "contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml"/> </plug-in> -- 第一种:使用Spring的ActionSupport 该方法: 简单快捷,但会导致struts和spring耦合在一起,如果要移值struts应用程序要重写代码. 例如: public class ActionName extends ActionSupport { public ActionForward execute( ){ ------------------------------------------------------------------------------ ApplicationContext ctx= getWebApplicationContext(); BookService bookService = (BookService) ctx.getBean("bookService"); ------------------------------------------------------------------------------------------------------------- } } 第二种:覆盖 RequestProcessor 该方法使用 org.springframework.web.struts.DelegatingRequestProcessor 类来覆盖 Struts 的 RequestProcessor 处理程序,通过 Spring 的 DelegatingRequestProcessor 进行整合,看下面的struts-config.xml文件的主要配置部分: <form-beans> <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> <form-beans/> 此方法比第一种要好,但如果您使用一个不同的 RequestProcessor,则需要手动整合 Spring 的 DelegatingRequestProcessor,添加的代码会造成维护的麻烦并且将来会降低您的应用程序的灵活性。 -- 第三种:将动作管理委托给 Spring 这里列出struts-config.xml中的主要部分: <action path="/searchSubmit" type="org.springframework.web.struts.DelegatingActionProxy" ............... </action> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/application.xml"/> </plug-in> 此方法是三种方法中最好的
转载请注明原文地址: https://www.6miu.com/read-5017160.html

最新回复(0)