Model-View-Controller,每个部分的实际作用是什么? [英] Model-View-Controller, what every part really does?

查看:159
本文介绍了Model-View-Controller,每个部分的实际作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我最近开始研究Spring MVC,因为目前我正在参与一个使用它的项目.在此之前,我检查了Struts 2框架.但是,我认为两个框架对MVC设计模式AngularJS的定义和实现都有不同,因为客户端MVC框架也有自己的框架. 我将解释Spring MVC和Struts 2之间的区别.

春季:(引用自春季在行动)

1-控制器: 在Spring中,控制器是处理请求,执行某些逻辑(理想情况下,应将此逻辑提取到某些服务或业务逻辑类)并返回数据的组件.

" DispatcherServlet的工作是将请求发送到Spring MVC控制器. 控制器是处理请求的Spring组件.但是典型的应用 可能有几个控制器,DispatcherServlet需要一些帮助来决定 将请求发送到哪个控制器.因此,DispatcherServlet会咨询一个或 更多处理程序映射,以确定请求的下一站将在哪里.汉 dler映射在以下情况下将特别注意请求所携带的URL 做出决定."p166

2-型号:是一些需要运回用户并在浏览器中显示的信息.

"由控制器执行的逻辑通常会导致一些需要的信息 被带回给用户并显示在浏览器中.该信息是 称为模型."

3- View :这是最明显的一种,其HTML + CSS与模型信息结合在一起.

现在让我们看看Struts 2是如何进行MVC的

Struts2:(引用《 Struts 2 in Action》)

1-控制器:控制器是一个将URL映射到适当动作的组件,动作在struts中实现模型? (在春季,这不是控制器的工作). Struts只有一个控制器,即FilterDispatcher.在春季,这是由DispatcherServlet所完成的,他们不称其为控制器!

控制器的工作是将请求映射到 动作"

控制器的角色由Struts 2 FilterDispatcher扮演" p13

2-模型:模型(以struts为单位)是将业务日志和某些数据模型结合在一起的组件,从而使应用程序处于状态.

模型是由Struts 2实现的 动作组件" p13

在更多技术术语中,模型是应用程序的内部状态.这是 状态由数据模型和业务逻辑组成" p14

因此,struts中的模型不仅是诸如用户,合同,订单等数据模型.它还包含应用程序的逻辑.

这是这两本书中表示的弦和支柱2的两个图: Spring MVC

Struts 2

现在,哪个MVC是正确的MVC?

谁与前控制器MVC相互关联?

感谢提前回答.

解决方案

我是这样想的.

Model:存储和检索数据;强制执行所有使用该应用程序的应用程序必须一致的策略,称为业务逻辑". (例如:年龄必须> 18.)

视图:向用户显示数据;为用户提供了与系统交互的机制.

控制器:在模型和视图之间进行中介;强制执行特定于此应用程序的策略(例如,清除输入),有时将其与业务逻辑"相混淆. (示例:age必须是仅包含数字的字符串,这是解析它的方法.)

我看到了MVC的两种常见变体,一种用于桌面UI,另一种用于Web UI.

Web MVC:控制器处理请求,更新和/或查询模型,选择视图模板,将模型与视图模板合并.

桌面MVC:控制器解释用户手势,更新和/或查询模型;模型触发我变了!"事件; View订阅我已更改!"活动以进行自我更新.

Spring对Controller的描述将其与Martin Fowler在其《企业应用程序体系结构模式》中的事务脚本"相混淆.我了解这一点,因为我们通常将事务脚本实现为控制器类型的操作.具有讽刺意味的是,Struts比Spring更鼓励(或至少鼓励)这一点.

动作在Struts中实现模型"听起来很疯狂.我只能认为这是一个错误.

最后,前端控制器是一个中央请求处理程序,可将请求分派到更具体的控制器.在桌面应用程序中,我们可以将"TransferFundsController"直接连接到"TransferFundsAction"按钮,因此我们不需要前端控制器.在Web应用程序中,我们为所有请求提供一个入口点,并且我们有很多常见行为(例如,解析请求参数),因此我们经常希望Front Controller能够完成所有这些工作.

我希望这会有所帮助.

进一步阅读:

Smalltalk-80(TM)中的应用程序编程:如何使用模型视图控制器(MVC) http://c2.com/cgi/wiki?ModelViewControllerHistory

Hi guys recently I've started exploring Spring MVC, as currently I am involved in a project using it. Before that I checked out Struts 2 framework. However, In my opinion the two frame work has a different definition and implementation of the MVC design pattern, AngularJS, as a client side MVC framework has its own too. I will explain what differences I've noticed between Spring MVC and Struts 2.

Spring : (quoting from Spring in Action )

1- Controller: In Spring, the controller is a component that process a request, does some logic (ideally this logic should be extracted to some service or business logic class) and returns data back.

"The DispatcherServlet’s job is to send the request on to a Spring MVC controller. A controller is a Spring component that processes the request. But a typical application may have several controllers and DispatcherServlet needs some help deciding which controller to send the request to. So the DispatcherServlet consults one or more handler mappings to figure out where the request’s next stop will be. The han- dler mapping will pay particular attention to the URL carried by the request when making its decision." p166

2- Model: is some information that needs to be carried back to the user and displayed in the browser.

"The logic performed by a controller often results in some information that needs to be carried back to the user and displayed in the browser. This information is referred to as the model."

3- View: This is the most obvious one its the HTML+CSS combined with model information.

Now lets us see how Struts 2 does the MVC

Struts2: (quoting from Struts 2 in Action )

1- Controller : The controller is a component that maps a url to an appropriate action,actions implement models in struts ?! ( in spring this is not what controller does ). Struts has only one controller which is FilterDispatcher. in Spring this is done by DispatcherServlet’s which they don't call controller !

"The controller’s job is to map requests to actions"

"The role of the controller is played by the Struts 2 FilterDispatcher" p13

2- Model:The model, in struts, is a component that hold business log and some data model together this make the application state.

"model is implemented by the Struts 2 action component" p13

"In more technical terms, the model is the internal state of the application. This state is composed of both the data model and the business logic" p14

so the model in struts is not only data model like User, Contract, Order .... it also contains the logic of the application.

Here are the two diagram of string and struts 2 as represented in these two books: Spring MVC

Struts 2

Now, Which MVC is the correct MVC ?

Who the front controller MVC is related to each other ?

Thanks for answers in advance.

解决方案

I think of it this way.

Model: stores and retrieve data; enforces policies that must be consistent for all applications that use it, known as "business logic". (Example: age must > 18.)

View: presents data to the user; provides a mechanism for the user to interact with the system.

Controller: mediates between the Model and View; enforces policies specific to this application (sanitising input, for example), sometimes confused with "business logic". (Example: age must be a string with only numbers, and here's how to parse it.)

I see two common varieties of MVC, one for desktop UIs and one for web UIs.

Web MVC: Controller handles requests, updates and/or queries Model, chooses View template, merges Model with View template.

Desktop MVC: Controller interprets user gesture, updates and/or queries Model; Model fires "I've changed!" events; View subscribes to "I've changed!" events to update themselves.

Spring's description of Controller confuses it with what Martin Fowler calls a "Transaction Script" in his book Patterns of Enterprise Application Architecture. I understand this, because we often implement Transaction Scripts as Controller-type actions. Ironically, Struts encourages (or at least encouraged) this more than Spring did.

"Actions implement Models in Struts" sounds like utter insanity. I can only assume that it's a mistake.

Finally, the Front Controller is a central request handler that dispatches requests to a more specific controller. In desktop apps, we can connect "TransferFundsController" directly to the "TransferFundsAction" button, so we don't need a Front Controller. In web apps, we have a single point of entry for all requests, and we have a lot of common behavior (parsing request parameters, for example), so we often want a Front Controller to do all that.

I hope this helps.

Further Reading:

Applications Programming in Smalltalk-80(TM): How to use Model-View-Controller (MVC) http://st-www.cs.illinois.edu/users/smarch/st-docs/mvc.html

http://c2.com/cgi/wiki?ModelViewControllerHistory

这篇关于Model-View-Controller,每个部分的实际作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆