Spring集成Struts的方式
Spring集成Struts的方式
??? 1.从一个知晓Spring的基类中派生Struts action;
??? 2.将请求委托给作为Spring bean管理的Struts action来处理;
(1)注册Spring插件
??? <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
??? ??? <set-property property=J"contextConfigLocation" value="/WEB-INF/training-servlet.xml,/..."/>
??? </plug-in>
(2.1)实现知晓Spring的Struts action
??? 集成Struts和Spring的一种方式是从一个公共的能访问Spring应用上下文的机类中派生所有的Struts action;
??? Spring提供了org.springframework.web.struts.ActionSupport,一个从org.apache.struts.action.Action的抽象实现;
??? public class **Action extends ActionSupport{
??? ??? public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
??? ??? ??? ApplicationContext context = getWebApplicationContext();
??? ??? ??? CourseService courseService = (CourseService) context.getBean("***");
??? ??? ??? courseService.get**();
??? ??? ??? return mapping.findForward(""):
??? ??? }
??? }??? ???
(2.2)委托action???
??? 编写一个Struts action,包含在Spring应用上下文中的真正Struts action的代理. 代理action从ContextLoaderPlugIn
??? 中获取应用上下文,从中查找真正的Struts action,然后将处理委托给真正的Struts action.
??? 例如:
??? ??? public class **Action extends Action{
??? ??? ??? public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
??? ??? ??? ??? **Service.get**();
??? ??? ??? ??? ...;
??? ??? ??? }???
??? ??? ??? private **Service courseService;
??? ??? ??? public void setCourseService(**Service **){
??? ??? ??? ??? this.***=**;
??? ??? ??? }
??? ??? }??? ???
??? ??? 注入**Service
??? ??? 在struts-config.xml中注册Struts action代理.
??? ??? Spring提供了org.springframework.web.struts.DelegatingActionProxy
??? ??? 只需要在struts-config.xml里面设置这个action就可以了.
??? ??? <action path="" type="org.springframework.web.struts.DelegatingActionProxy"/>???
??? ??? 做为 Spring Bean编写Struts Action
??? ??? <bean name=""??? class="">
??? ??? ??? <property name="**Service">
??? ??? ??? ??? <ref bean=""/>
??? ??? ??? </property>
??? ??? </bean>??? ???
??? ??? name的值必需和struts-config.xml中的path一致;???