了解托管 Bean/支持 Bean

发布于 2024-12-06 16:32:33 字数 194 浏览 2 评论 0原文

我正在学习 Java EE 6,我正在尝试掌握它的整体形象。我正在阅读有关 JSF 以及如何添加组件的内容。我正在将组件中的值设置/读取到具有 @ManagedBean 注释的 bean。

我在正确理解它时遇到一些困难。什么是托管bean?仅仅是保存组件状态的对象吗?他们还可以有其他方法吗? EJB 适用于哪里?托管 bean 是否调用 EJB 上的方法?

I am learning Java EE 6 and I am trying to grasp the overall image of it. I am reading about JSF and how adding components. I am setting/reading values from the components to a bean which has the @ManagedBean annotation.

I have some trouble understanding it properly. What is Managedbeans? Is it just just objects that holds the state of the components? And they can have other methods as well? Where does the EJBs fit in? Does the managed beans invoked methods on the EJBs?

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

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

发布评论

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

评论(2

雪若未夕 2024-12-13 16:32:33

什么是托管bean?是否只是保存状态的对象
组件?

JSF 托管 bean 与任何其他 Java bean 类似,只不过它由 JSF 管理。换句话说,它是一个由 JSF 根据需要创建和销毁的 bean。

Hortsman Core JSF 2 书中指出。

JSF 实现执行以下操作:

  1. 根据需要创建和丢弃 Bean(因此术语“托管
    beans”)
  2. 显示网页时读取 Bean 属性
  3. 发布表单时设置 Bean 属性

他们还可以有其他方法吗?

是的,它们可以有您想要的尽可能多的方法。但是,理想情况下您会(并且应该)希望托管 bean 尽可能精简。例如,它可能有一个搜索方法,但您不应该在该方法内进行实际搜索但这种搜索方法的唯一目的应该是将任务委托给业务层(可能是也可能不是基于 EJB 的)。
换句话说,没有繁重的工作

EJB 适用于哪里?

EJB 是您的业务层,它们拥有强大的二头肌并承担所有繁重的工作。由于 EJB3 JPA 被引入,它也是 EJB 的一部分。然而,JPA 是持久层。除 JPA 之外的所有 EJB 都在 EJB 容器内运行。所有 Java EE 兼容服务器都提供这些。

在典型的 3 层架构中(目前大多超过 3 层,但 3 层更容易解释。JSF 是您的 Web 层,EJB 是您的业务层,JPA 也是 EJB 规范的一部分,但不需要 EJB 容器是您的 ORM 或持久层。
不要太担心单词容器,您很快就会习惯它,并且很少需要担心它。如果您使用的是 Java EE 服务器,那么一切都已为您做好了设置。

托管 bean 是否调用 EJB 上的方法?

是的,如上所述。所有繁重的工作都在这里。然而,将 EJB 与 JSF 一起使用并不是强制性的。您可以使用任何其他框架,例如 Spring,甚至可以编写简单的 pojo,但这是另一个讨论领域。

What is Managedbeans? Is it just just objects that holds the state of
the components?

A JSF Managed bean is like any other Java bean except that if it managed by JSF. In other words it is a bean that is created and destroyed by JSF as needed.

Hortsman Core JSF 2 book states.

The JSF implementation does the following:

  1. Creates and discards beans as needed (hence the term “managed
    beans”)
  2. Reads bean properties when displaying a web page
  3. Sets bean properties when a form is posted

And they can have other methods as well?

Yes they can have as many methods as you may want.However you would (and should) ideally like to have your managed bean as lean as possible.For example it might have a search method but you should not be doing actually search inside this method but this search methods sole purpose should be to delegate the task to the business layer (which may or may not be EJB based) .
I other words no heavy lifting .

Where does the EJBs fit in?

EJB is your Business tier , they have big biceps and do all the heavy lifting. Since EJB3 JPA was introduced and that is also part of EJB. JPA however is the persistence tier. All EJBs except for JPA run in inside an EJB container. All Java EE complaint server provide these .

In a typical 3 tier architecture (these days however it is mostly more than 3 but 3 tiered is easier to explain. JSF is your Web tier , EJBs are your business tier and JPA which is also part of EJB specification but does not need EJB container is your ORM or Persistence tier.
Do not worry about word container too much you will get used to it quickly and rarely you will have to worry about it. If you are using a Java EE server it's all setup for you.

Does the managed beans invoked methods on the EJBs?

Yes as explained above already . All the heavy lifting here. However it is not mandatory to use EJB with JSF. You could use any other framework e.g Spring or could even write simple pojos but thats an other area of discussion.

当爱已成负担 2024-12-13 16:32:33

通过此链接 JSF - 托管 Bean

托管 Bean:

托管 Bean 是向 JSF 注册的常规 Java Bean 类。换句话说,Managed Beans 是由 JSF 框架管理的 java bean。

通过此链接为网页创建和使用支持 Bean

支撑豆:

在 JSF 中,支持 Bean 是 JavaBean,主要用于提供 UI 逻辑并管理应用程序的 Web 层和业务层之间的数据(类似于数据传输对象)。通常,每个 JSF 页面都有一个支持 Bean。支持 bean 包含页面上使用的 UI 组件的逻辑和属性。

注意:

要使支持 bean 在应用程序启动时可用,您需要
将其注册为具有名称和范围的托管 bean

From this link JSF - Managed Beans

Managed Bean :

Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a java bean managed by JSF framework.

From this link Creating and Using a Backing Bean for a Web Page

Backing Bean :

In JSF, backing beans are JavaBeans used mainly to provide UI logic and to manage data between the web tier and the business tier of the application (similar to a data transfer object). Typically you have one backing bean per JSF page. The backing bean contains the logic and properties for the UI components used on the page.

NB:

For a backing bean to be available when the application starts, you
register it as a managed bean with a name and scope

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