【正文】
ozkanexKeywords: web, web framework, design patterns, model view controller patternAbstract: The ModelViewController design pattern is cited as the architectural basis for many web development frameworks. However, the version of MVC used for web development has changed as it has evolved from the original Smalltalk MVC. This paper presents an analysis of those changes, and proposes a separate WebMVC pattern that more accurately describes how MVC is implemented in web frameworks.1 INTRODUCTION現(xiàn)在已經(jīng)是多對(duì)多的關(guān)系了。MVCWeb控制器組件有三個(gè)主要職責(zé):l 前端控制器:接受傳入的請(qǐng)求并把它們路由到相應(yīng)的處理程序l 方法處理:接受請(qǐng)求參數(shù);驗(yàn)證請(qǐng)求參數(shù)的語(yǔ)法(這可能通過(guò)視圖元素進(jìn)行了重復(fù)驗(yàn)證);通過(guò)調(diào)用模型元素來(lái)編排請(qǐng)求的處理程序l 控制流:調(diào)用相應(yīng)的視圖元素作為一個(gè)對(duì)請(qǐng)求正在被處理的響應(yīng),根據(jù)方法的結(jié)果來(lái)調(diào)用。視圖平臺(tái)(客戶端)被物理地與其他組件分離開,因此視圖組件在結(jié)構(gòu)與操作方面與控制器和模型變得不那么緊密。這相當(dāng)于一個(gè)本質(zhì)上是視圖=控制器的依賴。這消除了許多涉及到處理HTTP請(qǐng)求參數(shù)和提供信息給JSP頁(yè)面來(lái)顯示的細(xì)節(jié)。模型也包括提供訪問訪問外部資源的ActiveResource元素。Rails是一個(gè)用于Ruby(Thomas和Hansson, 2007)編程語(yǔ)言開發(fā)的web應(yīng)用程序框架。 MVC2為HTTP請(qǐng)求使用一個(gè)單一的處理程序,為每一個(gè)請(qǐng)求確定并實(shí)例化一個(gè)合適的控制器。用戶菜單被認(rèn)為是一種虛擬類型的設(shè)備,它主要用于傳送輸入信號(hào)給控制器層,就跟鍵盤和鼠標(biāo)一樣。應(yīng)用程序中不同的操作會(huì)有多個(gè)視圖,不同的視圖呈現(xiàn)給多個(gè)用戶。基于MVC的web應(yīng)用框架的修訂版本已經(jīng)被提出了(Chun, Yanhua, 和Hanhong, 2003) (Barrett和Delaney, 2004)。然而,用于web開發(fā)的MVC版本隨著原來(lái)的Smalltalk的MVC的演變而發(fā)生了一些改變。本文的第一個(gè)目標(biāo)是介紹MVC設(shè)計(jì)模式,其中包括它的原始形態(tài)(第2節(jié))以及現(xiàn)代眾所周知的用于web應(yīng)用框架的變更后的形態(tài)(第3節(jié))。模型還保持著對(duì)視圖和控制器組件的依賴,當(dāng)應(yīng)用程序的狀態(tài)發(fā)生變化時(shí)它會(huì)有通知。當(dāng)控制器接受到輸入信號(hào)時(shí),它首先將其傳送到活動(dòng)的子控制器,因此輸入信號(hào)首先會(huì)被最低層級(jí)的控制器處理。它也可以通過(guò)為新的接口介質(zhì)添加一個(gè)視圖/控制器的組合,或者通過(guò)獨(dú)立于其他組件為模型添加新的功能而增加其可擴(kuò)展性。模型組件用于包含應(yīng)用程序的邏輯和數(shù)據(jù)庫(kù)訪問。用戶界面(視圖組件)用過(guò)動(dòng)態(tài)的模板文件呈現(xiàn)出來(lái),這些動(dòng)態(tài)模板就是嵌入了Ruby就腳本的標(biāo)準(zhǔn)HTML文檔,類似于ASP,PHP,JSP等。方法通過(guò)一個(gè)XML配置文件或者通過(guò)Java注解機(jī)制來(lái)定義和配置。l 沒有一一對(duì)應(yīng)的視圖控制器對(duì)應(yīng)關(guān)系:在原來(lái)的MVC中,每個(gè)視圖元素有獨(dú)立的控制器元素為了單獨(dú)的視圖元素而定義。l 模型類被實(shí)例化的需求:web框架實(shí)例化模型對(duì)象是由于處理事務(wù)的需要以及封裝域?qū)嶓w的需要,而不是出于堅(jiān)持一個(gè)設(shè)想的原始的MVC中的模型組件。MVCWeb模型組件同程負(fù)責(zé)保留應(yīng)用程序的狀態(tài)。控制器會(huì)通過(guò)視圖元素為用戶準(zhǔn)備信息。這個(gè)MVCWeb模式是為了反映當(dāng)前的MVC在web應(yīng)用程序中的實(shí)現(xiàn)。the evolution of the MVC pattern as it was adapted from Smalltalk to web frameworks.3 MVC IN WEB FRAMEWORKS Actions are defined and configured through an XML configuration file or through the Java annotation mechanism. This configuration information also controls the flow of a web application by determining what view follows each action, depending upon the oute of the action. A central part of Struts2 is the Value Stack, where information flowing between view and controller is stored and converted The Front Controller pattern: All three frameworks use this pattern, in which a single controller element is responsible for routing ining HTTP requests, based upon the requested URL and configuration data. l 167。 Query handling: provide information to view and controller elements in response to queries. The MVCWeb view ponent presents a user interface, including data presentation and input devices. Its responsibilities include: l 167。 validate request parameter syntax (this may repeat validation done by view elements)。 The controller must be able to route requests from multiple view elements (Front Controller pattern) and to manage the flow of the application in response. The controller may pass information from model to view as part of this function. l 167。 Control flow: invoke the appropriate view element as a response to the request being processed, depending upon the oute of the action invoked. Components of the MVCWeb pattern interact in the following ways. l 167。 query the model as necessary to obtain information to be displayed l 167。 Controller elements are responsible for data validation: Transaction parameter validation is essentially a model function. Validation logic can be pushed into the model ponent, but the responsibility for executing the validation function is still with the controller. l 167。are HTML templates with serverside scripts that can generate dynamic content. Each view receives information from the controller that activated it, either as native objects or as viewmodel objects, which are unique pilations of data from the model, each designed for a specific view. The model ponent is intended to contain application logic and database access. A differentiation is made between view models, which are data structures intended to convey information to a specific view element, and application models, which are domain entities and related transactions that operate on them. The model ponent also provides objectrelational mapping, which hides the details of how application domain objects are mapped to database tables.The MVC design pattern was introduced with the Smalltalk programming environment as a way to structure interactive applications in a modular fashion (Krasner and Pope, 1988). As the