java 区分不同种类的 JSF Managed-Beans

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7223055/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:10:13  来源:igfitidea点击:

Making Distinctions Between Different Kinds of JSF Managed-Beans

javajsfjakarta-eebacking-beans

提问by Laurens

I recently read this article from Neil Griffin Making Distinctions Between Different Kinds of JSF Managed-Beansand it got me thinking about the distinction between different beans in my own application. To quickly summarise the gist:

我最近阅读了 Neil Griffin 发表的《区分不同种类的 JSF 托管 Bean 之间的区别》的这篇文章,它让我开始思考我自己的应用程序中不同 Bean 之间的区别。快速总结一下要点:

  • Model Managed-Bean: This type of managed-bean participates in the "Model" concern of the MVC design pattern. When you see the word "model" -- think DATA. A JSF model-bean should be a POJO that follows the JavaBean design pattern with getters/setters encapsulating properties.

  • Backing Managed-Bean: This type of managed-bean participates in the "View" concern of the MVC design pattern. The purpose of a backing-bean is to support UI logic, and has a 1::1 relationship with a JSF view, or a JSF form in a Facelet composition. Although it typically has JavaBean-style properties with associated getters/setters, these are properties of the View -- not of the underlying application data model. JSF backing-beans may also have JSF actionListener and valueChangeListener methods.

  • Controller Managed-Bean: This type of managed-bean participates in the "Controller" concern of the MVC design pattern. The purpose of a controller bean is to execute some kind of business logic and return a navigation outcome to the JSF navigation-handler. JSF controller-beans typically have JSF action methods (and not actionListener methods).

  • Support Managed-Bean: This type of bean "supports" one or more views in the "View" concern of the MVC design pattern. The typical use case is supplying an ArrayList to JSF h:selectOneMenu drop-down lists that appear in more than one JSF view. If the data in the dropdown lists is particular to the user, then the bean would be kept in session scope.

  • Utility Managed-Bean: This type of bean provides some type of "utility" function to one or more JSF views. A good example of this might be a FileUpload bean that can be reused in multiple web applications.

  • Model Managed-Bean:这种类型的 managed-bean 参与了 MVC 设计模式的“模型”关注点。当你看到“模型”这个词时——想想数据。JSF 模型 bean 应该是一个 POJO,它遵循 JavaBean 设计模式,使用 getter/setter 封装属性。

  • Backing Managed-Bean:这种类型的托管 bean 参与了 MVC 设计模式的“视图”关注点。后台bean 的目的是支持UI 逻辑,并且与JSF 视图或Facelet 组合中的JSF 表单具有1::1 关系。尽管它通常具有带有关联 getter/setter 的 JavaBean 样式属性,但这些属性是 View 的属性——而不是底层应用程序数据模型的属性。JSF 支持 bean 也可能有 JSF actionListener 和 valueChangeListener 方法。

  • Controller Managed-Bean:这种类型的托管 bean 参与了 MVC 设计模式的“控制器”关注点。控制器 bean 的目的是执行某种业务逻辑并将导航结果返回给 JSF 导航处理程序。JSF 控制器 bean 通常具有 JSF 操作方法(而不是 actionListener 方法)。

  • Support Managed-Bean:这种类型的 bean “支持”MVC 设计模式的“视图”关注点中的一个或多个视图。典型的用例是向出现在多个 JSF 视图中的 JSF h:selectOneMenu 下拉列表提供一个 ArrayList。如果下拉列表中的数据特定于用户,则 bean 将保留在会话范围内。

  • Utility Managed-Bean:这种类型的 bean 为一个或多个 JSF 视图提供某种类型的“实用程序”功能。一个很好的例子可能是可以在多个 Web 应用程序中重用的 FileUpload bean。

This made sense to me and for the past few hours I have been refactoring my code and came up with the following with respect to the user login:

这对我来说很有意义,在过去的几个小时里,我一直在重构我的代码,并就用户登录提出以下几点:

The AuthenticationControlleris an example of a Controller Managed-Bean. It is request-scoped and features two getters and setters for setting a username and password, and two navigation methods, authenticateand logout, navigating the user to either their private area upon successful login, or back to the main page when logging out.

AuthenticationController是 Controller Managed-Bean 的一个示例。这是请求范围,并配备了两个getter和setter方法用于设置用户名和密码,两个导航方法,authenticatelogout注销时,成功登录后导航用户或者他们的私人空间,或者回到主页。

The UserBeanis an example of a Support Managed-Bean. It is session-scoped and features an instance of Userclass (which would be null when you are not authenticated) with a getter and setter, nothing more.

UserBean是 Support Managed-Bean 的一个例子。它是会话范围的,并具有一个User带有 getter 和 setter的类实例(当您未通过身份验证时将为 null),仅此而已。

The AuthenticationControllerhas this user as a managed property (@ManagedProperty(value = "#{userController.user} private User user;). Upon successful authentication, the AuthenticationControllerwould set the managed property to the actual user instance with the corresponding username that was used for the login.

AuthenticationController有这个用户作为托管属性(@ManagedProperty(value = "#{userController.user} private User user;)。成功验证后,AuthenticationController将使用用于登录的相应用户名将托管属性设置为实际用户实例。

Any new beans would be able to grab the user as a managed property as well and pull the data they need, such as group membership for instance, if the Userclass would feature a list with group names.

如果User类具有包含组名的列表,则任何新 bean 也能够将用户作为托管属性获取并提取他们需要的数据,例如组成员身份。

Would this way be the proper way to go about with regard to the seperation of concerns?

这种方式是否是分离关注点的正确方式?

回答by BalusC

This is a very subjective question. I personally disagree that article and find that it's giving really bad advice to starters.

这是一个非常主观的问题。我个人不同意那篇文章,并发现它给初学者提供了非常糟糕的建议。



Model Managed-Bean: This type of managed-bean participates in the "Model" concern of the MVC design pattern. When you see the word "model" -- think DATA. A JSF model-bean should be a POJO that follows the JavaBean design pattern with getters/setters encapsulating properties.

Model Managed-Bean:这种类型的 managed-bean 参与了 MVC 设计模式的“模型”关注点。当你看到“模型”这个词时——想想数据。JSF 模型 bean 应该是一个 POJO,它遵循 JavaBean 设计模式,使用 getter/setter 封装属性。

I would absolutely not make or call it a managed bean. Just make it a property of a @ManagedBean. For example a DTO or JPA @Entity.

我绝对不会将其称为托管 bean。只需将其设为 a 的属性即可@ManagedBean。例如 DTO 或 JPA @Entity



Backing Managed-Bean: This type of managed-bean participates in the "View" concern of the MVC design pattern. The purpose of a backing-bean is to support UI logic, and has a 1::1 relationship with a JSF view, or a JSF form in a Facelet composition. Although it typically has JavaBean-style properties with associated getters/setters, these are properties of the View -- not of the underlying application data model. JSF backing-beans may also have JSF actionListener and valueChangeListener methods.

Backing Managed-Bean:这种类型的托管 bean 参与了 MVC 设计模式的“视图”关注点。后台bean 的目的是支持UI 逻辑,并且与JSF 视图或Facelet 组合中的JSF 表单具有1::1 关系。尽管它通常具有带有关联 getter/setter 的 JavaBean 样式属性,但这些属性是 View 的属性——而不是底层应用程序数据模型的属性。JSF 支持 bean 也可能有 JSF actionListener 和 valueChangeListener 方法。

This way you keep duplicating and mapping the properties of the entity in the managed bean. This makes no sense to me. As said, just make the entity a property of the managed bean and let the input fields refer it directly like #{authenticator.user.name}instead of #{authenticator.username}.

通过这种方式,您可以不断复制和映射托管 bean 中实体的属性。这对我来说毫无意义。如上所述,只需使实体成为托管 bean 的属性,并让输入字段直接引用它,#{authenticator.user.name}而不是#{authenticator.username}.



Controller Managed-Bean: This type of managed-bean participates in the "Controller" concern of the MVC design pattern. The purpose of a controller bean is to execute some kind of business logic and return a navigation outcome to the JSF navigation-handler. JSF controller-beans typically have JSF action methods (and not actionListener methods).

Controller Managed-Bean:这种类型的托管 bean 参与了 MVC 设计模式的“控制器”关注点。控制器 bean 的目的是执行某种业务逻辑并将导航结果返回给 JSF 导航处理程序。JSF 控制器 bean 通常具有 JSF 操作方法(而不是 actionListener 方法)。

This describes the @RequestScoped/@ViewScoped@ManagedBeanclass pretty much. Whether event listener methods are allowed or not depends on whether they are specific to the view which is tied to the bean and/or are for their job dependent on the bean's state. If they are, then they belongs in the bean. If not, then they should be a standalone implementation of any FacesListenerinterface, but definitely not a managed bean.

这几乎描述了@RequestScoped/@ViewScoped@ManagedBean类。是否允许事件侦听器方法取决于它们是否特定于绑定到 bean 的视图和/或它们的工作取决于 bean 的状态。如果是,则它们属于 bean。如果不是,那么它们应该是任何FacesListenerinterface的独立实现,但绝对不是托管 bean。



Support Managed-Bean: This type of bean "supports" one or more views in the "View" concern of the MVC design pattern. The typical use case is supplying an ArrayList to JSF h:selectOneMenu drop-down lists that appear in more than one JSF view. If the data in the dropdown lists is particular to the user, then the bean would be kept in session scope.

Support Managed-Bean:这种类型的 bean “支持”MVC 设计模式的“视图”关注点中的一个或多个视图。典型的用例是向出现在多个 JSF 视图中的 JSF h:selectOneMenu 下拉列表提供一个 ArrayList。如果下拉列表中的数据特定于用户,则 bean 将保留在会话范围内。

Fine. For application wide data like dropdown lists just use an @ApplicationScopedbean and for session wide data like logged-in user and its preferences just use a @SessionScopedone.

美好的。对于下拉列表等应用程序范围的数据,只需使用一个@ApplicationScopedbean,对于会话范围的数据,如登录用户及其首选项,只需使用一个 bean @SessionScoped



Utility Managed-Bean: This type of bean provides some type of "utility" function to one or more JSF views. A good example of this might be a FileUpload bean that can be reused in multiple web applications.

Utility Managed-Bean:这种类型的 bean 为一个或多个 JSF 视图提供某种类型的“实用程序”功能。一个很好的例子可能是可以在多个 Web 应用程序中重用的 FileUpload bean。

This makes not really sense to me. Backing beans are usually tied to single views. This sounds too much like an ActionListenerimplementation which is to be used by <f:actionListener>in command components to your choice. Definitely not a managed bean.

这对我来说没有意义。支持 bean 通常绑定到单个视图。这听起来太像一个ActionListener<f:actionListener>您选择的命令组件使用的实现。绝对不是托管 bean。

For kickoff examples of the right approach, see also:

有关正确方法的启动示例,另请参见: