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
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,通常每个bean都绑定到特定的<h:form>
和/或视图.
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是由某个框架(在此为JSF)管理的后备bean的 instance 案件). 类定义本身实际上是支持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屋!