Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
744 views
in Technique[技术] by (71.8m points)

jsf 2 - Passing a JSF2 managed pojo bean into EJB or putting what is required into a transfer object

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...