java - 需要 MVC 模式的帮助

标签 java ruby-on-rails model-view-controller

我刚刚开始阅读有关 MVC 模式的内容,但我并不完全理解它。

The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller). In event-driven systems, the model notifies observers (usually views) when the information changes so that they can react.

The view renders the model into a form suitable for interaction, typically a user interface element. Multiple views can exist for a single model for different purposes. A viewport typically has a one to one correspondence with a display surface and knows how to render to it.

The controller receives user input and initiates a response by making calls on model objects. A controller accepts input from the user and instructs the model and viewport to perform actions based on that input.

(来自 http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller )

还有

Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).

View. The view manages the display of information.

Controller. The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.

(来自 http://msdn.microsoft.com/en-us/library/ff649643.aspx )

我想我理解模型和 View 是什么,但我不理解 Controller 。从我读到的文本和一些示例中我看到 Controller 接受用户的输入。我见过这样的例子(在Java中): Controller 是一个JOptionPane,它从用户那里接收一个华氏度的数字。然后在模型中对其进行分配和转换,并在命令行中显示结果。我不明白为什么 Controller 会接受这样的输入?

在 Ruby on Rails 中,我有存储数据的模型、显示不同 View 并对 View 中接收到的输入执行“操作”的 Controller 。

最佳答案

简单来说,

View - 负责绘制或渲染模型中的信息

模型 - 负责您的业务逻辑并存储应用程序的状态

Controller - 负责解释用户的操作并调用模型上的必要事件并通知 View 自行更新(如果需要)。

View 应该只负责绘制/渲染,因此它不应该知道用户交互时应该发生什么事件。模型应该只对业务逻辑和数据感兴趣。它不应该知道有关 View 的任何信息。因此, Controller 将这两部分联系在一起,允许模型可重用,并在单个模型上启用多个 View (想想同一数据模型的移动/电视/Web View )。

在 Web 世界中, Controller 是接收 HTTP 输入的部分, View 是 HTML/JSP 代码, Controller 是 Servlet(或等效框架),它作用于 HTTP 请求以执行必要的业务该模型。

关于java - 需要 MVC 模式的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5787638/

相关文章:

php - 我不明白为什么DeleteTask.php不起作用,但RetrieveTask.php却起作用

java - 如何获取 XmlType 的字符串表示形式?

ruby-on-rails - 在表单提交的底部显示错误信息

java - Android:在 View 中使用观察者模式(可能与 MVC、MVVM 相关)

android - MVC : who should fetch data, 适配器或 Activity

ruby-on-rails - 你怎么能强制验证嵌套模型的所有字段,即使它们没有改变? ( rails 3.1)

java - 查找 Map 中的最高 n 个值

java - 使用 maven-bundle-plugin 为不包含所有依赖项的 jar 的 OSGI 容器创建 Web 应用程序包 (WAB)

java - 如何在我的 java 代码中获取 JanusGraph 实例?

html - Ruby on Rails Helper 方法 - HTML 显示为纯文本