JSF 托管 bean 命名约定 [英] JSF managed bean naming conventions
问题描述
这些天我曾经使用 JSF,但是有一个约定",我怀疑是否应该使用.在使用托管 bean 时,人们过去常常将其命名为 XxxxxManagedBean
,其中前缀可以是与您的业务相关的任何名称.
These days I used to work with JSF, but there's a "convention" I'm in doubt if I should use. While working with managed beans, people used to name it as XxxxxManagedBean
where the prefix can be any name related to your business.
你是这样工作的吗?特别是,尽管使搜索变得容易,但我不太喜欢.您使用的是其他约定吗?
Have you worked like that? Particularly, I don't like that much despite makes search easy. Are you using another convention?
感谢您回答这个简单的疑问.
Thanks for answering this simple doubt.
推荐答案
JSF 本身没有指定严格的约定.我见过以下约定:
There is no strict convention specified by JSF itself. I've seen the following conventions:
FooBean
FooBacking
FooManager
FooController
FooManagedBean
或者甚至只是 Foo
然后被放置在一个特定的包中,比如 com.example.controller
、com.example.backing
甚至com.example.view
等
Or even just Foo
which is then placed in a specific package like com.example.controller
, com.example.backing
or even com.example.view
, etc.
我自己倾向于将 FooManager
用于应用程序和会话范围的 bean(例如 DataManager
、UserManager
、LocaleManager
等)和只是 Foo
,或者按照我当前项目的要求,FooBacking
(例如 Login
或 LoginBacking
)用于请求和视图范围的 bean,它们通常与特定的
和/或视图相关联.
I myself tend to use FooManager
for application and session scoped beans (e.g. DataManager
, UserManager
, LocaleManager
, etc) and just Foo
, or as mandated by my current project, FooBacking
(e.g. Login
or LoginBacking
) for request and view scoped beans, which are each usually tied to a specific <h:form>
and/or view.
FooBean
太含糊了.确实很多类都可以标记为javabeans.JSF 托管 bean、JPA 实体、EJB、数据传输对象、值对象等.Bean
命名并不以任何方式指示类的真正职责.是的,我使用 经常 public class Bean
或 MyBean
在我的博客或论坛/问答答案中的通用代码示例中,但在现实世界中,您应该避免这种情况.
FooBean
is too vague. Really a lot of classes can be marked as javabeans. JSF managed beans, JPA entities, EJBs, data transfer objects, value objects, etc. The Bean
naming does not indicate the real responsibility of the class in any way. True, I use often public class Bean
or MyBean
in my generic code examples in blogs or forum/Q&A answers, but in real world you should avoid that.
FooManagedBean
在 IMO 是一个糟糕的名字,它不仅太长而且太丑,而且从技术上讲,托管 bean 是一个由某些人管理的支持 bean 的实例框架(在本例中为 JSF).类定义本身实际上是一个支持 bean,而不是托管 bean.所以一个 FooBackingBean
在技术上更正确,但它仍然太长,而且 Bean
部分有点痒.
FooManagedBean
is IMO a poor name, it's not only too long and ugly, but technically, a managed bean is an instance of a backing bean which is managed by some framework (JSF in this case). The class definition itself is really a backing bean, not a managed bean. So a FooBackingBean
is technically more correct, but it's still too long and the Bean
part is a bit itchy.
无论如何,这是一个非常主观的问题,很难用唯一正确"的答案来客观地回答.只要您在整个项目中与它保持一致,对我或其他任何人来说,这真的无关紧要.
Anyway, this is a pretty subjective question which can hardly be answered objectively with The One And Correct answer. It really doesn't matter that much to me or anyone else what you makes of it, as long as you're consistent with it throughout the entire project.
这篇关于JSF 托管 bean 命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!