原文1
Web MVC with the Spring Framework
Juergen Hoeller
1. Introduction: Spring the Application Framework
When first confronted with the Spring Framework, one might be tempted to think: 'Oh no, not yet another web framework'. This article will outline why Spring isnt particularly a web framework but a generic lightweight application framework with dedicated web support, and show the architectural differences to Struts and WebWork In contrast to Struts or WebWork, Spring is an application framework for all layers: It offers a bean configuration foundation, AOP support, a JDBC abstraction framework, abstract transaction support, etc. It is a very non-intrusive effort: Your application classes do not need to depend on any Spring classes if not necessary, and you can reuse every part on its own if you like to. From its very design, the framework encourages clean separation of tiers, most importantly web tier and business logic: e.g. the validation framework does not depend on web controllers. Major goals are
reusability and testability: Unnecessary container or framework dependencies can be considered avoidable evils.
Of course, Springs own web support is nicely integrated with the frameworks general patterns. Nevertheless, replacing the web solution with Struts, WebWork, or the like is easy. Both with Springs web support or a different one, Spring allows for building a true dedicated middle tier in the web container, with the option to reuse exactly the same business logic in test environments or standalone applications. And within J2EE, your business logic will not unnecessarily depend on container services like JTA or EJB - allowing complex, well-architected web applications to run in a 'simple' container like Tomcat or Resin.
Note that Spring doesnt generally aim to compete with existing solutions. It rather fosters seamless integration with standards like Servlet, JSP, JTA, JNDI, JDBC, and JDO, and well-suited tools like Hibernate, Velocity, Log4J, and Cauchos Hessian/Burlap. The framework is designed to grow with the needs of your applications, in terms of technology choice: For example, you will probably use JTAvia Springs JtaTransactionManager if you need distributed transactions - but only then, as there are perfect replacements for single databases, like DataSourceTransactionManager or HibernateTransactionManager.
2. Web MVC: The Design of Springs Web Framework
Springs web framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, and locale and theme resolution. The default handler is a very simple Controller interface, just offering a 'ModelAndView handleRequest(request,response)' method. This can already be used for application controllers, but you will prefer the included implementation hierarchy, consisting of AbstractController,AbstractCommandController,MultiActionController, SimpleFormController,AbstractWizardFormController. Application controllers will typically be subclasses of those. Note that you can choose an appropriate base class: If you dont have a form, you dont need a FormController. This is a major difference to Struts.
You can take any object as command or form object: Theres no need to implement an interface or derive from a base class. Springs data binding is highly flexible, e.g. it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. So you dont need to duplicate your business objects properties as Strings in your form objects, just to be able to handle invalid submissions, or to convert the Strings properly. Instead, its often preferable to bind directly to your business objects. This is another major difference to Struts which is built around required base classes like Action and ActionForm - for every type of action. Compared to WebWork, Spring has more differentiated object roles: It supports the notion of a Controller, an optional command or form object, and a model that gets passed to the view. The model will normally include the command or form object but also arbitrary reference data. Instead, a WebWork Action combines all those roles into one single object. WebWork does allow you to use existing business objects as part of your form, but just by making them bean properties of the respective Action class. Finally, the same Action instance that handles the request gets used for evaluation and form population in the view. Thus, reference data needs to be modelled as bean properties of the Action too. These are arguably too many roles in one object.
Regarding views: Springs view resolution is extremely flexible. A Controller implementation can even write a view directly to the response, returning null as ModelAndView. In the normal case, a ModelAndView instance consists of a view name and a model Map, containing bean names and corresponding objects (like a command or form, reference data, etc). View name resolution is highly configurable, either via bean names, via a properties file, or via your own ViewResolver implementation. The abstract model Map allows for complete abstraction of the view technology, without any hassle: Be it JSP, Velocity, or anything else - every renderer can be integrated directly. The model Map simply gets transformed into an appropriate format, like JSP request attributes or a Velocity template model.
3. Integration: Using a Different Web Framework with Spring
Many teams will try to leverage their investments in terms of know-how and tools, both for existing projects and for new ones. Concretely, there are not only a large number of books and tools for Struts but also a lot of developers that have experience with it. Thus, if you can live with Struts architectural flaws, it can still be a viable choice for the web layer. The same applies to WebWork and other web frameworks. If you dont want to use
剩余内容已隐藏,支付完成后下载完整资料
原文1
Spring的web MVC 构架模式
Juergen Hoeller
1.介绍:Spring 应用构架
当第一次面对Spring框架,人们可能会想:“哦不,又一种web构架”。这篇文章将告诉你Spring是不是特别的Web框架,但与专门的Web支持一般轻量级的application framework,并展示给Struts和WebWork的结构差异。与此相反的Struts或WebWork的,Spring是所有层的application framework:它提供了一个bean的配置基础,AOP支持,JDBC的提取框架,抽象事务支持,等等。这是一个非常著的特点:您的应用程序类做不需要,如果没有必要依赖于任何Spring的class,如果你喜欢,只使用它的某一部分的功能。从它的设计理念,你可以看到String 帮助你实现了真正的逻辑层和web层的分离:例如。一个校验应用将不用依靠controllers,就可以实现。这样的目标是 更好的重用和易测:可重用性和可测试性:不必要的容器或框架的依赖可以被认为是可以避免的罪恶。
当然,Spring的自己的web支持是很好的集成框架的一般模式。尽管如此,Spring替换struts,webwork或者其他的web方案非常的容易。对于Spring的web支持或者不同的地方,Spring允许你在web容器里面建立一个中间层,可以选择准确重用在测试环境或独立应用程序相同的业务逻辑。和J2EE环境里面,你的商务逻辑不必依靠像JTA,EJB容器提供的服务 - 让复杂的,良好架构的Web应用程序在如Tomcat或Resin一个“简单”的容器中运行。
需要注意的是,Spring不是和已经存在的解决方案进行竞争。我们鼓励结合,如,Servlet,JSP,JTA,JNDI,JDBC和JDO和非常适合的工具如Hibernate,Velocity,Log4J,和Cauchos Hessian/Burlap标准的无缝集成。该框架的设计与应用程序的需求增长,在技术的选择:例如,如果你需要分布式事务处理,你可能需要用Spring的
Jta TransactionManager 来实现JTA服务。或者用DataSourceTransactionManager or HibernateTransactionManager来实现。
- Web MVC:Spring web 框架的设计思想
Spring 框架通过配置操作mappings,展示resolution,本地化和模版集成围绕着分派请求操作的servlet - DispatcherServlet设计的.缺省的操作是一个非常简单的控制接口, 他只提供了ModelAndView
handleRequest(request,response)方法.这将用于一个应用的控制,但是,如果你想包含多个层次的控制,AbstractController, AbstractCommandController,MultiActionController,SimpleFormController,AbstractWizardFormController 将帮助你完成.应用控制将代表那些子系统.注意,你可以选择一个适当的基类:如果你没有 web form,你就不必用FormController.这就是和Struts最大的不同. 你可以通过命令或者form对象来操作任何对象:这不需要接口工具或者一个基础类的驱动.Spring的数据邦定是非常的灵活的.举例来说,它描述了具有在应用范围内的校验错误的输入机制,但不是系统错误.所以在你的form对象里面你不必复制你的业务对象的string属性,只操作出错的子任务,或者适当地转换string.换句话说,它通常可以很好的直接邦定你的业务对象. 这也是和struts围绕请求基础类Action 和 ActionForm (每个action操作类型) 建立主要的不同之一. 对比
WebWork,Sping更多的区别在于对象角色:Sping支持控制器的感念,一个操作命令或者form对象,和得到数据传递给视图的模式.这个模式通常包含命令和form对象,但有时也包含任意的参考数据.换句话说,一个WebWork Action联合所有这些角色到一个单独的对象.WebWork 允许你用已经存在的业务对象作为你 form 的一部分,但是只生成各自Action 的 bean 属性. 最后,操作请求的Action 实例在一个视图里面获得付值和form population. 然而,参考数据也需要作为Action 的属性被模拟. 一个类里面有太多的角色是值得讨论的. 关于视图:Spring的视图方案非常的灵活. 一个控制器执行可以通过response 返回ModelAndView对象null,就可以直接写到一个视图.在通常的状况下,一个ModelAndView实例结合了 一个view 和一个 model Map,包含了bean name 和 通讯对象(像命令或者form,参考数据等等). View名称是非常高端的的配置,不是通过bean name, 一个properties 文件就是通过你自己的ViewResolver.这个抽象的model Map 允许你在视图层面完成提取,没有任何的争辩JSP/Velocity或者其他,每一种都可以直接完整使用.这个model Map 还可以简单得得到适当的格式化数据的转换,像JSP 请求属性或者Velocity 模版模式.
3.集成:用Spring一个不同web 框架
很多开发团队会尽量利用其已经获得的期限和工具方面,无论是现有的项目和新的项目。具体而言,这里没有像Struts那样大量的图书和工具,但也有很多是有这方面的经验的开发人员。因此,如果你能忍受Struts的架构性缺陷,它仍然是web层一个很好的选择。这同样适用WebWork和其它web框架。
如果你不想使用Spring的web MVC,但是想充分利用Spring提供的其它解决方案,可以轻松集成您选择使用Spring的Web框架。只需通过ContextLoadListener启动Spring root application context,并通过Struts或者WebWork的action利用ServletContext属性(或者Spring的helper方法)存取它。请注意,没有任何的“plugins”,因此没有专门的集成:来自web层的角度看,你会简单地使用Spring作为一个管理application context 实例入口点的类库。所有你注册bean和Spring的服务可以在您的指尖,即使没有Spring的web。 Spring并没有在使用方法和Struts或WebWork的竞争,它只是解决了许多地区这些纯粹的web框架所没有的功能,从bean配置,数据访问和事务处理。所以,你可以利用Spring提供的中间层和数据存储层来丰富你的应用,甚至,你想用JDBC或者Hibernate进行事务抽象.
4.特点核对列表
如果聚焦于web 支持,Spring的一些显著特点是: 清楚地角色分离:controller , validator , command object , form object , model object,和 DispatcherServlet , handler mapping vs view resolver, 等等 强大而且直接的框架和作为JavaBeans的应用配置,包括简单的参照和应用内容,例如,从web控制器到业务对象和数据校验. 适应性,外挂:无论什么样的控制器你都需要得到代替Action/ActionForm所做的每件事情的方案(简单,命令,form,范例,多重action,或者定制一个) 重用业务逻辑代码,不需要复制:你可以用已经有的业务逻辑对象作为命令或则form对象代替反射特定的ActionForm子类. 可订制的邦定和数据校验:作为应用级的输入机制的错误校验,固定日期和数字邦定,例如,通过手动检查和转换业务对象来替换单个的string。 可订制的操作,可订制的视图方案:通过name/value Map灵活的模型传送,用一个简单的方法从简单到复杂操作映射和视图方案策略.
本地定制和主题方案:Spring没有taglib,完全支持于JSPs,JSTL,不需要额外的连接就可以支持Velocity,等等. 简单但是强大的tag library避免了HTML产生的混乱,最大限度的灵活扩展了标记代码.
5.spring和springmvc的差别
spring 是是一个开源框架,是为了解决企业应用程序开发,功能如下
◆目的:解决企业应用开发的复杂性
◆功能:使用基本的JavaBean代替EJB,并提供了更多的企业应用功能
◆范围:任何Java应用
简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
◆轻量——从大小与开销两方面而言Spring都是轻量的。完整的Spring框架可以在一个大小只有1MB多的JAR文件里发布。并且Spring所需的处理开销也是微不足道的。此外,Spring是非侵入式的:典型地,Spring应用中的对象不依赖于Spring的特定类。
◆控制反转——Spring通过一种称作控制反转(IoC)的技术促进了松耦合。当应用了IoC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创建或者查找依赖对象。你可以认为IoC与JNDI相反——不是对象从容器中查找依赖,而是容器在对象初始化时不等对象请求就主动将依赖传递给它。
◆面向切面——Spring提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑与系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。 ◆容器——Spring包含并管理应用对象的配置和生命周期,在这个意义上它是一种容器,你可以配置你的每个bean如何被创建——基于一个可配置原型(prototype),你的bean可以创建一个单独的实例或者每次需要时都生成一个新的实例——以及它们是如何相互关联的。然而,Spring不应该被混同于传统的重量级的EJB容器,它们经常是庞大与笨重的,难以使用。
◆框架——Spring可以将简单的组件配置、组合成为复杂的应用。在Spring中,应用对象被声明式地组合,典型地是在一个XML文件里。Spring也提供了很多基础功能(事务管理、持久化框架集成等等),将应用逻辑的开发留给了你。 所有Spring的这些特征使你能够编写更干净、更可管理、并且更易于测试的代码。它们也为Spring中的各种模块提供了基础支持。
Spring的两大核心AOP与IOC,可以单独用于任何应用,包括与Struts等MVC框架与Hibernate等ORM框架的集成,目前很多公司所谓的轻量级开发就是用 Spring Struts(2) Hibernate。
Spring MVC就是一个MVC框架,个人觉得Spring MVC annotation式的开发比Struts2方便,可以直接代替上面的Struts(当然Struts的做为一个非常成熟的MVC,功能上感觉还是比Spring强一点,不过Spring MVC已经足够用了)。当然spring mvc的执行效率比struts高,是因为struts的值栈影响效率。
spring mvc类似于struts的一个MVC开框架,其实都是属于spring,spring mvc需要有spring的架包作为支撑才能跑起来。
原文2
一种基于Hibernate和Struts的J2EE应用开发策略
本文分析了Hibernate和Struts的机制,一种基于Hibernate和Struts的J2EE应用开发策略.在这种策略,模型层使用Hibernate实现,视图,控制器再使用Struts框架.这样可以大大降低开发效率的代码匹配性和提高系统的开发效率。
关键字 Hibernate,Struts,MVC,持久层
1 .序言:随着逐步成熟和完善的技术的Java,在标准的平台建立企业级应用中,J2 EE平台得到了长足的发展。几个技术要求帮助从在J2 EE规范包括:Enterprise JavaBean(EJB)、Java Servlets(Servlet)、Java Server Pages(JSP)、Java Message Service(JMS)等,开发大量应用系统.但是,也出现了一些问题,在传统的J2 EE开发过程中的应用:
1)数据模型和逻辑模型之间的矛盾。用法基本上目前数据库都是关系型数据库,但Java是本质上是一种其面向对象的语言,对象为具有读取使用SQL和JDBC节省进行数据库操作,降低效率和系统的可维护性;
- J2EE应用的传统多采用基于EJB的重量级框架,这种框架适于开发的大型企业应用程序,但使用EJB容器进行开发和调试需要浪费了大量的的时间。对于降低代码的耦合性,开发效率提高系统,本文提出了一种基于Struts框架和Hibernate框架的J2EE应用开发策略。
2 .数据持久层及Hibernate Hibernate是一个数据持久层框架,是一种实现对象和关系之间映射(O/R Mapping)的工具,它对JDBC进行了轻量级的对象封装,使程序员可以使用对象编程思想来操作数据库。它不仅提供了从Java类到数据表的映射,也提供了数据查询和恢复机制。相对于使用JDBC和SQL来操作数据库,使用Hibernate能大大的提高实现的效率。Hibernate框架
剩余内容已隐藏,支付完成后下载完整资料
资料编号:[153853],资料为PDF文档或Word文档,PDF文档可免费转换为Word
您可能感兴趣的文章
- 饮用水微生物群:一个全面的时空研究,以监测巴黎供水系统的水质外文翻译资料
- 步进电机控制和摩擦模型对复杂机械系统精确定位的影响外文翻译资料
- 具有温湿度控制的开式阴极PEM燃料电池性能的提升外文翻译资料
- 警报定时系统对驾驶员行为的影响:调查驾驶员信任的差异以及根据警报定时对警报的响应外文翻译资料
- 门禁系统的零知识认证解决方案外文翻译资料
- 车辆废气及室外环境中悬浮微粒中有机磷的含量—-个案研究外文翻译资料
- ZigBee协议对城市风力涡轮机的无线监控: 支持应用软件和传感器模块外文翻译资料
- ZigBee系统在医疗保健中提供位置信息和传感器数据传输的方案外文翻译资料
- 基于PLC的模糊控制器在污水处理系统中的应用外文翻译资料
- 光伏并联最大功率点跟踪系统独立应用程序外文翻译资料
