使用 DelegatingRequestProcessor(1)
查看 Struts 的源代码,我们可以看到由 ActionServlet 调用 RequestProcessor 完成实际的转发。如想在 ActionServlet 处将请求转发给 ApplicationContext 的 bean,可以通过扩展 RequestProcessor 完成,使用扩展的 RequestProcessor 替换 Struts 的 RequestProcessor 。Spring 能完成这种扩展,因为Spring 提供的 DelegatingRequestProcessor 继承RequestProcessor。为了让Stn血使用I>elegatU1gFtequestProcessor,还需要在 struts-config.xml文件中增加如下代码:
//使用 spring 的 RequestProcessor 替换 struts 原有的 RequestProcessor <controller processorClass="org.springframework.web.struts.Delegating RequestProcessor"/> |
//配置 struts action 时候,指定了实现类 <action path="/user" type="lee.UserAction"/> //配置 struts action时,没有指定实现类。 <action path="/user"/> |
<!--XML 文件版本,编码集> <?xml version="1.0" encoding="gb2312"?> <!--struts配置文件的文件头,包括dtd等信息--> <!DOCTYPE struts-config PUBLIC'-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" ''http://struts.apache.org/dtds/struts-config_1_2.dtd"> <!--struts配置文件的根元素--〉 <struts-config> <!-- 配置 formbean. 所有的 formbean都放在 form-beans元素里定义--〉 <form-beans><!定义了一个formbean. 确定 formbean名和实现类--〉 <form-bean name="loginForm" type="lee.LoginForm"/ form-beans><'定义 action部分,所有的action都放在action-mapping元素里定义--> <action-mappings> <!这里只定义了一个action. 而且没有指定该action的 type元素--〉 <actionpath="/login" name="loginForm" scope="request" validate="true" input="/login.jsp" > <!--定义 action内的两个局部 forward 元素--〉 <forward name="input" path="/login.jsp"/><forward name="welcome" path="/welcome.html"/><faction> <faction-mappings> <!--使用DelegatingRequestProcessor替换 RequestProcessor--> <controller processorClass="org.springframework. web.struts.DelegatingRequestProcessor"/> <!--加载国际化的资源包--〉 <message-resources parameter="mess"/> <!--- 装载验证的资源文件--〉 <plug-in className="org.apache.struts.validator. ValidatorPlugIn"><set-property property="pathnames" value="/WEB-INF/validator-rules. xml , /WEB-INF/validation.xml"/><set-property property="stopOnFirstError" value="true"/></plug-in> <!-- 装载 Spring配置文件,随应用启动创建ApplicationContext实例--〉 <plug-in className="org.springframework.web.struts. ContextLoaderPlugIn"><set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml, /WEB-INF/action-servlet.xml"/> </plug-in〉 </struts-config> |
修改后的struts-config.xml文件中加载了国际化资源文件,其配置Struts 的 action不需要 class属性,也可完成ApplicationContext的创建。
由于程序没有使用web.xml文件加载Spring容器实例,因此无须增加ContextLoader的配置。仅仅增加了Struts 的标签库配置,主要用于程序国际化等方面。在Struts 标签库配置中增加如下代码即可:
<'配置bean标签--> <taglib> <taglib-uri>/tags/struts-bean</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-locatio口〉 </taglib> <!配置 html 标签> <taglib> <taglib-uri>/tags/struts-html</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib- location></taglib><!--配置 logic 标签--〉 <taglib> <taglib-uri>/tags/struts-logic</taglib-uri> <taglib-locatio口>/WEB-INF/struts-logic.tld</taglib-location> </taglib> |
<?xml version="1.0" encoding="gb2312"?> <!--指定 Spring 配置文件的dtd> <!DOCTYPE beans PUBLIC"-//SPRiNG//DTDBEAN//EN" ''http://www.springframework.org/dtd/spring-beans.dtd"> <!-- spring配置文件的根元素--〉 <beans> <!--每个request请求产生新实例,所以将action 配置成non-singleton--> <bean name="/login"class="lee.LoginAction"singleton="false"> <!--配置依赖注入--> <property name="vb"><!--引用容器中另外的bean--> <ref bean="vb"/> </property> </bean></beans> |
由于每次请求时,都应该启动新的action来处理用户请求,因此应将actionbean配置成 non-singleton行为。
注意: ActionServlet转发请求时,是根据bean的 name属性,而不是id属性。因此,此处确定的name属性应与 Struts 的 action属性相同。
applicationContext.xml 只有一个 bean 配置(配置了 vb bean)。其详细配置如下:
<?xml version="l.O" encoding="gb23l2"?> <!--指定 Spring 配置文件的dtd> <lDOCTYPE beans PUBLIC"-//SPRING//DTD BEAN//EN"''http://www.springframework.org/dtd/spring-beans.dtd''> <!-- spring配置文件的根元素 -> <beans> <!--配置ValidBean实例--〉 <bean id="vb" class="lee.ValidBeanImpl"/></beans> |