首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring与Struts集验方式二

2012-10-14 
Spring与Struts集成方式二在集成方式一的基础上做改进:第一种集成方案是在action中通过WebApplicationCont

Spring与Struts集成方式二

在集成方式一的基础上做改进:

第一种集成方案是在action中通过WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()); UserDao userDao = (UserDao)wac.getBean("userDao");      来解决。第二集成方案是通过spring注入,即把action配置在spring配置文件中,并把要用到的dao对象做为action bean的属性注入。我们创建一个新的配置文件applicationContext-actions.xml<bean name="loginAction" ref="userDao"></property></bean>但这样还是使用struts的action,是单实例的,那么我们想使用spring的action怎么配置呢有两种配置方法:不管哪种配置方法首先应保持struts-config.xml中的path与applicationContext-actions.xml中的bean的name一致。这是因为ActionServlet先是找struts-config.xml中的path,而path又是唯一的,那么要获得spring提供的action,只需在spring的配置中采用name与path一样即可。第一种:使用代理的action类:DelegatingActionProxy方法:使用spring的代理action,所以将struts-config中的action的type更改org.springframework.web.struts.DelegatingActionProxy注意到spring源代码下的spring-framework-2.5.6.SEC01\dist\modules目录加入:spring-webmvc-struts.jar如:<action path="/login" type="org.springframework.web.struts.DelegatingActionProxy"name="loginForm"><forward name="success" path="/login_success.jsp"></forward></action>并且我们在spring配置中可以加入scope="prototype"即不通过单实例的方式创建,现在就可以解决struts单实例而引起的线程不安全问题。第二种:使用spring的代理DelegatingRequestProcessor覆盖struts的RequestProcessor。方法:为了在 struts-config.xml 文件中配置 DelegatingRequestProcessor,你需要重载 <controller> 元素的 “processorClass” 属性。 下面的几行应该放在 <action-mapping> 元素的后面。 <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"/>如果使用 Struts 的 modules 特性,则 bean 命名必须含有 module 的前缀。 举个例子,如果一个 Action 的定义为 <action path="/user"/>,而且它的 module 前缀为“admin”, 那么它应该对应名为 <bean name="/admin/user"/> 的 bean。 配置完成后我们再看看LoginAction中,直接使用userDao.add(name, pwd);不再需要通过WebApplicationContext获得dao再执行方法。

?

看看配置完后的代码:

action中代码:

package com.lwf.spring.web.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.springframework.web.context.WebApplicationContext;import org.springframework.web.context.support.WebApplicationContextUtils;import com.lwf.spring.web.dao.UserDao;import com.lwf.spring.web.form.LoginForm;public class LoginAction extends Action {private UserDao userDao;public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {LoginForm loginForm = (LoginForm)form;String name = loginForm.getName();String pwd = loginForm.getPassword();userDao.add(name, pwd);return mapping.findForward("success");}public void setUserDao(UserDao userDao) {this.userDao = userDao;}}

?

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><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>  <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>

?

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" type="org.springframework.web.struts.DelegatingActionProxy"name="loginForm"><forward name="success" path="/login_success.jsp"></forward></action></action-mappings><message-resources parameter="MessageResources"></message-resources></struts-config>

?

新建的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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean name="/login" 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><bean id="userDao" ></bean></beans>

?

其它文件不变。

具体代码见spring_struts_2

热点排行