0.加载struts
在web.xtm中
<!-- Struts Action Mapping-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
1.加载spring应用环境
在web.xml用
<!-- Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔此参数用于后面的Spring-Context loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
<!-- //如果不配置contextConfigLocation,会默认找/WEB-INF/applicationContext.xml文件
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-common.xml</param-value>
-->
</context-param>
<!-- 配置spring上下文载入器,即Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
来加载spring的应用环境
或在struts-config.xml中注册Spring插件
<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml"/>
</plug-in>
来注册Spring插件!
2. Struts获得spring的中配置的bean的方式
(1) 利用WebApplicationContextUtils的getRequiredWebApplicationContext获得spring的应用环境
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext);
Object obj = ctx.getBean("beanName");
(2)继承Spring的ActionSupport,然后用getRequiredWebApplicationContext获得spring的应用环境
getRequiredWebApplicationContext().getBean("beanName");
3.捆绑Spring中的动作,即用spring处理action,不用ActionSupport
(1)在struts-config.xml中,用spring的DelegatingRequestProcessor代替struts默认的RequestProcessor
<action-mappings>
<action path="/adminLogin" className="org.springframework.web.struts.DelegatingRequestProcessor">
<forward name="success" path="/pages/adminLogin.jsp"></forward>
</action>
</action-mappings>
然后在applicationContext-action.xml中用
<bean name="/adminLogin" class="com.company.mypro.AdminLoginAction">
<property name="adminManager" ref="adminManger"/>
</bean> 就行了.
备注: struts-config.xml中
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
完整的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Spring ApplicationContext配置文件的路径 ,可使用通配符,多个路径用,号分隔此参数用于后面的Spring-Context loader -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
<!-- //如果不配置contextConfigLocation,会默认找/WEB-INF/applicationContext.xml文件
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/applicationContext-common.xml</param-value>
-->
</context-param>
<!-- 配置spring上下文载入器,即Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- Struts Action Mapping-->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml,/WEB-INF/modules/struts-config-admin.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
strust与spring结合
最新推荐文章于 2021-10-29 13:28:32 发布