Spring和struts整合的两种方式

  • 第一种

web.xml

<context-param>
	spring配置文件application.xml  	
	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:application.xml</param-value>
  </context-param>
  
  <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>
  	<init-param>
  		<param-name>debug</param-name>
  		<param-value>3</param-value>
  	</init-param>
  	<init-param>
  		<param-name>detail</param-name>
  		<param-value>3</param-value>
  	</init-param>
  	<load-on-startup>0</load-on-startup>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>action</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>

 struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<struts-config>
	
	<form-beans>
		<form-bean name="loginForm" type="wsl.form.LoginForm"/>
	</form-beans>
	
	<action-mappings>
		<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy" name="loginForm">
			<forward name="success" path="/success.jsp"/>
		</action>
	</action-mappings>
	<!-- 加载spring框架   -->
	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
		<set-property property="contextConfigLocation" value="classpath:application.xml"/>
	</plug-in>
</struts-config>

 Spring配置文件application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
     xmlns:context="http://www.springframework.org/schema/context"   
      xmlns:aop="http://www.springframework.org/schema/aop"   
      xmlns:tx="http://www.springframework.org/schema/tx"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
    http://www.springframework.org/schema/aop     
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd    
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    
    http://www.springframework.org/schema/context    
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	<bean id="loginServiceImp" class="wsl.service.imp.LoginServiceImp">
	</bean>
	<bean name="/login" class="wsl.action.LoginAction">
		<property name="loginService" ref="loginServiceImp"/>
	</bean>
</beans>
  •  第二种

web.xml中增加

<servlet>   
	    <servlet-name>context</servlet-name>   
	    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>   
	    <load-on-startup>1</load-on-startup>   
</servlet>

 struts-config.xml中将加载spring框架替换成

<!-- 把本文件中的action委托给spring框架管理    --> 
<controller>   
	<set-property property="processorClass"   
		value="org.springframework.web.struts.DelegatingRequestProcessor"/>   
</controller>

 删除action中的type="org.springframework.web.struts.DelegatingActionProxy"

猜你喜欢

转载自wsl198632.iteye.com/blog/1569064