Spring的web MVC 構架模式 (轉)
Mwith the
Spring的web MVC 構架
Juergen Hoeller
1. Introduction: Spring the Application Framework
1.介紹:Spring 應用構架
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 isn't particularly a web framework but a generic lightweight application framework with dedicated web support, and show the architectural differences to and WebWork
當你第一次看到Spring的時候,你一定會說:"哦 不,又一種web 構架".這篇文章將告訴你Spring明顯區別於其他輕量級application framework,
它將專注於web的支援,與struts 和 webwork有著明顯的區別。
In contrast to Struts or WebWork, Spring is an application framework for all layers: It offers a bean configuration foundation, AOP support, a JC 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 consred avoidable evils.
在和struts 和 webwork的對比上,Spring是一個服務於所有層面的application framework:提供了bean的基礎,AOP的支援,的提取,
抽象事務支援,等等。它有一個非常顯著的特點:在某個層面上如果你不需要Spring的支援,你就可以不使用String的class,只使用它的某一部分的功能。
從它的設計理念,你可以看到String 幫助你實現了真正的邏輯層和web層的分離:例如。一個校驗應用將不用依靠controllers,就可以實現。這樣的目標是
更好的重用和易測:過分依靠不必要的容器和框架將不能實現這一點。
Of course, Spring's own web support is nicely integrated with the framework's general patterns. Nevertheless, replacing the web solution with Struts, WebWork, or the like is easy. Both with Spring's 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 , your business logic will not unnecessarily depend on container services like JTA or - allowing complex, well-architected web applications to run in a "simple" container like 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 existing solutions. It rather fosters seamless integration with standards like , , JTA, JNDI, JDBC, and JDO, and well-suited tools like , Velocity, Log4J, and Caucho's 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 JTA via Spring's JtaTransactionManager if you need distributed transactions - but only then, as there are perfect replacements for single databases, like DataTransactionManager or HibernateTransactionManager.
值得注意的是,Spring 不是和已經存在的解決方案進行競爭. 我們鼓勵結合標準的技術,如, Servlet, JSP, JTA, JNDI, JDBC, and JDO, 和非常匹配的工具,如,Hibernate, Velocity, Log4J, and Caucho's Hessian/Burlap.這個框架的的設計思想是在你的應用需要改良的時候,你將會做一些技術的選擇:例如,如果你需要分散式事務處理,你可能需要用Spring的Jta TransactionManager 來實現JTA服務.或者,用DataSourceTransactionManager or HibernateTransactionManager 來實現美妙完美的單個。
2. Web MVC: The Design of Spring's Web Framework
Web MVC:Spring web 框架的設計思想
Spring's web framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler maps, 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 don't have a form, you don't need a FormController. This is a major difference to Struts.
Spring 框架透過配置操作mappings,展示resolution,本地化和模版整合圍繞著分派請求操作的servlet - DispatcherServlet設計的.預設的操作是一個非常簡單的控制介面,
他只提供了ModelAndView handleRequest(request,response)方法.這將用於一個應用的控制,但是,如果你想包含多個層次的控制,AbstractController, AbstractCommandController, MultiActionController, SimpleFormController, AbstractWizardFormController 將幫助你完成.應用控制將代表那些子.注意,你可以選擇一個適當的基類:如果你沒有 web form,你就不必用FormController.這就是和Struts最大的不同.
You can take any as command or foobject: There's no need to implement an interface or derive from a base class. Spring's 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 don't 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 proy. Instead, it's 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.
你可以透過命令或者form來操作任何物件:這不需要介面工具或者一個基礎類的.Spring的資料邦定是非常的靈活的.舉例來說,它描述了具有在應用範圍內的校驗錯誤的輸入機制,但不是系統錯誤.所以在你的form物件裡面你不必複製你的業務物件的string屬性,只操作出錯的子任務,或者適當地轉換string.換句話說,它通常可以很好的直接邦定你的業務物件.
這也是和struts圍繞請求基礎類Action 和 ActionForm (每個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.
對比WebWork,Sping更多的區別在於物件角色:Sping支援控制器的感念,一個操作命令或者form物件,和得到資料傳遞給檢視的模式.這個模式通常包含命令和form物件,但有時也包含任意的參考資料.換句話說,一個WebWork Action聯合所有這些角色到一個單獨的物件.WebWork 允許你用已經存在的業務物件作為你 form 的一部分,但是隻生成各自Action 的 bean 屬性. 最後,操作請求的Action 例項在一個檢視裡面獲得付值和form population. 然而,參考資料也需要作為Action 的屬性被模擬. 一個類裡面有太多的角色是值得討論的.
Regarding views: Spring's 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 hae: Be it JSP, Velocity, or anything else - every renderer can be integrated directly. The model Map simply gets tranormed into an appropriate format, like JSP request attributes or a Velocity template model.
關於檢視: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. Integration: Using a Different Web Framework with Spring
整合:用Spring一個不同web 框架
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.
許多開發團隊將為他們已有的專案或者新的專案已經獲得的期限和工具進行投資.這裡沒有像Struts那樣大量的圖書和工具,但是同樣我們有大量的擁有Spring開發技巧的開發人員.然而,如果你願意生活在Struts的構架瑕疵中的話,他將是你在web層開發不錯的選擇.當然,其他應用也是一樣.
If you don't want to use Spring's web MVC but intend to leverage other solutions that Spring offers, you can integrate the web framework of your choice with Spring easily. Simply start up a Spring application context via its ContextLoaderListener, and access it via its ServletContext attribute (or Spring's respective helper method) from within a Struts or WebWork action. Note that there aren't any "plugins" involved, therefore no dedicated integration: From the view of the web layer, you'll simply use Spring as a library, with the root application context instance as entry 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 at your finger even without Spring's web MVC. Spring doesn't compete with Struts or WebWork in this usage, it just addresses the many areas that the pure web frameworks don't, from bean configuration to data access and transaction handling. So you are able to enrich your application with a Spring middle tier and/or data access tier, even if you just want to use e.g. the transaction abstraction with JDBC or Hibernate.
所有你註冊bean和Spring服務都可以在不需要Spring的web MVC的情況下都可以被你輕鬆掌握.Spring 不是和Struts ,WebWork這些應用進行競爭,它將作為一個純web框架應用於
很多領域,因為它不需要配置資料和事務操作.所以,你可以利用Spring提供的中間層和資料儲存層來豐富你的應用,甚至,你想用JDBC或者Hibernate進行事務抽象.
4. Feature Check List
特點核對列表
If just focussing on the web support, some of Spring's unique features are:
如果聚焦於web 支援,Spring的一些顯著特點是:
.Clear separation of roles: controller vs validator vs command object vs form object vs model object, DispatcherServlet vs handler mapping vs view resolver, etc.
清楚地角色分離:controller , validator , command object , form object , model object,和 DispatcherServlet , handler mapping vs view resolver, 等等
.Powerful and straightforward configuration of both framework and application classes as Beans, including easy in-between referencing via an application context, e.g. from web controllers to business objects and validators.
強大而且直接的框架和作為JavaBeans的應用配置,包括簡單的參照和應用內容,例如,從web控制器到業務物件和資料校驗.
.Adaptability, non-intrusiveness: Use whatever Controller subclass you need (plain, command, form, wizard, multi action, or a custom one) for a given scenario instead of deriving from Action/ActionForm for everything.
適應性,外掛:無論什麼樣的控制器你都需要得到代替Action/ActionForm所做的每件事情的方案(簡單,命令,form,範例,多重action,或者定製一個)
.Reusable business code, no need for duplication: You can use existing business objects as command or form objects instead of mirroring them in special ActionForm subclasses.
重用業務邏輯程式碼,不需要複製:你可以用已經有的業務邏輯物件作為命令或則form物件代替反射特定的ActionForm子類.
.Customizable binding and validation: type mismatches as application-level validation errors that keep the offending value, localized date and number binding, etc instead of String-only form objects with manual parsing and conversion to business objects.
可訂製的邦定和資料校驗:作為應用級的輸入機制的錯誤校驗,固定日期和數字邦定,例如,透過手動檢查和轉換業務物件來替換單個的string。
.Customizable handler mapping, customizable view resolution: flexible model transfer via name/value Map, handler mapping and view resolution strategies from simple to sophisticated instead of one single way.
可訂製的操作,可訂製的檢視方案:透過name/value Map靈活的模型傳送,用一個簡單的方法從簡單到複雜操作對映和檢視方案策略.
.Customizable locale and theme resolution, support for JSPs with and without Spring tag library, support for , support for Velocity without the need for extra bridges, etc.
本地定製和主題方案:Spring沒有,完全支援於JSPs,JSTL,不需要額外的連線就可以支援Velocity,等等.
.Simple but powerful tag library that avoids HTML generation at any cost, allowing for maximum flexibility in terms of markup code.
簡單但是強大的tag library避免了HTML產生的混亂,最大限度的靈活擴充套件了標記程式碼.
Links
連線
原文:
Spring Framework website:
Web MVC support () :
譯者主頁:
Yanger
to:y-ge@263.net">y-ge@263.net
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-962868/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 極簡架構模式-MVC模式架構模式MVC
- 探索從 MVC 到 MVVM + Flux 架構模式的轉變MVCMVVMUX架構模式
- iOS 架構模式–解密 MVC,MVP,MVVM以及VIPER架構iOS架構模式解密MVCMVPMVVM
- 選用Vue做MVC架構模式VueMVC架構模式
- 架構師對MVC設計模式的理解架構MVC設計模式
- Java Web(八) MVC和三層架構JavaWebMVC架構
- Web 框架的架構模式探討Web框架架構模式
- Web開發框架中的架構模式比較(一) (轉)Web框架架構模式
- Web開發框架中的架構模式比較(三) (轉)Web框架架構模式
- Web開發框架中的架構模式比較(五) (轉)Web框架架構模式
- Web開發框架中的架構模式比較(四) (轉)Web框架架構模式
- mvc架構MVC架構
- Spring 框架基礎(06):Mvc架構模式簡介,執行流程詳解Spring框架MVC架構模式
- Spring系列(六) Spring Web MVC 應用構建分析SpringWebMVC
- iOS MVC、MVVM、MVP架構模式淺淺析iOSMVCMVVMMVP架構模式
- iOS架構入門 - MVC模式例項演示iOS架構MVC模式
- 膜拜阿里架構師全程手寫Spring MVC阿里架構SpringMVC
- Spring MVC 構建入門級 Web 應用程式SpringMVCWeb
- 使用Java和Spring MVC構建Web應用JavaSpringMVCWeb
- [譯]iOS架構模式——解密MVC、MVP、MVVM和VIPERiOS架構模式解密MVCMVPMVVM
- 從MVC框架看MVC架構的設計MVC框架架構
- 後MVC時代的前端架構MVC前端架構
- 面向模式構建系統架構 (轉)模式架構
- 轉享:表現層架構模式比較:MVP(SC),MVP(PV),PM,MVVM 和 MVC架構模式MVPMVVMMVC
- MVC 與三層架構MVC架構
- iOS MVC架構介紹iOSMVC架構
- 極簡架構模式-控制反轉架構模式
- Maven+Spring MVC+MyBatis+dbpc+Mysql Web專案構建MavenSpringMVCMyBatisMySqlWeb
- web.xml+spring mvc基本配置WebXMLSpringMVC
- Java Web系列:Spring MVC基礎JavaWebSpringMVC
- 架構模式的聖經架構模式
- 從MVC到DDD的架構演進MVC架構
- 記錄一次阿里架構師全程手寫Spring MVC阿里架構SpringMVC
- 使用 Spring 3 MVC HttpMessageConverter 功能構建 RESTful web 服務SpringMVCHTTPRESTWeb
- 深入淺出Spring Web MVC:從零開始構建你的第一個Web應用SpringWebMVC
- 快速瞭解Web MVC設計模式WebMVC設計模式
- 架構之路(七)MVC點滴架構MVC
- MVC模式的PHP實現(3) (轉)MVC模式PHP