一. structs1.x插件集成
contextClass:可选,用于指定WebApplicationContext实现类,默认是XmlWebApplicationContext;
contextConfigLocation:指定Spring配置文件位置,如果我们的ActionServlet 在 web.xml 里面通过 hello指定名字为“hello”,且没有指定contextConfigLocation,则默认Spring配置文件是/WEB-INF/hello-servlet.xml;
namespace:因为默认使用ActionServlet在web.xml定义中的Servlet的名字,因此如果想要使用其他名字可以使用该变量指定,如指定“hello”,将加载的Spring配置文件为/WEB-INF/hello-servlet.xml;
//structs-config.xml
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextClass" value="org.springframework.web.context.support.XmlWebApplicationContext"/>
<set-property property="contextConfigLocation" value="/WEB-INF/hello-servlet.xml"/>
<set-property property="namespace" value="hello"/>
</plug-in>
(注:通用配置的Spring Web容器将作为ContextLoaderPlugin中创建的Spring Web容器的父容器存在,然而可以省略通用配置而直接在struts配置文件中通过ContextLoaderPlugin插件指定所有配置文件。)
二.集成Spring+Structs1.x
(注:使用插件方式时Action将在Spring中配置而不是在Struts中配置了)
Spring提供两种方式:
1.将Struts配置文件中的的type属性指定为DelegatingActionProxy,然后在Spring中配置同名的Spring管理的Action Bean;
//先在Structs的配置文件structs-config.xml中定义action
<action path="/hello3" type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
</action>
//在Spring配置文件中servlet-config.xml中定义对应Action的bean(即 bean的name与action的path要一致)
<bean name="/hello3" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
<property name="message" ref="message"/>
</bean>
2.DelegatingRequestProcessor方式与Spring集成:
//structs-config.xml
//1.换掉Struts默认的RequestProcessor,
<controller>
<set-property property="processorClass"
value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
//2.进行Action定义
<action path="/hello4" type=" cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
<forward name="hello" path="/WEB-INF/jsp/hello.jsp"/>
</action>
//在spring-config.xml定义action对应的bean(即 bean的name与action的path要一致)
<bean name="/hello4" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3">
<property name="message" ref="message"/>
</bean>
(注:Struts通过DelegatingRequestProcessor去到Spring Web容器中查找同名的Action Bean,没有代理Action,推荐使用该方式)