简体   繁体   English

JSF注销页面-最佳实践:使用一个通用bean是否正确?

[英]JSF logout page - best practices: is it correct to use one common bean?

Is it correct/ a best practice to have a commonBean for operations that are common to the application? 为应用程序中常见的操作使用commonBean是正确/最佳实践吗? In my case this would be a logout function that clears the logged value of a session bean. 在我的情况下,这将是注销功能,用于清除会话Bean的记录值。

One of my coworkers told me that only one controller bean should be used per view, however it seems like a waste to have the same method replicated in multiple beans. 我的一位同事告诉我,每个视图只能使用一个控制器bean,但是在多个bean中复制相同的方法似乎很浪费。

Your colleague is referring to the seperation of View and Controller as a loosely defined principle in a pure MVC pattern. 您的同事将View和Controller的分离称为纯MVC模式中松散定义的原理。 The Managed Bean in a JSF application lends itself well to the role of a Controller, however this should not be considered a hard and fast rule for the very reasons that you have stated. JSF应用程序中的Managed Bean非常适合充当Controller的角色,但是由于您已经陈述的原因,这不应被视为一成不变的规则。

I quote Martin Fowler from Patterns of Enterprise Application Architecture: 我引用《企业应用程序体系结构模式》中的Martin Fowler:

As I said, the value of MVC lies in its two separations. 正如我所说,MVC的价值在于它的两个分离。 Of these are the separation of presentation and model is one of the most important design principles in software, and the only time you shouldn't follow it is in very simple systems where the model has no real behavior in it anyway. 其中,表示和模型的分离是软件中最重要的设计原则之一,而您唯一不应该遵循的是在非常简单的系统中,其中模型始终没有实际行为。 As soon as you get some nonvisual logic you should apply the separation. 一旦获得一些非视觉逻辑,就应该应用分隔。 Unfortunately, a lot of UI frameworks make it difficult, and those that don't are often taught without a separation. 不幸的是,许多UI框架使其变得很困难,而那些没有框架的UI框架经常被教给他们。

The separation of view and controller is less important , so I'd only recommend doing it when it is really helpful. 视图和控制器的分离不太重要 ,因此,仅在确实有帮助时才建议这样做。 For rich-client systems, that ends up being hardly ever, although it is common in Web front ends where the controller is separated out. 对于富客户端系统,尽管在分离控制器的Web前端很常见,但这种情况几乎很难实现。 Most of the patterns on Web design here are based on that principle. Web设计中的大多数模式都基于该原理。

Essentially Fowler states that binding view to controller is not as important as binding model to view, so utilizing controllers across views or multiple controllers per view is certainly acceptable if it is helpful to do so. 本质上,Fowler指出,将视图绑定到控制器并不像将模型绑定到视图一样重要,因此,如果有帮助的话,当然可以接受跨视图使用控制器或每个视图使用多个控制器。

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

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