java MVC 中的推与拉模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17441926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:00:16  来源:igfitidea点击:

Push vs Pull Model in MVC

javaspring-mvcstruts2

提问by Punit Patel

What is the difference between the push & pull models of MVC?

MVC 的推拉模型有什么区别?

Is Struts2, Spring MVC Pull based?

是否基于 Struts2、Spring MVC Pull?

回答by Ruchira Gayan Ranaweera

According to Struts2 Interview Questions and Answers

根据Struts2面试问答

Struts2 is a Pull-MVC based architecture, in which all data is stored in Value Stack and retrieved by view layer for rendering.

Struts2 是一个基于 Pull-MVC 的架构,其中所有数据都存储在 Value Stack 中,并由视图层检索以进行渲染。

Specifically:

具体来说:

In case of Push-MVC the data (Model) is constructed and given to the view layer by the Controllers by putting it in the scoped variables like request or session. Typical example is Spring MVC and Struts1. Pull-MVC on the other hand puts the model data typically constructed in Controllers are kept in a common place i.e. in actions, which then gets rendered by view layer.

在 Push-MVC 的情况下,数据(模型)由控制器构建并提供给视图层,方法是将其放入请求或会话等作用域变量中。典型的例子是 Spring MVC 和 Struts1。另一方面,Pull-MVC 将通常在控制器中构建的模型数据保存在一个公共位置,即操作中,然后由视图层呈现。

回答by Tala

The concept of push and pull refers to what the server does in relation to the client. In a "push" application, a server is pushing or sending data to clients at it's own initiation. In a "pull" application, the server is pulling or waiting for and receiving messages initiated by clients.

推和拉的概念是指服务器相对于客户端所做的事情。在“推送”应用程序中,服务器在自己启动时向客户端推送或发送数据。在“拉”应用程序中,服务器正在拉或等待和接收由客户端发起的消息。

A good explanation is given here mvc-pattern-its-importance-push-pulland here pull-vs-push-mvc-architecture

这里给出了一个很好的解释mvc-pattern-its-importance-push-pull和这里pull-vs-push-mvc-architecture

Struts1 and Spring both use Push MVC. This question might be helpful spring-mvc-complex-model-population-from-multiple-sourcesStruts2 uses Pull

Struts1 和 Spring 都使用 Push MVC。这个问题可能有帮助spring-mvc-complex-model-population-from-multiple-sourcesStruts2 uses Pull

回答by riccardo.cardin

Struts2, Play! and so on are all kinds of pull modelimplementations of the MVC pattern.

Struts2,玩!等等都是MVC模式的各种pull模型实现。

Terms "push" and "pull" refer directly to the type of implementation of the observer pattern used between View and Model. As stated in GoF Observer pattern explaination, we have:

术语“”和“”直接指的是视图和模型之间使用的观察者模式的实现类型。正如 GoF 观察者模式解释中所述,我们有:

At one extreme, which we call the push model, the subject sends observers detailed information about the change, whether they want it or not. At the other extreme is the pull model; the subject sends nothing but the most minimal notification, and observers ask for details explicitly thereafter.

在一种极端情况下,我们称之为推送模型,主体向观察者发送有关更改的详细信息,无论他们是否愿意。另一个极端是拉模型;主体只发送最少的通知,然后观察者明确询问细节。

This means that the implementation of push modelrequires that View and Model are implemented using the same language and they are executed in the the same environment. Good examples of this kind of implementation are Javascript single page applications, in which View and Model components execute inside the browser and a framework, i.e. Backbone, provides MVC (a.k.a. Observer) mechanism. Often, Model component interacts with some kind of server API, to persist and get persisted datas.

这意味着推送模型的实现要求 View 和 Model 使用相同的语言实现,并且它们在相同的环境中执行。这种实现的好例子是Javascript 单页应用程序,其中 View 和 Model 组件在浏览器内执行,一个框架,即Backbone,提供 MVC(又名观察者)机制。通常,模型组件与某种服务器 API 交互,以持久化和获取持久化数据。

On the other hand, pull modellets you implement MVC using different technologies for View component, and Controller / Model components. In this kind of MVC, there is not an explicit use of the Observer pattern and View interacts exclusively with Controller. View component, which usually executes into the browser, sends to Controller component request of model's updates or model's state. Usually requestes are implemented using HTTP protocol. This kind of implementation requires the use of some type of "augmented HTML scripting language", such as JSP, which allows to create automatically the link between View and Controller.

另一方面,拉模型允许您使用视图组件和控制器/模型组件的不同技术来实现 MVC。在这种 MVC 中,没有明确使用观察者模式,视图只与控制器交互。通常在浏览器中执行的视图组件向控制器组件发送模型更新或模型状态的请求。通常请求是使用 HTTP 协议实现的。这种实现需要使用某种类型的“增强的 HTML 脚本语言”,例如 JSP,它允许自动创建 View 和 Controller 之间的链接。

回答by Subhajit

Sorry .. I dont think that struts 1, struts 2 and spring MVC can be taken as PUSH MVC ..

对不起..我不认为struts 1,struts 2和spring MVC可以被视为PUSH MVC ..

as both of all frameworks use a front controller[Filer class for Struts , And Controller Listener for Spring ] defined in their respective deployment descriptor. Now both of all these frame work save the form data in their respective bean[Or model] object using that controller by reflection.

因为所有框架都使用在各自的部署描述符中定义的前端控制器[用于 Struts 的 Filer 类,用于 Spring 的控制器监听器]。现在,所有这些框架都通过反射使用该控制器将表单数据保存在各自的 bean[Or model] 对象中。

Now from our Action Controller we can receive the bean object and will get the values but in the front controller where the bean object[or model] is actually generated set the value in their resperctive field by using request.getParameter(param) or request.getParameterValues(param) methods internally.. So this can be considered as PULL.

现在,我们可以从 Action Controller 接收 bean 对象并获取值,但在实际生成 bean 对象 [或模型] 的前端控制器中,通过使用 request.getParameter(param) 或 request.getParameter(param) 设置其对应字段中的值。内部的 getParameterValues(param) 方法.. 所以这可以被认为是 PULL。

So as per my thinking we can say that These framework can use two layers namely PULL layers used by end programmer and PUSH layers used by framework base classes.

因此,根据我的想法,我们可以说这些框架可以使用两层,即最终程序员使用的 PULL 层和框架基类使用的 PUSH 层。