托管 Bean 和支持 Bean 之间的区别

发布于 2024-10-12 16:39:46 字数 95 浏览 4 评论 0原文

我在几个论坛中遇到了“托管 bean”和“支持 bean”这两个术语。很多人认为两者是一样的。但是,似乎还是有一些细微的差别。任何人都可以帮助我理解这两个术语之间的确切区别吗?

I came across the terms "managed bean" and "backing bean" in several forums. Many people think both are the same. But, there seems to be a slight difference. Can any one help me to understand the exact difference between these two terms?

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

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

发布评论

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

评论(11

酒与心事 2024-10-19 16:39:46

改变我最初的答案——两者之间没有任何有意义的区别。 教程 说支持 bean 随后被声明为托管 bean。因此,总而言之:

  • 支持 bean 是脱离上下文的类;
  • 只要声明托管 bean 与 JSF 托管 bean 工具一起使用,托管 bean 就是支持 bean。

我从未真正使用过“backing bean”这个术语,因为我发现它没有用处。所以你最好只使用“托管bean”。请注意,在 JSF 2.0(和 CDI)中,您有 @ManagedBean - 因此您的 bean 是托管 bean。

BalusC 建议“支持 bean”是定义,“托管 bean”是实例。虽然这可能是 JSF 创建者的最初想法,但我认为不值得支持它。例如,CDI 和 spring 对于“bean 定义”和“bean 实例”没有不同的术语。

JSF 2.0 规范只提到了术语“backing bean”几次,没有任何定义。除此之外,它还提到“backing bean class”,这可能意味着“backing bean”!=“backing bean class”,这会带来进一步的混乱。

所以总而言之 - 对我来说两者是可以互换的,并且我坚持只使用“托管 bean”

Changing my initial answer - there is no meaningful difference between the two. The tutorial says that backing beans are later declared as managed beans. So, to summarize:

  • a backing bean is the class out of context
  • a managed bean is the backing bean whenever it is declared to be used with the JSF managed bean facility.

I've never actually used the term "backing bean", because I found no use to it. So you might be better off using only "managed bean". Note that in JSF 2.0 (and in CDI) you have @ManagedBean- so your bean is a managed bean.

BalusC suggested that "backing bean" is the definition, and "managed bean" is the instance. While this might have been the original idea of JSF creators, I don't think it is worth supporting it. CDI and spring for example don't have different term for "bean definition" and "bean instance".

The JSF 2.0 specification mentions the term "backing bean" only a few times, with no definition whatsoever. In addition to that it mentions "backing bean class", which might mean that "backing bean" != "backing bean class", which brings further confusion.

So to conclude - for me both are interchangeable, and I'd stick to only using "managed bean"

地狱即天堂 2024-10-19 16:39:46

什么是托管 Bean?

由 JSF 实现管理的 JavaBean 对象称为托管 Bean。
托管 Bean 描述了如何创建和管理 Bean。
它与 bean 的功能无关。

什么是 Backing Bean?

Backing beans 是与页面中使用的 UI 组件相关联的 JavaBeans 组件。
支持 bean 管理将 UI 组件对象的定义与执行特定于应用程序的处理和保存数据的对象分开。
支持 bean 定义与页面上使用的 UI 组件关联的属性和处理逻辑。
每个 backing-bean 属性都绑定到组件实例或其值。
支持 bean 还定义了一组为组件执行功能的方法,例如验证组件的数据、处理组件触发的事件以及在组件激活时执行与导航相关的处理。

支持 Bean 和托管 Bean 之间有什么区别?

支持 Bean 只是一种约定,是 JSF 托管 Bean 的子类型,具有非常特殊的用途。除了用法之外,支持 Bean 与任何其他托管 Bean 没有什么特别之处。

MB:托管 Bean; BB:支持 Bean

1) BB:支持 Bean 是表单引用的任何 Bean。

MB:托管 bean 是已向 JSF 注册的支持 bean(在 faces-config.xml 中),并在需要时由 JSF 自动创建(并可选择初始化)。

托管 bean 的优点是 JSF 框架将自动创建这些 bean,并可以选择使用您在 faces-config.xml 中指定的参数来初始化它们。

2) BB:Backing Beans 应该仅在请求范围中定义

MB:由 JSF 创建的托管 Bean 可以存储在请求、会话或应用程序范围

支持 Bean 应该在请求范围内定义,与特定页面以一对一的关系存在,并保存所有页面特定的事件处理代码。
在现实场景中,多个页面可能需要在幕后共享相同的支持 bean。
支持 bean 不仅包含视图数据,还包含与该数据相关的行为。

What is Managed Bean?

JavaBean objects managed by a JSF implementation are called managed beans.
A managed bean describes how a bean is created and managed.
It has nothing to do with the bean's functionalities.

What is Backing Bean?

Backing beans are JavaBeans components associated with UI components used in a page.
Backing-bean management separates the definition of UI component objects from objects that perform application-specific processing and hold data.
The backing bean defines properties and handling-logics associated with the UI components used on the page.
Each backing-bean property is bound to either a component instance or its value.
Backing bean also defines a set of methods that perform functions for the component, such as validating the component's data, handling events that the component fires and performing processing associated with navigation when the component activates.

What are the differences between a Backing Bean and Managed Bean?

Backing Beans are merely a convention, a subtype of JSF Managed Beans which have a very particular purpose. There is nothing special in a Backing Bean that makes it different from any other managed bean apart from its usage.

MB : Managed Bean ; BB : Backing Bean

1) BB: A backing bean is any bean that is referenced by a form.

MB: A managed bean is a backing bean that has been registered with JSF (in faces-config.xml) and it automatically created (and optionally initialized) by JSF when it is needed.

The advantage of managed beans is that the JSF framework will automatically create these beans, optionally initialize them with parameters you specify in faces-config.xml.

2) BB: Backing Beans should be defined only in the request scope

MB: The managed beans that are created by JSF can be stored within the request, session, or application scopes .

Backing Beans should be defined in the request scope, exist in a one-to-one relationship with a particular page and hold all of the page specific event handling code.
In a real-world scenario, several pages may need to share the same backing bean behind the scenes.
A backing bean not only contains view data, but also behavior related to that data.

第几種人 2024-10-19 16:39:46

支持 Bean 是与 JSF UI 绑定的任何 Bean。而托管 bean 是任何 bean

Backing Bean is any bean that is bound with JSF UI. while Managed bean is any bean

百变从容 2024-10-19 16:39:46

简而言之,

您作为开发人员所做的事情:

@ManagedBean(name="managedBean")
@RequestScoped
public class BackingBean {
    // ...
}

JSF 作为 bean 管理框架在幕后所做的事情:

BackingBean managedBean = new BackingBean();
externalContext.getRequestMap().put("managedBean", managedBean);

因此,支持 bean 是由您开发的具体类,通常与视图绑定,而托管 bean 是具体实例,即由 bean 管理框架按需创建并放入所需范围,并可通过 EL 中的 #{managementBean} 获得。您永远不需要自己创建并将其放入范围内。如果您这样做,那么就无法使用框架管理的 bean。

CDI @Named 和 Spring @Component 本质上与 JSF @ManagedBean 执行相同的操作。

要详细了解 JSF、CDI 和 Spring 等 Bean 管理框架如何查找和创建其托管 Bean,以下疑难解答程序应提供深入的见解:识别并解决 javax.el.PropertyNotFoundException:目标无法到达

另请参阅:

Simply put,

You as developer do:

@ManagedBean(name="managedBean")
@RequestScoped
public class BackingBean {
    // ...
}

JSF as bean management framework does under the covers:

BackingBean managedBean = new BackingBean();
externalContext.getRequestMap().put("managedBean", managedBean);

So, the backing bean is the concrete class which is developed by you and usually tied to the view, and the managed bean is the concrete instance, which is under the covers created and put in the desired scope by the bean management framework on demand, and available by #{managedBean} in EL. You never need to create and put it in the scope yourself. If you did so then there's no means of a framework-managed bean.

CDI @Named and Spring @Component do essentially the same thing as JSF @ManagedBean.

To learn more about how bean management frameworks like JSF, CDI and Spring find and create their managed beans, the following troubleshooter should provide in depth insight: Identifying and solving javax.el.PropertyNotFoundException: Target Unreachable.

See also:

亢潮 2024-10-19 16:39:46

http://docs.oracle.com/javaee/5/tutorial/doc /bnaqm.html

典型的 JavaServer Faces 应用程序包括一个或多个支持 Bean,每个支持 Bean 都是与特定页面中使用的 UI 组件相关联的 JavaServer Faces 托管 Bean。托管 bean 是可以使用托管 bean 工具进行配置的 JavaBeans 组件(请参阅 JavaBeans 组件),这在配置 Bean 中进行了描述。本节介绍在应用程序中创建、配置和使用支持 bean 的基本概念。

http://docs.oracle.com/javaee/6/tutorial/doc /bnaqm.html 没有提及“支持 bean”。

http://docs.oracle.com/javaee/5/tutorial/doc/bnaqm.html says

A typical JavaServer Faces application includes one or more backing beans, each of which is a JavaServer Faces managed bean that is associated with the UI components used in a particular page. Managed beans are JavaBeans components (see JavaBeans Components) that you can configure using the managed bean facility, which is described in Configuring Beans. This section introduces the basic concepts on creating, configuring, and using backing beans in an application.

http://docs.oracle.com/javaee/6/tutorial/doc/bnaqm.html makes no mention of "backing bean".

南汐寒笙箫 2024-10-19 16:39:46

托管 Bean 由容器管理(实例化和销毁)。

支持 Bean 是作为数据模型支持视图的托管 Bean。

Managed Beans are managed (instantiated and destroyed) by container.

Backing Beans are managed beans that back views as data models.

许一世地老天荒 2024-10-19 16:39:46

我想说的是,支持 bean 是严格用于 UI 目的的 bean,即在 jsp 文件中引用的 bean。毕竟,由 JSF 容器管理的所有 Bean 都是托管 Bean,但是它们可能在不同的上下文中使用。有关更多信息,请参阅此处接受的答案: JSF:ManagedBean,好地方处理业务逻辑?

I would say that the backing bean is a bean used strictly for UI purposes, that is, referenced in the jsp files. After all, all beans managed by JSF container are managed beans, however there are different contexts in which they might be used. For more see accepted answer here: JSF : ManagedBean, Good place to deal with Business Logic?

池予 2024-10-19 16:39:46

Backing Bean 是一种托管 Bean。托管Bean是一个对象(即类的实例),由容器创建(这就是它被称为托管的原因),当然该对象有一个类,如果您愿意,您可以创建任意多个该类的实例无论它们使用 m = new MyManagedBean() 进行什么注释,它都将是一个不那么托管的 Bean,或者至少不是由容器管理而是由您管理:)

并且支持 bean 是一种托管 bean(如Santosh 指出:一种约定),通常使用 JSF requestScope(但在某些框架(如 ADF)中,甚至有一个仅用于支持 bean 的指定范围,称为 backingBeanScope - 您绝对猜不到)。

是的...jsf 托管 bean 的风格(称为 BackingBeans)用于绑定 UIComponent 并编写 ActionListener,而我们可以说“模型 bean”风格将进入会话范围,例如并保存您的数据

Backing Bean is a kind of Managed Bean. Managed Bean is an Object (i.e. instance of a Class ), created by a container (that's why it is called managed) and of course that Object has a Class and if you feel like it, you can create as many instances of that class no matter what annotation they have with m = new MyManagedBean(), just it will be a Not-So-Managed-Bean or at least not managed by a container but managed by you :)

And backing bean is a kind of managed bean (as Santosh put it: a convention) that usually uses the JSF requestScope (but in some frameworks like ADF there is even a designated scope only for backing beans called backingBeanScope - you would have never guessed) .

And yes... The flavor of jsf managed beans that one would call BackingBeans is used to bind UIComponents and write ActionListeners, while lets say "model beans" flavor would go in the session scope for example and hold your data

倾其所爱 2024-10-19 16:39:46

我参加了一门非常好的(昂贵的)JSF 课程。我在那里学到的只是 http 中解释的内容://www.avromroyfaderman.com/2008/07/from-backing-bean-to-management-bean/

也许这种差异不是理论上的差异或术语的词源,但它确实是一种非常实用的设置架构的方法,特别是如果您是一个具有多个开发人员的大型项目的一部分和/或需要维护大量的码了好久。基本上,这个想法是将 UI 业务逻辑放入托管 Bean 中。支持 bean 很稀疏,仅支持页面本身。

I took a very good (expensive) course on JSF. What I learned there is just about what is explained in http://www.avromroyfaderman.com/2008/07/from-backing-bean-to-managed-bean/.

Perhaps this difference is not the theoretical difference or the etymology of the terms, but it is certainly a very practical way to set up your architecture, especially if you are part of a large project with multiple developers and/or need to maintain a lot of code for a long time. Basically the idea is that you put your UI Business Logic in Managed Beans. Backing beans are sparse and just support the page itself.

み格子的夏天 2024-10-19 16:39:46

通过此链接 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

渡你暖光 2024-10-19 16:39:46

托管 Bean:

托管 Bean 是已通过 JSF(在 faces-config.xml 中)或使用注释注册的支持 Bean。托管 Bean 在需要时由 JSF 自动创建(并可选择初始化)。

如果您在应用程序中使用托管 Bean,则必须在 JSF 页面中使用以下语法来设置或获取表单 bean 的值

<h:inputText value="#{user.name}"/>

支持 Bean

包含 Web 表单的部分或全部组件对象的 Bean。这样的 bean 称为 Web 表单的支持 bean。

当您使用支持 bean 时,您需要连接上的组件
形成豆上的那些。您可以使用绑定属性来实现此目的。

示例:

<h:inputText binding="#{myForm.myComponent}" .../>

观察我们如何获取或设置支持 bean 的值。

支持 Bean 应该在请求范围内定义,与特定页面以一对一的关系存在,并保存所有页面特定事件处理代码

希望它对某人有用。

Managed Bean:

A managed bean is a backing bean that has been registered with JSF (in faces-config.xml) or using Annotations. Managed Bean automatically created (and optionally initialized) by JSF when it is needed.

If you use Managed Bean in your application you have to use following syntax in JSF page to set or get values form bean

<h:inputText value="#{user.name}"/>

Backing Bean:

A bean that contains some or all component objects of a web form. Such a bean is called a backing bean for the web form.

When you use a backing bean, you need to wire up the components on the
form to those on the bean. You use the binding attribute for this purpose

Example:

<h:inputText binding="#{myForm.myComponent}" .../>

Observe how we are getting or setting values to Backing bean.

Backing Beans should be defined in the request scope, exist in a one-to-one relationship with a particular page and hold all of the page specific event handling code

Hope it useful to someone.

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