托管 Bean 和会话 Bean 之间的区别

发布于 2024-11-26 05:41:32 字数 653 浏览 0 评论 0原文

假设我有一个实体类 Car。

@Entity
public class Car

我的 IDE 允许我从实体类自动生成会话 bean,因此我最终得到了 CarFacade

@Stateless
public class CarFacade

我还可以生成 JSF 托管 bean

@ManagedBean     
@RequestScoped
public class RegistrationController

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

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

这是正确的吗?

Say I have an Entity class, Car. 

@Entity
public class Car

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

@Stateless
public class CarFacade

I can also generate 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.

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.

Is this correct?

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

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

发布评论

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

评论(1

掩耳倾听 2024-12-03 05:41:32

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

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

但对于如何使用和传递模型,您并不完全正确。通常,您将模型设置为托管 bean 的属性,以便您可以将其绑定到表单的输入元素,最后将其原封不动地传递到业务服务。

例如

<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}" />

private Car car;
private @EJB CarFacade carFacade;

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

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

// ...

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

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.

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.

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";
}

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