(Spring文档翻译)Part V, the Web 17.1 Spring Web MVC framework介绍

指南文档的这个部分涵盖了Spring框架对表现层(特别是基于Web的表现层)以及WebSocket消息风格的web应用的支持。

Spring框架拥有自己的web框架,Spring Web MVC,包含在前面几个章节。之后的几章是关于Spring框架对其他web技术的集成支持,像JSF等。

再之后是Spring框架的MVC porlet 框架。

Spring 的MVC框架围绕着DispatcherServlet设计,DispatcherServlet将请求转发给handler,用可配置的handler 映射、视图解析、本地化和主题方案

并且支持文件上传。

默认的handler基于@Controller和@RequestMapping注解,提供了一个非常具有弹性的handling方法,在Spring3.0的介绍中,@Controller机制允许你通过@PathValiable注解和其他的一些特性来创建RESTful的Web网站和应用。

“对扩展开放...”是SpringWebMVC的一个关键设计原则,在Spring中,总的来说是"对扩展开放,对修改关闭的"原则。

SpringWebMVC中的一些核心类的方法被标记为final。作为一个开发者你不能覆盖这些方法来提供你自己的行为。这个并没有被绝对的要求,但要在头脑里要明确这个原则。

对于这个原则的一个解释,可以参考Seth Ladd等人的 Expert Spring Web MVC and Web Flow这本书,特别是"A Loot At Desing"这部分,在第一版的117页。

当你使用Spring MVC的时候,你不能对final方法提供增强。例如,你不能增强AbstractController.setSynchronizeOnSession()这个方法。参考Section9.6.1来获取更多的AOP代理的信息和为什么你不能对final方法增强。

在Spring Web MVC中,你可以使任何对象来当做命令或返回表单的对象,你不需要实现特定框架的接口或基类。Spring的data binding非常具有弹性。例如,它将类型匹配错误当做验证错误来对待,这样就可以被应用发现而不是系统错误。这样你就不必复制你的业务对象的属性,像从表单对象里的简单的没有类型的字符串来处理不合法的提交或这是将它合适的转化为String。Spring经常可以非常好的直接绑定到你的业务对象上。

Spring的视图解决方案极其具有弹性。Controller通常负责准备一个带有数据的Map模型并且选择一个view名字,但是它也可以直接写入到response流来结束这次请求。视图的名称也高度可配置,可以通过文件的扩展名或者Accept header,content type negotiation,通过bean名称,一个properties文件,甚至一个传统的ViewResolver实现。model(MVC中的M)是一个Map借口,允许对视图技术的完全的抽象。你可以直接用基于模板的渲染技术,像JSP,Velocity,Freemaker集成,或直接生产XML,JSON,Atom,以及其他的很火内容形式。

Map模型可以简单地转化到合适的格式,像JSP的request属性,Velocity模板的model.


英文原文:

Introduction to Spring Web MVC framework

The Spring Web model-view-controller (MVC) framework is designed around aDispatcherServlet that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, time zone and theme resolution as well as support for uploading files. The default handler is based on the @Controller and@RequestMapping annotations, offering a wide range of flexible handling methods. With the introduction of Spring 3.0, the @Controller mechanism also allows you to create RESTful Web sites and applications, through the @PathVariable annotation and other features.

In Spring Web MVC you can use any object as a command or form-backing object; you do not need to implement a framework-specific interface or base class. Spring’s data binding is highly flexible: for example, it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. Thus you need not duplicate your business objects' properties as simple, untyped strings in your form objects simply to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects.

Spring’s view resolution is extremely flexible. A Controller is typically responsible for preparing a model Map with data and selecting a view name but it can also write directly to the response stream and complete the request. View name resolution is highly configurable through file extension or Accept header content type negotiation, through bean names, a properties file, or even a custom ViewResolver implementation. The model (the M in MVC) is a Map interface, which allows for the complete abstraction of the view technology. You can integrate directly with template based rendering technologies such as JSP, Velocity and Freemarker, or directly generate XML, JSON, Atom, and many other types of content. The model Map is simply transformed into an appropriate format, such as JSP request attributes, a Velocity template model.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值