困惑:与经典 MVC 控制器相比,JSF2 中 Bean 的作用

发布于 2024-12-28 14:37:20 字数 1299 浏览 5 评论 0原文

我有一个与设计和架构更多相关的问题。我有基于经典 MVC 的背景,必须亲自接触 JSF2。我阅读了有关 JSF2 的 IBM 文章 (http://www.ibm.com/developerworks /library/j-jsf1/)并认为我理解总体概念。

我通过ROO开始接触JSF2。我有一种感觉,ROO(也许这对于任何 JSF2 类型的应用程序都是如此,也许不是)正在对 bean 进行非常奇怪/不清楚的使用。一般来说,我真的不清楚 Bean 的实际作用是什么!例如,如果我有一个带有用于编辑单个用户条目的表单的视图,我将在 a 中初始化用户,我们将其称为 UserBean (可能将其存储在成员变量中)并通过 getter 访问此变量。如果我现在想要概述所有用户,我将再次在 UserBean 中呈现视图,保存用户集合,并再次通过 getter 访问该集合。前面的描述实际上是我使用 jsf 做事的方式。这意味着我将 UserBean 更多地用作有状态服务作为控制器。

在典型的控制器情况下,我将为每种类型的操作(列出用户、编辑用户、查看用户等)创建一个单独的控制器,并具有特定的初始化数据,这样我将通过控制器分离逻辑的上下文。

我经常使用特定于上下文的服务,例如,如果我经常处理用户在应用程序中的分布,我会创建一个用户服务来处理特定于用户的逻辑,该逻辑可能太复杂而无法放入其自身中。例如,如果我现在研究 roo 生成的 Bean,我会找到以编程方式呈现表单、输入字段和标签的方法,这些方法再次存储用户列表、指示数据是否已加载的布尔字段、单个用户成员等等更像是放入 UserService (或其他)的方法。我想知道这是否是 JSF2 的预期使用方式,换句话说:将与一个上下文相关的所有内容推送到 bean 中,不使用服务,并编写处理所有内容的“超级控制器 bean”。

我真的不知道你的问题是否正确,但可能对我有帮助的是,

  1. 一个非常具有示范性和值得称赞的示例应用程序的提示,该应用程序以与 jsf2 功能结合使用的方式使用 bean 和例如,围绕给定类型的实体实现基本 CRUD 用例。 (一个令人困惑的大问题是,在我的例子中,ROO 总是使用 AJAX 和 javascript 的东西,比如 Modal-Dialogs 来实现 CRUD 逻辑。我想知道 JSF 是否有一种更经典的方法来实现这一点? [使用“经典”我例如,表示基于 URl 的视图和用于列出、编辑和查看实体的分离视图])
  2. 一种启发典型“好人做事的方式”JSF 模式的资源(也许这是 J2EE 模式?)。

太感谢了!

如果我不清楚,请随时督促我具体说明具体要点!

I have a question that is more design and architecture related. I am coming from a classical MVC based background and have to get my hands dirty on JSF2. I read the IBM articles on JSF2 (http://www.ibm.com/developerworks/library/j-jsf1/) and think I understand the overall concept.

I started to get in touch with JSF2 through ROO. I have the feeling that ROO (maybe this is true for any JSF2-Type App, or maybe not) is making very strange/unclear use of beans. It is in general really not clear to me what the actual role of a Bean is! For example, if I have a view with a form that is for editing a single user-entry, i would initialize the user in a, lets call it UserBean (maybe store it in a member variable) and access this variable trough getters. If i now want to overview all users, I would again render the view in in the UserBean hold a collection of users and again access this collection through getters. The previous description is actually the way i would do things with jsf. This means I would user the UserBean more as a stateful-service as a controller.

In a typical controller situation i would create for every type of action (list user, edit user, view user, etc) a separate controller, with specific initialized data and this way i would separate the context of the logic by controllers.

I often use context-specific services, e.g., if I handle user's often an spread over the application, I create a user-service that handles user-specific logic that is maybe too complex to be put into itself. If I now for example look into roo generated Beans, I would find methods that programmatically render forms, input fields, and labels, that again store list's of users, Boolean fields that indicate if data had already been loaded, single user members and a lot of methods that more look like to be put into a UserService (or whatever). I am wondering if this is the way JSF2 is intended to be used, in words: pushing everything that is related to one context into on bean, not making use of service, and writing "super-controller-beans" that handle everything.

I don't really know if you get the question right, but what would maybe help me is, a hint to

  1. a very exemplary and commendable example application that makes use of beans the way they were intended to be used in combination with jsf2 features and usecases that for example implement basic CRUD usecases around a given type of entity. (One big confusing point is, that in my case ROO always makes use of AJAX and javascript stuff like Modal-Dialogs to implement CRUD logic. I wonder if with JSF there is a more classical way to to this? [With 'classical' I mean for example URl-based views and separated views for listing, editing, and viewing entities])
  2. a resource that enlightens typical "thats-the-way-the-good-guys-do-it" JSF-Patterns (maybe this is J2EE Patterns?).

Thank you so much!

Please feel free the push me to concretize specific points if I am not clear!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

桃扇骨 2025-01-04 14:37:20

您发布的 JSF2 链接指向 JSF1.2 文章。如果您想开始使用 JSF2 或 JSF,我建议您点击以下链接。

我建议从普通的普通 JSF 开始,而不是使用 JSF 的 ROO 来掌握 JSF。

回答您的问题

  • 第一个链接为您提供了简单的 jsf 示例,在 JSF 中您可以同时使用基于 ajax 和经典的提交表单的方式。在 JSF 1.x 版本中,ajax 并不是 JSF 的重要组成部分,它是由第三方组件库(主要是 RichFaces 和 PrimeFaces 等)实现的。在JSF2中有对ajax的内置支持,这不适用于不再需要的第三方组件,它们仍然提供一些扩展功能。我建议您浏览这个链接 找出 JSF 1.x 和 JSF 2 之间的差异。
  • 我不知道的模式(例如特定于 JSF 的代码)可以分类为模型 - 视图 - 控制器。典型情况 Person 代表模型,PersonMangedBean 扮演控制器的角色,它扮演从视图(jsp/facelets)获取数据的核心角色,在 bean 本身或服务 bean 处理数据后处理导航到经典视图可能是 listPersons.xhtml。
  • JSF 托管 bean 并不是处理该 bean 中所有内容的“超级控制器 bean”。我尝试按照您提到的方式对事物进行分类,即有一个服务层,其中所有业务逻辑可能是 EJB 或 Spring 托管 bean,并且它至少将业务逻辑与视图技术 JSF 分离,从而可以在其他地方重用它(服务)如果设计得当,可以作为一个库。
  • 提示:JSF 是基于组件的框架,而不是基于操作的框架,它有自己的生命周期,掌握该生命周期将节省大量时间并正确理解框架。这个 链接 对于 JSF 1.x 来说很好对于 JSF2 也是如此,用于对生命周期的基本了解。

希望这有帮助。

The link for JSF2 you have posted points to JSF1.2 article. In case you want to start of with JSF2 or JSF I suggest following links.

I'll suggest start with plain vanilla JSF rather than ROO with JSF to get a hang of JSF.

To answer your question

  • First link provides you with simple jsf examples, in JSF you can have both ajax based and classical way of submitting form. In JSF 1.x versions ajax was not part and parcel of JSF it was implemented by third party component library mainly RichFaces and PrimeFaces to name few. In JSF2 there is inbuilt support for ajax, this does not apply third party components are no longer required, they still provide some extended features. I'll suggest go through this link to find differences between JSF 1.x and JSF 2.
  • Patterns I am not aware of as such as specific to JSF apart code can be categorized in model - view - controller. Typical case Person represents model, PersonMangedBean plays role of controller which plays central role of getting data from view(jsp/facelets) and after processing data in bean itself or service beans handles navigation to classic views may be listPersons.xhtml.
  • JSF managed beans are not "super-controller-beans" handling every thing in that bean. I try to categorize things the way you mentioned i.e. have a service layer where we have all business logic may be EJB or Spring managed bean and it decouples at-least business logic away from view technology JSF whereby it(service) can be reused somewhere else as a library if designed properly.
  • Tip: JSF is component based framework not an action based and it has lifecycle of its own, do get a grip of that life-cycle will save lots of time and proper understanding of the framework. This link though for JSF 1.x holds good for JSF2 too, for basic understanding of life-cycle.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文