将 JSF 理解为 MVC 框架 [英] Understanding JSF as a MVC framework

查看:20
本文介绍了将 JSF 理解为 MVC 框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 JSF,但我对为什么 JSF 是一个 MVC 框架感到很困惑(或者至少哪些部分属于哪个字母").

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 MVC 中哪些组件是 MVC框架?

如果您不在聚合视图中查看它,我会读到它,模型是您的实体,视图是您的 XHTML 代码,控制器是托管 bean.嗯...好吧,但是视图不是经常依赖于执行进一步的业务逻辑调用,例如返回一组实体,描述仍然适合吗?

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. 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?

我读过的一本书将其描述为托管 bean 是某种消息"传送器,Faces Servlet(控制器)使用它来调用业务层(模型),然后 XHTML 代码就是视图.

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.

有太多的解释和差异,所以我不知道如何理解它.

There are so many explanations and differences so I don't know which or how to understand it.

推荐答案

为什么在 JSF 和许多其他 web 框架中经常不完全清楚它的哪些部分对应于 MVC 的哪一部分的部分原因是 MVC 模式最初是为桌面应用程序设计的.

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.

在桌面应用程序中,节点 M、V 和 C 是最大连通图,这意味着每个部分都可以与其他部分进行通信.例如.如果模型发生变化,它可以将此更改推送到视图.这在桌面应用程序中有多个视图表示的情况下尤其明显.更改一个,并实时查看另一个更新.

In a desktop application, the nodes M, V and C are a maximum connected graph, meaning each part can communicate with every other part. E.g. 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.

由于 Web 应用程序的客户端/服务器和请求/响应性质,经典 MVC 无法 1:1 映射到大多数 Web 框架.

Due to the client/server and request/response nature of web applications, classic MVC doesn't map 1:1 to most web frameworks.

具体来说,在JSF中的映射如下:

Specifically, in JSF the mapping is as follows:

  • 模型 - 服务/DAO 以及它们生产和消费的实体.其入口点是托管 bean,但在 Java EE(JSF 是其中的一部分)中,这些工件通常分别由 EJB 和 JPA 实现.
  • 视图 - 将 UI 组件和它们组合成一个完整的页面.这完全在 JSF 领域内,分别由 JSF UIComponents 和 Facelets 实现.
  • 控制器 - 处理来自用户的命令和传入数据的交通警察,将其路由到正确的部分并选择要显示的视图.在 JSF 中没有编写这个控制器,但它已经由框架提供(它是 FacesServlet).
  • Model - The Services/DAOs plus the entities they produce and consume. 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.
  • View - The UI components and their composition into a full page. This is fully in the domain of JSF and implemented by JSF UIComponents and Facelets respectively.
  • 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 中,您没有实现控制器.因此,支持 bean 或任何其他类型的托管 bean 不是控制器.

Especially the last part is frequently not well understood: In JSF you don't implement a controller. Consequently, a backing bean or any other kind of managed bean is NOT the controller.

第一部分(模型)也并不总是很清楚.业务逻辑可能由 EJB 和 JPA 实现,但从 JSF 的角度来看,值绑定所引用的一切都是模型.这也是 JSF 生命周期阶段之一的名称的来源:Update Model.在这个阶段,JSF 将数据从 UI 组件推送到模型中.从这个意义上说,(JSF) 托管 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. This is also where the name of one of the JSF life-cycle phases comes from: Update Model. In this phase JSF pushes data from the UI components into the model. In that sense, (JSF) managed beans are thus the model.

虽然 JSF 本身并没有明确定义这个概念,但是托管 bean 经常重复出现并且具有特定的用法,称为支持 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 仍然是模型,但实际上它是位于模型、视图和控制器中间的管道元素.因为它执行一些可能被视为某些控制器任务的任务,所以它经常被误认为是控制器.但是,如前所述,这是不正确的.它还可以执行一些模型任务,偶尔也会做一些视图逻辑.

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. 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.

另见:

这篇关于将 JSF 理解为 MVC 框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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