JSF 托管 Bean 命名约定

发布于 2024-12-27 09:24:27 字数 169 浏览 0 评论 0原文

这些天我曾经使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

所谓喜欢 2025-01-03 09:24:27

JSF 本身没有指定严格的约定。我见过以下约定:

  • FooBean
  • FooBacking
  • FooManager
  • FooController
  • FooManagedBean

甚至只是 Foo ,然后将其放置在特定的包中,例如 com.example.controllercom.example.backing 甚至 com. example.view

我自己倾向于使用FooManager 用于应用程序和会话范围的 bean(例如 DataManagerUserManagerLocaleManager 等)以及 Foo,或者按照我当前项目的要求,FooBacking(例如LoginLoginBacking)用于请求和查看作用域bean,这每个通常都与特定的 和/或视图。

FooBean 太模糊了。确实有很多类可以标记为 javabean。 JSF 托管 Bean、JPA 实体、EJB、数据传输对象、值对象等。Bean 命名并不以任何方式表明该类的真正职责。确实,我经常 public class BeanMyBean 在博客或论坛/问答答案中的通用代码示例中,但在现实世界中您应该避免这样做。

FooManagedBean 在我看来是一个糟糕的名字,它不仅太长而且丑陋,而且从技术上讲,托管 bean 是由某个框架(JSF)管理的支持 bean 的实例在这种情况下)。 类定义本身实际上是一个支持bean,而不是托管bean 。因此,FooBackingBean 在技术上更正确,但它仍然太长,而且 Bean 部分有点痒。

无论如何,这是一个相当主观的问题,很难用唯一正确的答案来客观地回答。对于我或其他任何人来说,你如何看待它并不重要,只要你在整个项目中保持一致即可。

There is no strict convention specified by JSF itself. I've seen the following conventions:

  • FooBean
  • FooBacking
  • FooManager
  • FooController
  • FooManagedBean

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文