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


發佈了43 篇原創文章 · 獲贊 28 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章