如何集成Spring和Struts(实例说明)

1.Struts和Springjava

Struts 表明了MVC第二类架构的实现,在Struts中最重要的组件是ActionServlet,Action和 ActionForm 子类,ActionServlet 表明controller,他基于配置文件接受请求和 把这些请求转发到相应的ActionForm和Action子类。 ActionForm把用户输入的数据传送到Action,Action调用商务层组件完成必要的操做,最后提交到view。ActionServlet使用一个配置文件(struts-config.xml)加载Action子类的定义用以接受用户请求,基于请求URL, controller 找到一个action定义去接受这个请求,Struts构件处理用户请求, 检查配置文件, 完成相应的动做。web

Spring是一种轻量级的容器,它使得使用一个外部XML配置文件很是容易绑定对象,每一个对象可以经过列出JavaBean属性获得一个依赖 对象的指针,经过绑定XML配置文件使剩下的工做更加简单。依赖注入(DI)是很是强大的功能,Spring支持可插拔的事务管理器,提供事物管理方式更 多的选择. 它集成了持久性的架构同时也提供了一个统一的exception 分类,Spring也提供面向方面(AOP)编程的简单机制。spring

2.Struts和Spring的集成apache

将Struts应用集成到Spring框架能够采用多种方法,首先Spring明显地被设计用于解决JEE的现实问题,如复杂性,性能低下,可测试性及其余;第二,Spring框架包括一个AOP实现让你可使用面向方面的程序设计技术;第三, Spring 框架能够可以很是容易地管理和协调Struts;和Struts相似,Spring也包含MVC 实现,两个架构都有优缺点,Struts是MVC最重要的架构,不少开发团队学会了依靠Struts在规定时限内开发出高质量的软件,所以开发团队宁愿 集成Spring的功能也不肯意转到Spring MVC;好消息是Spring的结构容许你集成Struts Web 框架、基于Spring的业务层和持久层,咱们的方法是应用Spring中的ActionSupport类去集成Struts。编程

3.加载应用的context架构

首先咱们须要使用Spring中的ContextLoaderPlugin为Struts ActionServlet去装载Spring应用的上下文,简单在struts-config.xml 文件中增长plug-in,以下(1)所示:app

﹤ ?xml version="1.0" encoding="ISO-8859-1" ?﹥框架

﹤ !DOCTYPE struts-config PUBLICjsp

"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"性能

"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"﹥

﹤ struts-config﹥

﹤ form-beans﹥

﹤ form-bean name="searchForm"

type="org.apache.struts.validator.DynaValidatorForm"﹥

﹤ form-property name="cardno" type="java.lang.String"/﹥

﹤ /form-bean﹥

﹤ /form-beans﹥

﹤ global-forwards type="org.apache.struts.action.ActionForward"﹥

﹤ forward name="welcome" path="/welcome.do"/﹥

﹤ forward name="searchEntry" path="/searchEntry.do"/﹥

﹤ forward name="searchSubmit" path="/searchSubmit.do"/﹥

﹤ /global-forwards﹥

﹤ action-mappings﹥

﹤ action path="/welcome" forward="/WEB-INF/pages/welcome.htm"/﹥

﹤ action path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/﹥

﹤ action path="/searchSubmit"

type=" com.infotek.Creditcard.actions.SearchSubmit"

input="/searchEntry.do"

validate="true"

name="searchForm"﹥

﹤ forward name="success" path="/WEB-INF/pages/detail.jsp"/﹥

﹤ forward name="failure" path="/WEB-INF/pages/search.jsp"/﹥

﹤ /action﹥

﹤ /action-mappings﹥

﹤ message-resources parameter="ApplicationResources"/﹥

﹤ plug-in className="org.apache.struts.validator.ValidatorPlugIn"﹥

﹤ set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/﹥

﹤ /plug-in﹥

﹤ plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"﹥ (1)

﹤ set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/﹥

﹤ /plug-in﹥

﹤ /struts-config﹥

4.使用Spring的ActionSupport类

要用Spring去集成Struts,建立一个Spring 上下文是必需要作的。 org.springframework.web.struts.ActionSupport 类提供一个 getWebApplicationContext() 方法很是容易地得到Spring上下文,所有你须要去作的是从Spring的ActionSupport 代替Struts 中的Action类去延伸你的action,以下所示:

package com.infotek.Creditcard.actions;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.DynaActionForm;

import org.springframework.context.ApplicationContext;

import org.springframework.web.struts.ActionSupport;

import com. infotek.Creditcard.beans.Creditcard;

import com. infotek.Creditcard.business.CreditcardService;

public class SearchSubmit extends ActionSupport { |(1)

public ActionForward execute(ActionMapping mapping,ActionForm form,

HttpServletRequest request,HttpServletResponse response)

throws IOException, ServletException {

DynaActionForm searchForm = (DynaActionForm) form;

String isbn = (String) searchForm.get("cardno");

//the old fashion way

//CreditcardService creditcardService = new CreditcardServiceImpl();

ApplicationContext ctx = getWebApplicationContext(); |(2)

CreditcardService creditcardService =

(CreditcardService ) ctx.getBean("creditcardService"); |(3)

CreditCard creditard = CreditCardService.read(cardno.trim());

if (null == creditard) {

ActionErrors errors = new ActionErrors();

errors.add(ActionErrors.GLOBAL_ERROR,new ActionError ("message.notfound"));

saveErrors(request, errors);

return mapping.findForward("failure") ;

}

request.setAttribute("creditcard", creditcard);

return mapping.findForward("success");

}

}

在(1)中,咱们经过延伸Spring ActionSupport 类而不是Struts Action 类建立了一个action;在(2)中,咱们使用getWebApplicationContext()方法得到一个 ApplicationContext;为了得到商务服务, 在(3)中,咱们使用ApplicationContext去查找Spring bean;这个技术很是容易理解,不幸的是它把Struts的action和Spring framework绑定了,若是你想替换Spring你不得不重写代码,并且Struts的action不在Spring的控制之下, 遗憾的是这种方法没法得到Spring AOP的好处。

5.结论

e良师益友尝试使用Spring的ActionSupport,ContextLoaderPlugIn去集成Struts,这是一种最高效的和最简单的方式,另外还可用Spring中的代理子类去代理Struts中的RequestProcessor和代理Struts的actions。

相关文章
相关标签/搜索