注册 登录  
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭






东北人! 狮子的獠牙、光速拳! 我很佩服163的办事认真,故乡一栏竟然能精确到乡,而且我们乡合并才不久。厉害!永远支持网易!


Web MVC with the Spring Framework(转贴)  

2006-09-21 22:12:24|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1. Introduction: Spring the Application Framework
1.介绍:Spring 应用构架

When first confronted with the Spring Framework, one might betempted to think: "Oh no, not yet another web framework". Thisarticle will outline why Spring isn't particularly a web frameworkbut a generic lightweight application framework with dedicated websupport, and show the architectural differences to Struts andWebWork

当你第一次看到Spring的时候,你一定会说:"哦 不,又一种web构架".这篇文章将告诉你Spring明显区别于其他轻量级applicationframework,
它将专注于web的支持,与struts 和 webwork有着明显的区别。

In contrast to Struts or WebWork, Spring is an applicationframework for all layers: It offers a bean configurationfoundation, AOP support, a JDBC abstraction framework, abstracttransaction support, etc. It is a very non-intrusive effort: Yourapplication classes do not need to depend on any Spring classes ifnot necessary, and you can reuse every part on its own if you liketo. From its very design, the framework encourages clean separationof tiers, most importantly web tier and business logic: e.g. thevalidation framework does not depend on web controllers. Majorgoals are reusability and testability: Unnecessary container orframework dependencies can be considered avoidable evils.

在和struts 和webwork的对比上,Spring是一个服务于所有层面的applicationframework:提供了bean的配置基础,AOP的支持,JDBC的提取框架,

Of course, Spring's own web support is nicely integrated with theframework's general patterns. Nevertheless, replacing the websolution with Struts, WebWork, or the like is easy. Both withSpring's web support or a different one, Spring allows for buildinga true dedicated middle tier in the web container, with the optionto reuse exactly the same business logic in test environments orstandalone applications. And within J2EE, your business logic willnot 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.

当然,Spring的自己的web支持和通常框架模式的细致完整.然而,Spring替换struts,webwork或者其他的web方案非常的容易.对于Spring的web支持或者不同的地方,Spring允许你在web容器里面建立一个中间层,在测试环境或者标准独立的应用里面来设置重用你的商务逻辑.还有在J2EE环境里面,你的商务逻辑不必依靠容器提供的服务,像JTA,EJB的支持.良好的构架的web应用可以运行在任何容器上,如,Tomcat 或者Resin.

Note that Spring doesn't generally aim to compete with existingsolutions. It rather fosters seamless integration with standardslike Servlet, JSP, JTA, JNDI, JDBC, and JDO, and well-suited toolslike Hibernate, Velocity, Log4J, and Caucho's Hessian/Burlap. Theframework is designed to grow with the needs of your applications,in terms of technology choice: For example, you will probably useJTA via Spring's JtaTransactionManager if you need distributedtransactions - but only then, as there are perfect replacements forsingle databases, like DataSourceTransactionManager orHibernateTransactionManager.

值得注意的是,Spring 不是和已经存在的解决方案进行竞争.我们鼓励结合标准的技术,如, Servlet, JSP, JTA, JNDI, JDBC, andJDO, 和非常匹配的工具,如,Hibernate, Velocity, Log4J, and Caucho'sHessian/Burlap.这个框架的的设计思想是在你的应用需要改良的时候,你将会做一些技术的选择:例如,如果你需要分布式事务处理,你可能需要用Spring的JtaTransactionManager来实现JTA服务.或者,用DataSourceTransactionManager orHibernateTransactionManager 来实现美妙完美的单个数据库交换。

2. Web MVC: The Design of Spring's Web Framework
Web MVC:Spring web 框架的设计思想

Spring's web framework is designed around a DispatcherServlet thatdispatches requests to handlers, with configurable handlermappings, view resolution, and locale and theme resolution. Thedefault handler is a very simple Controller inte***ce, justoffering a "ModelAndView handleRequest(request,response)" method.This can already be used for application controllers, but you willprefer the included implementation hierarchy, consisting ofAbstractController, AbstractCommandController,MultiActionController, SimpleFormController,AbstractWizardFormController. Application controllers willtypically be subclasses of those. Note that you can choose anappropriate base class: If you don't have a form, you don't need aFormController. This is a major difference to Struts.

Spring框架通过配置操作mappings,展示resolution,本地化和模版集成围绕着分派请求操作的servlet- DispatcherServlet设计的.缺省的操作是一个非常简单的控制接口,
他只提供了ModelAndViewhandleRequest(request,response)方法.这将用于一个应用的控制,但是,如果你想包含多个层次的控制,AbstractController,AbstractCommandController, MultiActionController,SimpleFormController, AbstractWizardFormController将帮助你完成.应用控制将代表那些子系统.注意,你可以选择一个适当的基类:如果你没有web form,你就不必用FormController.这就是和Struts最大的不同.

You can take any object as command or form object: There's no needto implement an inte***ce or derive from a base class. Spring'sdata binding is highly flexible, e.g. it treats type mismatches asvalidation errors that can be evaluated by the application, not assystem errors. So you don't need to duplicate your businessobjects' properties as Strings in your form objects, just to beable to handle invalid submissions, or to convert the Stringsproperly. Instead, it's often preferable to bind directly to yourbusiness objects. This is another major difference to Struts whichis built around required base classes like Action and ActionForm -for every type of action.

这也是和struts围绕请求基础类Action 和 ActionForm(每个action操作类型) 建立主要的不同之一.

Compared to WebWork, Spring has more differentiated object roles:It supports the notion of a Controller, an optional command or formobject, and a model that gets passed to the view. The model willnormally include the command or form object but also arbitraryreference data. Instead, a WebWork Action combines all those rolesinto one single object. WebWork does allow you to use existingbusiness objects as part of your form, but just by ****** them beanproperties of the respective Action class. Finally, the same Actioninstance that handles the request gets used for evaluation and formpopulation in the view. Thus, reference data needs to be modelledas bean properties of the Action too. These are arguably too manyroles in one object.

对比WebWork,Sping更多的区别在于对象角色:Sping支持控制器的感念,一个操作命令或者form对象,和得到数据传递给视图的模式.这个模式通常包含命令和form对象,但有时也包含任意的参考数据.换句话说,一个WebWorkAction联合所有这些角色到一个单独的对象.WebWork允许你用已经存在的业务对象作为你 form 的一部分,但是只生成各自Action的 bean 属性. 最后,操作请求的Action实例在一个视图里面获得付值和form population.然而,参考数据也需要作为Action 的属性被模拟.一个类里面有太多的角色是值得讨论的.

Regarding views: Spring's view resolution i* **tremely flexible. AController implementation can even write a view directly to theresponse, returning null as ModelAndView. In the normal case, aModelAndView instance consists of a view name and a model Map,containing bean names and corresponding objects (like a command orform, reference data, etc). View name resolution is highlyconfigurable, either via bean names, via a properties file, or viayour own ViewResolver implementation. The abstract model Map allowsfor complete abstraction of the view technology, without anyhassle: Be it JSP, Velocity, or anything else - every renderer canbe integrated directly. The model Map simply gets transformed intoan appropriate format, like JSP request attributes or a Velocitytemplate model.

关于视图:Spring的视图方案非常的灵活. 一个控制器执行可以通过response返回ModelAndView对象null,就可以直接写到一个视图.在通常的状况下,一个ModelAndView实例结合了
一个view 和一个 model Map,包含了bean name 和通讯对象(像命令或者form,参考数据等等).View名称是非常高端的的配置,不是通过bean name, 一个properties文件就是通过你自己的ViewResolver.这个抽象的model Map允许你在视图层面完成提取,没有任何的争辩:JSP,Velocity,或者其他,每一种都可以直接完整使用.这个modelMap 还可以简单得得到适当的格式化数据的转换,像JSP请求属性或者Velocity 模版模式.

3. Integration: Using a Different Web Framework withSpring
集成:用Spring一个不同web 框架

Many teams will try to leverage their investments in terms ofknow-how and tools, both for existing projects and for new ones.Concretely, there are not only a large number of books and toolsfor Struts but also a lot of developers that have experience withit. Thus, if you can live with Struts' architectural flaws, it canstill be a viable choice for the web layer. The same applies toWebWork and other web frameworks.


If you don't want to use Spring's web MVC but intend to leverageother solutions that Spring offers, you can integrate the webframework of your choice with Spring easily. Simply start up aSpring root application context via its ContextLoaderListener, andaccess it via its ServletContext attribute (or Spring's respectivehelper method) from within a Struts or WebWork action. Note thatthere aren't any "plugins" involved, therefore no dedicatedintegration: From the view of the web layer, you'll simply useSpring as a library, with the root application context instance asentry point.

如果你不想用Spring的 web MVC,但是想借用Spring嫁接其他的解决方案,你可以非常简单地通过Spring继承你自己的web框架.你可以非常简单地通过ContextLoaderListener
启动一个Spring root application context, 并且,通过Struts或者WebWork 的action 利用ServletContext 属性(或者Spring的helper方法)存取它. 值得注意的是, 这里没有任何的
"plugins"被调用,因此没有专门的集成:来自web层的视图,你可以简单的将Spring作为一个管理application context实例入口点的类库.

All your registered beans and all of Spring's services can be atyour fingertips even without Spring's web MVC. Spring doesn'tcompete with Struts or WebWork in this usage, it just addresses themany areas that the pure web frameworks don't, from beanconfiguration to data access and transaction handling. So you areable to enrich your application with a Spring middle tier and/ordata access tier, even if you just want to use e.g. the transactionabstraction with JDBC or Hibernate.

所有你注册bean和Spring服务都可以在不需要Spring的webMVC的情况下都可以被你轻松掌握.Spring 不是和Struts,WebWork这些应用进行竞争,它将作为一个纯web框架应用于

4. Feature Check List

If just focussing on the web support, some of Spring's uniquefeatures are:

如果聚焦于web 支持,Spring的一些显著特点是:

.Clear separation of roles: controller vs validator vs commandobject vs form object vs model object, DispatcherServlet vs handlermapping vs view resolver, etc.

清楚地角色分离:controller , validator , command object , formobject , model object,和 DispatcherServlet , handler mapping vsview resolver, 等等

.Powerful and straightforward configuration of both framework andapplication classes as JavaBeans, including easy in-betweenreferencing via an application context, e.g. from web controllersto business objects and validators.


.Adaptability, non-intrusiveness: Use whatever Controller subclassyou need (plain, command, form, wizard, multi action, or a customone) for a given scenario instead of deriving fromAction/ActionForm for everything.


.Reusable business code, no need for duplication: You can useexisting business objects as command or form objects instead ofmirroring them in special ActionForm subclasses.


.Customizable binding and validation: type mismatches asapplication-level validation errors that keep the offending value,localized date and number binding, etc instead of String-only formobjects with manual parsing and conversion to businessobjects.


.Customizable handler mapping, customizable view resolution:flexible model transfer via name/value Map, handler mapping andview resolution strategies from simple to sophisticated instead ofone single way.


.Customizable locale and theme resolution, support for JSPs withand without Spring tag library, support for JSTL, support forVelocity without the need for extra bridges, etc.


.Simple but powerful tag library that avoids HTML generation at anycost, allowing for maximum flexibility in terms of markupcode.


阅读(426)| 评论(0)
推荐 转载



<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->


网易公司版权所有 ©1997-2018