将 JSF2 管理的 pojo bean 传递到 EJB 或将所需的内容放入传输对象 [英] Passing a JSF2 managed pojo bean into EJB or putting what is required into a transfer object

查看:25
本文介绍了将 JSF2 管理的 pojo bean 传递到 EJB 或将所需的内容放入传输对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在从 JSF 2 调用 EJB 3 Session Beans.但是,我不确定是否应该将 JSF 托管 bean 传递给 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?

假设表单上的任何内容(以及支持 bean)是我通过 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?

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

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.

推荐答案

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

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.

例如

@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);
    }

    // ...
}

用作

<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>

这篇关于将 JSF2 管理的 pojo bean 传递到 EJB 或将所需的内容放入传输对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆