简体   繁体   English

区分不同种类的JSF托管Bean

[英]Making Distinctions Between Different Kinds of JSF Managed-Beans

I recently read this article from Neil Griffin Making Distinctions Between Different Kinds of JSF Managed-Beans and it got me thinking about the distinction between different beans in my own application. 我最近阅读了Neil Griffin的《区分不同类型的JSF托管豆》中的这篇文章,这让我开始思考自己的应用程序中不同bean之间的区别。 To quickly summarise the gist: 快速总结要点:

  • Model Managed-Bean: This type of managed-bean participates in the "Model" concern of the MVC design pattern. Model Managed-Bean:这种类型的Managed-Bean参与MVC设计模式的“模型”关注。 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. JSF模型bean应该是遵循JavaBean设计模式且具有getters / setter封装属性的POJO。

  • Backing Managed-Bean: This type of managed-bean participates in the "View" concern of the MVC design pattern. 支持Managed-Bean:这种类型的Managed-Bean参与MVC设计模式的“视图”关注。 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. 支持bean的目的是支持UI逻辑,并且与JSF视图或Facelet组合中的JSF表单具有1 :: 1的关系。 Although it typically has JavaBean-style properties with associated getters/setters, these are properties of the View -- not of the underlying application data model. 尽管它通常具有带有关联的getter / setter的JavaBean样式的属性,但它们是View的属性,而不是基础应用程序数据模型的属性。 JSF backing-beans may also have JSF actionListener and valueChangeListener methods. JSF支持bean也可以具有JSF actionListener和valueChangeListener方法。

  • Controller Managed-Bean: This type of managed-bean participates in the "Controller" concern of the MVC design pattern. Controller Managed-Bean:这种类型的Managed Bean参与了MVC设计模式的“ Controller”关注。 The purpose of a controller bean is to execute some kind of business logic and return a navigation outcome to the JSF navigation-handler. 控制器bean的目的是执行某种业务逻辑并将导航结果返回给JSF导航处理程序。 JSF controller-beans typically have JSF action methods (and not actionListener methods). JSF控制器bean通常具有JSF动作方法(而不是actionListener方法)。

  • Support Managed-Bean: This type of bean "supports" one or more views in the "View" concern of the MVC design pattern. 支持托管的Bean:这种类型的Bean在MVC设计模式的“视图”方面“支持”一个或多个视图。 The typical use case is supplying an ArrayList to JSF h:selectOneMenu drop-down lists that appear in more than one JSF view. 典型的用例是向JSF h:selectOneMenu下拉列表提供一个ArrayList,这些列表出现在多个JSF视图中。 If the data in the dropdown lists is particular to the user, then the bean would be kept in session scope. 如果下拉列表中的数据特定于用户,则Bean将保留在会话范围内。

  • Utility Managed-Bean: This type of bean provides some type of "utility" function to one or more JSF views. 实用程序托管Bean:这种类型的Bean为一个或多个JSF视图提供某种“实用程序”功能。 A good example of this might be a FileUpload bean that can be reused in multiple web applications. 一个很好的例子就是FileUpload bean,它可以在多个Web应用程序中重用。

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 AuthenticationController is an example of a Controller Managed-Bean. AuthenticationController是控制器受管Bean的示例。 It is request-scoped and features two getters and setters for setting a username and password, and two navigation methods, authenticate and logout , navigating the user to either their private area upon successful login, or back to the main page when logging out. 它是请求范围的,具有两个用于设置用户名和密码的getter和setter,以及两个导航方法( authenticatelogout ,可在成功登录后将用户导航到其专用区域,或在logout时导航到主页。

The UserBean is an example of a Support Managed-Bean. UserBean是Support Managed-Bean的示例。 It is session-scoped and features an instance of User class (which would be null when you are not authenticated) with a getter and setter, nothing more. 它是会话作用域的,具有User类的实例(如果未通过身份验证,则为null),该实例具有getter和setter,仅此而已。

The AuthenticationController has this user as a managed property ( @ManagedProperty(value = "#{userController.user} private User user; ). Upon successful authentication, the AuthenticationController would 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成功后, AuthenticationController会将Managed属性设置为实际用户实例,并使用相应的用户名用于登录。

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 User class 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? 这样就可以解决关注点分离问题吗?

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. Model Managed-Bean:这种类型的Managed-Bean参与MVC设计模式的“模型”关注。 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. JSF模型bean应该是遵循JavaBean设计模式且具有getters / setter封装属性的POJO。

I would absolutely not make or call it a managed bean. 我绝对不会制造或称其为托管bean。 Just make it a property of a @ManagedBean . 只需将其设置为@ManagedBean的属性即可。 For example a DTO or JPA @Entity . 例如DTO或JPA @Entity


Backing Managed-Bean: This type of managed-bean participates in the "View" concern of the MVC design pattern. 支持Managed-Bean:这种类型的Managed-Bean参与MVC设计模式的“视图”关注。 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. 支持bean的目的是支持UI逻辑,并且与JSF视图或Facelet组合中的JSF表单具有1 :: 1的关系。 Although it typically has JavaBean-style properties with associated getters/setters, these are properties of the View -- not of the underlying application data model. 尽管它通常具有带有关联的getter / setter的JavaBean样式的属性,但它们是View的属性,而不是基础应用程序数据模型的属性。 JSF backing-beans may also have JSF actionListener and valueChangeListener methods. JSF支持bean也可以具有JSF actionListener和valueChangeListener方法。

This way you keep duplicating and mapping the properties of the entity in the managed bean. 这样,您就可以重复复制和映射托管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的属性,然后让输入字段像#{authenticator.user.name}而不是#{authenticator.username}一样直接引用它即可。


Controller Managed-Bean: This type of managed-bean participates in the "Controller" concern of the MVC design pattern. Controller Managed-Bean:这种类型的Managed Bean参与了MVC设计模式的“ Controller”关注。 The purpose of a controller bean is to execute some kind of business logic and return a navigation outcome to the JSF navigation-handler. 控制器bean的目的是执行某种业务逻辑并将导航结果返回给JSF导航处理程序。 JSF controller-beans typically have JSF action methods (and not actionListener methods). JSF控制器bean通常具有JSF动作方法(而不是actionListener方法)。

This describes the @RequestScoped / @ViewScoped @ManagedBean class pretty much. 这几乎描述了@RequestScoped / @ViewScoped @ManagedBean类。 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. 是否允许使用事件侦听器方法取决于它们是否特定于绑定到Bean的视图和/或用于其工作的方式取决于Bean的状态。 If they are, then they belongs in the bean. 如果它们是,则它们属于bean。 If not, then they should be a standalone implementation of any FacesListener interface , but definitely not a managed bean. 如果不是,那么它们应该是任何FacesListener接口的独立实现,但绝对不是托管Bean。


Support Managed-Bean: This type of bean "supports" one or more views in the "View" concern of the MVC design pattern. 支持托管的Bean:这种类型的Bean在MVC设计模式的“视图”方面“支持”一个或多个视图。 The typical use case is supplying an ArrayList to JSF h:selectOneMenu drop-down lists that appear in more than one JSF view. 典型的用例是向JSF h:selectOneMenu下拉列表提供一个ArrayList,这些列表出现在多个JSF视图中。 If the data in the dropdown lists is particular to the user, then the bean would be kept in session scope. 如果下拉列表中的数据特定于用户,则Bean将保留在会话范围内。

Fine. 精细。 For application wide data like dropdown lists just use an @ApplicationScoped bean and for session wide data like logged-in user and its preferences just use a @SessionScoped one. 对于应用程序范围的数据(如下拉列表),只需使用@ApplicationScoped bean;对于会话范围的数据,如登录的用户及其首选项,只需使用@SessionScoped


Utility Managed-Bean: This type of bean provides some type of "utility" function to one or more JSF views. 实用程序托管Bean:这种类型的Bean为一个或多个JSF视图提供某种“实用程序”功能。 A good example of this might be a FileUpload bean that can be reused in multiple web applications. 一个很好的例子就是FileUpload bean,它可以在多个Web应用程序中重用。

This makes not really sense to me. 这对我来说真的没有意义。 Backing beans are usually tied to single views. 支持bean通常绑定到单个视图。 This sounds too much like an ActionListener implementation which is to be used by <f:actionListener> in command components to your choice. 这听起来太像一个ActionListener实现,供您选择的命令组件中的<f:actionListener>使用。 Definitely not a managed bean. 绝对不是托管bean。

For kickoff examples of the right approach, see also: 有关正确方法的启动示例,另请参阅:

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

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