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

问题描述

当前,我正在从JSF 2调用EJB 3会话Bean.但是,我不确定是否应该将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?

尽管POJO的后备bean带有JSF生命周期标记(例如@ManagedBean),并带有大量注释,并位于Web project中,而EJB分别位于EJB project中.

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或将所需的内容放入传输对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-05 05:38