将 JSF2 管理的 pojo bean 传递到 EJB 中或将所需内容放入传输对象中

发布于 2025-01-05 02:08:01 字数 337 浏览 7 评论 0原文

目前我正在从 JSF 2 调用 EJB 3 会话 Bean。但是,我不确定是否应该将 JSF 托管 bean 传递到 EJB 中?

假设表单上的任何内容(以及支持 bean)都是我通过 EJB 层持久保存所需的一切,我是否应该手动将所有属性克隆到传输对象中,或者是否有更好的方法来执行此操作?

POJO 的支持 bean 大量使用 JSF 生命周期标记(例如 @ManagedBean)进行注释,并且驻留在 Web 项目 中,而 EJB 则单独驻留在 EJB 项目 中。

Currently i am calling EJB 3 Session Beans from JSF 2. However, i am not sure if i should be passing JSF managed beans into EJB?

Assuming that whatever on the form (and thus the backing bean) was everything i needed to persist through the EJB layer, should i clone out all the attributes by hand into a transfer object, or is there a better way of doing this?

The backing bean though POJO is heavily annotated with JSF lifecycle tags (Such as @ManagedBean) and resides in the Web project while the EJBs reside separately in the EJB project.

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

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

发布评论

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

评论(2

雨后彩虹 2025-01-12 02:08:01

听起来好像您已将模型与控制器紧密耦合,如大多数基本 JSF 教程中所示。您应该将模型从控制器中解耦到它自己的类中。当您使用 EJB 时,您很有可能也在使用 JPA(否则 EJB 对持久性如何真正有用?),您可以只使用现有的 JPA @Entity 类作为模型。

例如,

@Entity
public class Product {

    @Id
    private Long id;
    private String name;
    private String description;
    private Category category;

    // ...
}

@ManagedBean
@ViewScoped
public class ProductController {

    private Product product;

    @EJB
    private ProductService service;

    public void save() {
        service.save(product);
    }

    // ...
}

which 一起用作

<h:form>
    <h:inputText value="#{productController.product.name}" />
    <h:inputTextarea value="#{productController.product.description}" />
    <h:selectOneMenu value="#{productController.product.category}">
        <f:selectItems value="#{applicationData.categories}" />
    </h:selectOneMenu>
    <h:commandButton value="Save" action="#{productController.save}" />
</h:form>

It sounds like as if you've tight-coupled the model with the controller like as shown in most basic JSF tutorials. You should decouple the model from the controller into its own class. As you're using EJBs, the chance is big that you're also using JPA (how else would EJBs be really useful for persistence?), you can just use the existing JPA @Entity class as model.

E.g.

@Entity
public class Product {

    @Id
    private Long id;
    private String name;
    private String description;
    private Category category;

    // ...
}

with

@ManagedBean
@ViewScoped
public class ProductController {

    private Product product;

    @EJB
    private ProductService service;

    public void save() {
        service.save(product);
    }

    // ...
}

which is to be used as

<h:form>
    <h:inputText value="#{productController.product.name}" />
    <h:inputTextarea value="#{productController.product.description}" />
    <h:selectOneMenu value="#{productController.product.category}">
        <f:selectItems value="#{applicationData.categories}" />
    </h:selectOneMenu>
    <h:commandButton value="Save" action="#{productController.save}" />
</h:form>
晨光如昨 2025-01-12 02:08:01

我试图对 CDI 执行相同的操作,主要区别(不包括使用 @Named 而不是 @ManagedBean)是我必须在 Controller 类中初始化我的传输对象。

而不是

private Product Product;

因此,我必须使用

private Product Product = new Product();

也许它会对某人有所帮助:)

I was trying to do the same with CDI and the main diffrence (excluding using @Named instead of @ManagedBean) was that I had to initialize my transport object in the Controller class.

So instead of:

private Product product;

I had to use:

private Product product = new Product();

Maybe it will help someone :)

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