简体   繁体   English

将JSF理解为MVC框架

[英]Understanding JSF as a MVC framework

I am reading on JSF and I feel rather confused why JSF is a MVC framework (or atleast which parts belongs to which "letter"). 我正在阅读JSF,我觉得为什么JSF是一个MVC框架(或者至少哪个部分属于哪个“字母”)我感到很困惑。

I looked at this question: What components are MVC in JSF MVC framework? 我看了一下这个问题: JSF MVC框架中哪些组件是MVC?

I read there if you don't look at it in an aggregated view the model is your entity, view is your XHTML code and controller is the managed bean. 我在那里阅读如果你不在聚合视图中查看模型是你的实体,视图是你的XHTML代码而控制器是托管bean。 Hmm...Ok, but doesn't the view very often depend on carrying out further business logic calls which returns a set of entities for example, does the description still fit? 嗯......好的,但视图通常不依赖于执行进一步的业务逻辑调用,这些调用返回一组实体,例如,描述是否仍然适合?

One book I read described it as managed beans is the some kind of "message" bringer that the Faces Servlet (Controller) use to invoke the business layer (Model) and then the XHTML code is the view. 我读过的一本书将其描述为托管bean,是Faces Servlet(Controller)用于调用业务层(Model)的某种“消息”bringer,然后XHTML代码就是视图。

There are so many explanations and differences so I don't know which or how to understand it. 有太多的解释和差异,所以我不知道哪个或如何理解它。

Part of the reason why it's often not entirely clear in JSF and many other web frameworks which parts of it correspond to which part of MVC, is that the MVC pattern was originally devised for desktop applications. 在JSF和许多其他Web框架中,它通常不完全清楚的部分原因是MVC模式最初是为桌面应用程序设计的。

In a desktop application, the nodes M, V and C are a maximum connected graph, meaning each part can communicate with every other part. 在桌面应用程序中,节点M,V和C是最大连接图,这意味着每个部分可以与每个其他部分通信。 Eg if the model changes, it can push this change to the view. 例如,如果模型更改,它可以将此更改推送到视图。 This is particularly visible in case there are multiple representations of the view in a desktop application. 如果桌面应用程序中存在多个视图表示,则尤其可见。 Change one, and see the other update in real-time. 更改一个,并实时查看其他更新。

Due to the client/server and request/response nature of web applications, classic MVC doesn't map 1:1 to most web frameworks. 由于Web应用程序的客户端/服务器和请求/响应特性,经典MVC不会将1:1映射到大多数Web框架。

Specifically, in JSF the mapping is as follows: 具体来说,在JSF中,映射如下:

  • Model - The Services/DAOs plus the entities they produce and consume. 模型 - 服务/ DAO加上他们生产和消费的实体。 The entry point to this is the managed bean, but in Java EE (of which JSF is a part) these artifacts are typically implemented by EJB and JPA respectively. 对此的入口点是托管bean,但在Java EE(JSF是其中的一部分)中,这些工件通常分别由EJB和JPA实现。
  • View - The UI components and their composition into a full page. 视图 - 将UI组件及其组成整页。 This is fully in the domain of JSF and implemented by JSF UIComponent s and Facelets respectively. 这完全属于JSF领域,分别由JSF UIComponent和Facelets实现。
  • Controller - The traffic cop that handles commands and incoming data from the user, routes this to the right parts and selects a view for display. 控制器 - 处理来自用户的命令和传入数据的交通警察,将其路由到正确的部分并选择要显示的视图。 In JSF one doesn't write this controller, but it's already provided by the framework (it's the FacesServlet ). 在JSF中,不会编写这个控制器,但它已经由框架提供(它是FacesServlet )。

Especially the last part is frequently not well understood: In JSF you don't implement a controller. 特别是最后一部分经常不太清楚:在JSF中你没有实现一个控制器。 Consequently, a backing bean or any other kind of managed bean is NOT the controller. 因此,支持bean或任何其他类型的托管bean 不是控制器。

The first part (the model) is also not always clearly understood. 第一部分(模型)也并不总是清楚地理解。 Business logic may be implemented by EJB and JPA, but from the point of view of JSF everything that is referenced by a value binding is the model. 业务逻辑可以由EJB和JPA实现,但是从JSF的角度来看,值绑定引用的所有内容都是模型。 This is also where the name of one of the JSF life-cycle phases comes from: Update Model . 这也是JSF生命周期阶段之一的名称来自: Update Model In this phase JSF pushes data from the UI components into the model. 在此阶段,JSF将UI组件中的数据推送到模型中。 In that sense, (JSF) managed beans are thus the model. 从这个意义上讲,(JSF)托管bean就是模型。

Although JSF itself doesn't explicitly define the concept, there is an often recurring and specific usage of managed beans called the backing bean . 虽然JSF本身没有明确定义这个概念,但是有一种经常重复使用的托管bean称为支持bean

For JSF a backing bean is still the model, but practically it's a plumbing element that sits in the middle of the Model, View and Controller. 对于JSF,支持bean仍然是模型,但实际上它是位于模型,视图和控制器中间的管道元素。 Because it performs some tasks that may be seen as some controller tasks, this is often mistaken to be the controller. 因为它执行一些可能被视为某些控制器任务的任务,所以这通常被误认为是控制器。 But, as explained before this is not correct. 但是,如前所述,这是不正确的。 It can also perform some model tasks and occasionally do some view logic as well. 它还可以执行一些模型任务,偶尔也可以执行一些视图逻辑。

See also: 也可以看看:

In a minimalist form, it's: 它以极简主义的形式出现:

  • Model: Anything you use for persistence (Hibernate, JPA, etc) and data modeling (Java Beans). 模型:用于持久性(Hibernate,JPA等)和数据建模(Java Bean)的任何东西。
  • View: xhtml, jsp, etc. 查看:xhtml,jsp等
  • Controller: Mananaged Beans. 控制器:Mananaged Beans。

JSF gives you the power to control your requests/responses. JSF使您能够控制您的请求/响应。 The way you create model/view it's not directly connected to the framework MVC concept. 您创建模型/视图的方式并不直接连接到框架MVC概念。 It's just a matter of choice. 这只是一个选择问题。 The MVC concept is related to code organization. MVC概念与代码组织有关。

Analogously Struts is a MVC framework, but it works mainly as a controller. 类似地,Struts是一个MVC框架,但它主要用作控制器。

I think I help you to better clarify your idea. 我想我会帮助你更好地澄清你的想法。

The interesting thing about the managed bean idea is that it can both be used as a model (MVC pattern) or as a controller ( mediating-controller MVC pattern , also called model-view-adapter), where the model and view do not interact directly. 关于托管bean思想的有趣之处在于它既可以用作模型(MVC模式),也可以用作控制器( 中介控制器MVC模式 ,也称为模型视图适配器),其中模型和视图不相互作用直。

In that latter case, the routing mechanism is not the controller, as the business logic is contained in the managed bean and the Model is strictly a domain model. 在后一种情况下,路由机制不是控制器,因为业务逻辑包含在托管bean中,而Model完全是域模型。 We then have: 然后我们有:

  • Model - contains the domain model, in most cases represents the tables in the database, persisted through DAOs. 模型 - 包含域模型,在大多数情况下代表数据库中的表,通过DAO持久化。

  • View - the ui components, connected to the bean; View - 连接到bean的ui组件;

  • Controller - the managed bean that contains the business logic and handles communication between the view and the model. Controller - 包含业务逻辑并处理视图和模型之间通信的托管bean。

I think some people confuse mediating-controller MVC as plain MVC, which leads to the different explanations encountered. 我认为有些人将中介控制器MVC混淆为普通的MVC,这会导致遇到不同的解释。

I think, all anwsers here are correct. 我想,这里所有的东西都是正确的。 But the main point for confusion arises for me from mixing the mvc components' definitions coming from the framework, here JSF, with those model and controller components you define in your application design: 但是混淆的主要原因是混合了来自框架的mvc组件定义,这里是JSF,以及您在应用程序设计中定义的模型和控制器组件:

Assume you change from JSF to any other Web UI framework in your business application: You will still have a "business model" and a "business controller". 假设您从业务应用程序中的JSF更改为任何其他Web UI框架:您仍将拥有“业务模型”和“业务控制器”。 (In our projects we just call these components "the model" and "the process".) (在我们的项目中,我们只称这些组件为“模型”和“过程”。)

What does this mean: Either you can't use mvc for the description of the overall application architecture, but solely for the UI, or you may have to accept many controller, model like components in your full application stack. 这意味着什么:您不能将mvc用于整个应用程序体系结构的描述,而只能用于UI,或者您可能必须在完整的应用程序堆栈中接受许多控制器,类似组件的模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM