java Controller属于Presentation层?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12429729/
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
Controller belongs to the Presentation layer?
提问by Ramesh Kotha
I heard that controller belongs to the presentation layer. How is it possible?
听说控制器属于表现层。这怎么可能?
I thought that :
我以为 :
- view is for presentation
- model is for business logic
- controller is for controlling logic
- 视图用于演示
- 模型用于业务逻辑
- 控制器用于控制逻辑
Is there good link to prove that controller belongs to the presentation layer?
有没有好的链接证明控制器属于表现层?
"Spring MVC is used for presentation layer": how can we use MVC only in the presentation layer?
《Spring MVC用于表现层》:如何只在表现层使用MVC?
回答by Jerome Dalbert
The presentation layer contains views andcontrollers.
You must not mistake an MVC architecture for a multitier/layer architecture (especially a 3-tier architecture). Most of the time Model/View/Controller is not the primary design of a web application, it is just a subset of a multitier/layer architecture.
表示层包含视图和控制器。
您不能将 MVC 架构误认为多层/层架构(尤其是 3 层架构)。大多数时候模型/视图/控制器不是 Web 应用程序的主要设计,它只是多层/层架构的一个子集。
Take a look at this oversimplified scheme (you could have the DAOs in a dedicated Data Access Layer, but this is not important in this post) :
看看这个过于简化的方案(您可以在专用的数据访问层中使用 DAO,但这在本文中并不重要):
Spring MVC is a presentation framework: it deals with controllers and views. But why the "M" in Spring MVC ? Just because, as many other presentation framework, it naturally deals with a representation of a model/entity ("M"). This representation is the one used in your controllers, displayed in your views, submitted in your forms, etc. That's why the framework is called Spring MVC, even if the model/entity is not part of the prensentation layer.
Spring MVC 是一个表示框架:它处理控制器和视图。但是为什么 Spring MVC 中的“M”呢?正因为与许多其他表示框架一样,它自然地处理模型/实体(“M”)的表示。这种表示在您的控制器中使用、在您的视图中显示、在您的表单中提交等。这就是该框架被称为 Spring MVC 的原因,即使模型/实体不是表示层的一部分。
I think it is a good name for this Framework, because it is really "MVC" oriented. Indeed the representation of a model/entity can be :
我认为这是这个框架的一个好名字,因为它确实是面向“MVC”的。实际上,模型/实体的表示可以是:
- direct : the framework directly handles the model/entity object
- indirect : the framework handles a form object or DTO, that contains information related to one or multiple entities
- direct :框架直接处理模型/实体对象
- 间接:框架处理表单对象或 DTO,其中包含与一个或多个实体相关的信息
Spring's recommendationis to directly use the model/entity ("M") object :
Spring 的建议是直接使用模型/实体(“M”)对象:
Reusable business code, no need for duplication. Use existing business objects as command or form objects instead of mirroring them to extend a particular framework base class.
业务代码可复用,无需重复。将现有业务对象用作命令或表单对象,而不是将它们镜像以扩展特定框架基类。
That's why I say the framework is very "MVC" oriented, compared to others, like Struts, where you have to use different form objects.
这就是为什么我说该框架非常面向“MVC”,与其他框架相比,例如 Struts,您必须使用不同的表单对象。
Some interesting links :
一些有趣的链接:
- Comparison between Multitier and MVC architecture, from Wikipedia
- This blog post about 3-tier architecturein ASP.NET
- This blog image of a 3-tier architecture
- DispatcherServlet chapterfrom Spring's documentation
- Multitier 和 MVC 架构之间的比较,来自维基百科
- 这篇关于ASP.NET 中的3 层架构的博客文章
- 这个三层架构的博客图片
- Spring 文档中的DispatcherServlet 章节
回答by JB Nizet
The controller controls the presentation layer logic. For all the business code, transactional use cases, persistence, etc., it typically delegates to a service layer.
控制器控制表示层逻辑。对于所有业务代码、事务用例、持久性等,它通常委托给一个服务层。
A typical way of doing that is to implement transactional services as spring beans and inject those spring beans in controllers. Typical use case: create a new product:
这样做的典型方法是将事务服务实现为 spring bean 并将这些 spring bean 注入控制器。典型用例:创建新产品:
- The controller receives a command bean from the browser
- It validates that all the required data is present, and if not, redisplays the product creation page with error messages
- It calls a service bean to create the product
- The service bean runs in a transaction. It gets the product category from the database, attaches the product to its category, computes the price for the product based on current pricing strategies, sends a JMS message to an external application, and returns the ID of the created product
- The controller redirects to the product detail page, using the ID of the created product as a URL parameter.
- 控制器从浏览器接收命令 bean
- 它验证所有必需的数据是否存在,如果不存在,则重新显示带有错误消息的产品创建页面
- 它调用服务 bean 来创建产品
- 服务 bean 在事务中运行。它从数据库中获取产品类别,将产品附加到其类别中,根据当前的定价策略计算产品的价格,向外部应用程序发送 JMS 消息,并返回创建的产品的 ID
- 控制器重定向到产品详细信息页面,使用创建的产品的 ID 作为 URL 参数。
回答by Erik Funkenbusch
It largely depends on what flavor of MVC you're using, and what environment you're using it in.
这在很大程度上取决于您使用的 MVC 风格,以及您在什么环境中使用它。
For example, ASP.NET MVC is entirely a UI pattern, so all three parts are part of presentation.
例如,ASP.NET MVC 完全是一种 UI 模式,因此所有三个部分都是呈现的一部分。
However, in most implementations of MVC, the Controller interacts with the user, and thus is part of the UI layer. It may handle button presses, and keyboard input... but in many cases, the controller is also responsible for connecting the Model and the View together.
然而,在 MVC 的大多数实现中,控制器与用户交互,因此是 UI 层的一部分。它可以处理按钮按下和键盘输入……但在许多情况下,控制器还负责将模型和视图连接在一起。
The one universal truth is that you should NOT be doing business logic in the controller if you cannot help it. Where the business logic exists depends on many factors. It might be part of the model in some implementations, or it may be it's own separate layer outside of MVC
一个普遍的事实是,如果你不能帮助它,你不应该在控制器中做业务逻辑。业务逻辑存在于何处取决于许多因素。在某些实现中它可能是模型的一部分,或者它可能是 MVC 之外的独立层