java 托管 bean 和会话 bean 之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6793351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:17:41  来源:igfitidea点击:

Difference between a managed bean and a session bean

javajsfjakarta-eeejb

提问by Laurens

Say I have an Entity class, Car. 

假设我有一个实体类 Car。 

@Entity
public class Car

My IDE lets me automatically generate session beans from entity classes, so I end up with a CarFacade

我的 IDE 允许我从实体类自动生成会话 bean,所以我最终得到了一个 CarFacade

@Stateless
public class CarFacade

I can also generate JSF Managed beans

我也可以生成 JSF Managed beans

@ManagedBean     
@RequestScoped
public class RegistrationController

I can understand the meaningful difference between the Entity class and other beans, but what are the differences between a stateless session bean and a managed bean? I read that a stateless session bean is for implementing your business logic that operates on the entities and managed beans are for interacting with the web-based front-end, by having the webpage call methods on the managed bean, and having the managed bean call business methods on the session bean.

我可以理解 Entity 类和其他 bean 之间有意义的区别,但是无状态会话 bean 和托管 bean 之间有什么区别?我读到无状态会话 bean 用于实现在实体上运行的业务逻辑,托管 bean 用于与基于 Web 的前端交互,通过在托管 bean 上使用网页调用方法,并让托管 bean 调用会话 bean 上的业务方法。

So in my example, the RegistrationController would feature a +register(String carRegistration) method that the webpage would call. The RegistrationController would in turn instantiate a Car and call +create(Car car) on the session bean, which would persist it.

因此,在我的示例中, RegistrationController 将具有网页将调用的 +register(String carRegistration) 方法。RegistrationController 将依次实例化 Car 并在会话 bean 上调用 +create(Car car),这将持久化它。

Is this correct?

这个对吗?

回答by BalusC

The JSF managed bean is the glue (controller) between the entity (model), the JSF page (view) and the enterprise bean (business service).

JSF 托管 bean 是实体(模型)、JSF 页面(视图)和企业 bean(业务服务)之间的粘合剂(控制器)。

So, yes, you are basically right in your understanding that the JSF page should invoke the managed bean's action method which should in turn delegate the model and the action further to the business service and eventually handle the navigation outcome based on the result of the service call.

所以,是的,您的理解基本上是正确的,即 JSF 页面应该调用托管 bean 的操作方法,该方法应该将模型和操作进一步委托给业务服务,并最终根据服务的结果处理导航结果称呼。

But you are not entirely right in how the model should be used and passed around. Usually you make the model a property of the managed bean so that you can just bind it to the form's input elements and finally pass it unchanged through to the business service.

但是您对模型的使用和传递方式并不完全正确。通常,您使模型成为托管 bean 的一个属性,以便您可以将它绑定到表单的输入元素,并最终将它原封不动地传递给业务服务。

E.g.

例如

<h:inputText value="#{registrationController.car.make}" />
<h:inputText value="#{registrationController.car.model}" />
<h:inputText value="#{registrationController.car.year}" />
<h:commandButton value="Save" action="#{registrationController.save}" />

with

private Car car;
private @EJB CarFacade carFacade;

public RegistrationController() {
    this.car = new Car();
}

public String save() {
    carFacade.create(car);
    return "someoutcome";
}

// ...