`

Spring与Struts集成方式三

阅读更多

在集成方式一和二中我们是在web.xml中加入配置代码:

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

 

在集成方式二中,为了使用spring的action我们需要在struts的配置中修改type为代理的action如:

<action-mappings>   
        <action path="/loginjsp" forward="/login.jsp"></action>   
        <action path="/login"    
            type="org.springframework.web.struts.DelegatingActionProxy"  
            name="loginForm">   
            <forward name="success" path="/login_success.jsp"></forward>       
        </action>   
    </action-mappings> 

 

而在applicationContext-actions.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: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"
		
>
	
	<bean name="/login" class="com.lwf.spring.web.action.LoginAction" scope="prototype">
		<property name="userDao" ref="userDao"></property>
	</bean>
</beans>

 

 

本文就是根据spring Framwork开发手册对上述做一些改进,改进后的配置只需要修改struts-config.xml即可。

1、对于之前在web.xml中加入的代码,我们在struts-config.xml使用插件来实现

如:在struts配置文件中加入

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  		<set-property property="contextConfigLocation"
    		  value="/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml"/>
	</plug-in>

 

即可替代web.xml中的内容。

2、对于我们使用DelegatingActionProxy,因而要修改struts配置文件中的type属性,那么如果每一个都要改势必麻烦。那么我们可以使用DelegatingRequestProcessor来解决。我们只需要在struts配置文件中加入

<controller>
  		<set-property property="processorClass"
     		 value="org.springframework.web.struts.DelegatingRequestProcessor"/>
	</controller>

 

增加这些设置之后,不管你查询任何类型的 Action,Sping都自动在它的context配置文件中寻找。 实际上,你甚至不需要指定类型。下面两个代码片断都可以工作:

<action path="/user" type="com.whatever.struts.UserAction"/>		
<action path="/user"/>

 

 

现在要集成spring与struts要怎么做呢。

1、创建web项目,加入struts的所有包和配置文件。可以创建LoginAction测试是否成功。

2、加入spring的所有包,并在src目录创建applicationContext-beans.xml和applicationContext-actions.xml文件

其中applicationContext-beans.xml专门用于注入daoBean,而applicationContext-actions.xml用于写一些与struts-config.xml相对应的action注入。

3、在struts-config.xml中加入plugin和controller即上面所讲的。

4、在applicationContext-actions.xml加入action bean的name必须与struts-config.xml中的path一样

好了。我们把改进后的完整代码贴出来:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="spring" version="2.5">
  <display-name>TestWeb</display-name>
  <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>2</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>action</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 applicationContext-actions.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: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"
		
>
	
	<bean name="/login" class="com.lwf.spring.web.action.LoginAction" scope="prototype">
		<property name="userDao" ref="userDao"></property>
	</bean>
</beans>

 

applicationContext-beans.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: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"
		
>
	<bean id="userDao" class="com.lwf.spring.web.dao.UserDaoImpl" ></bean>
</beans>

 最后是struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
	<form-beans>
		<form-bean name="loginForm" type="com.lwf.spring.web.form.LoginForm"></form-bean>
	</form-beans>
	<action-mappings>
		<action path="/loginjsp" forward="/login.jsp"></action>
		<action path="/login" 
			name="loginForm">
			<forward name="success" path="/login_success.jsp"></forward>	
		</action>
	</action-mappings>
	
	<controller>
  		<set-property property="processorClass"
     		 value="org.springframework.web.struts.DelegatingRequestProcessor"/>
	</controller>
	
	
	<message-resources parameter="MessageResources"></message-resources>
	<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
  		<set-property property="contextConfigLocation"
    		  value="/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml"/>
	</plug-in>
	
</struts-config>

 

注意看我们的

<action path="/login" 
			name="loginForm">
			<forward name="success" path="/login_success.jsp"></forward>	
		</action>

 

中省略了type也是可以的,因为现在由spring来注入action了。。

 

因为准备做ssh的集成,所以项目名称改成了SpringStrutsHibernate,不过这里面只包括了spring+struts的集成,实际就是对集成方式二的改进。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics