区分不同种类的 JSF Managed-Beans [英] Making Distinctions Between Different Kinds of JSF Managed-Beans

查看:24
本文介绍了区分不同种类的 JSF Managed-Beans的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近阅读了 Neil Griffin 的这篇文章区分不同种类的 JSF Managed-Beans 和它让我思考了我自己的应用程序中不同 bean 之间的区别.快速总结一下要点:

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. To quickly summarise the gist:

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

  • 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:这种类型的托管 bean 参与MVC 设计模式的视图"关注点.的目的backing-bean 是支持 UI 逻辑,和 1::1 关系JSF 视图,或 Facelet 组合中的 JSF 表单.虽然它通常具有带有关联的 JavaBean 样式的属性getter/setter,这些是视图的属性——而不是视图的属性底层应用程序数据模型.JSF 支持 bean 也可能有 JSFactionListener 和 valueChangeListener 方法.

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:这种类型的托管bean参与MVC 设计模式的控制器"关注点.的目的控制器 bean 是执行某种业务逻辑并返回一个JSF 导航处理程序的导航结果.JSF 控制器 bean通常有 JSF 操作方法(而不是 actionListener 方法).

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:这种类型的bean支持"一个或多个视图在 MVC 设计模式的视图"关注点中.典型用例正在向 JSF h:selectOneMenu 下拉菜单提供一个 ArrayList出现在多个 JSF 视图中的列表.如果数据在下拉列表是特定于用户的,那么 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.

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

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.

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

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:

AuthenticationController 是控制器托管 Bean 的一个示例.它是请求范围的,具有两个用于设置用户名和密码的 getter 和 setter,以及两个导航方法,authenticatelogout,将用户导航到他们的私人区域登录成功,或退出时返回主页面.

The AuthenticationController is 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, authenticate and logout, navigating the user to either their private area upon successful login, or back to the main page when logging out.

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

The UserBean is an example of a 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.

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

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.

如果 User 类具有包含 group 的列表名字.

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.

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

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:这种类型的托管 bean 参与了 MVC 设计模式的模型"关注点.当你看到模型"这个词时——想想数据.JSF 模型 bean 应该是遵循 JavaBean 设计模式的 POJO,使用 getter/setter 封装属性.

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

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.

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

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

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

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

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

This describes the @RequestScoped/@ViewScoped @ManagedBean class 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 FacesListener interface, but definitely not a managed bean.

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

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

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.

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

这对我来说没有意义.支持 bean 通常绑定到单个视图.这听起来太像 ActionListener 实现,由 在您选择的命令组件中使用.绝对不是托管 bean.

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

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

For kickoff examples of the right approach, see also:

这篇关于区分不同种类的 JSF Managed-Beans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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