1

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.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215

2 Answers2

7

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>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Hi BalusC, thanks, i tried an implementation similar to this after reading one of your replies in other threads. Just curious, wouldn't Product also need to be annotated as a Managed bean, i think in my previous attempt i hit some exception because i did not annotate Product as Managed bean. – Oh Chin Boon Feb 13 '12 at 08:33
  • 1
    No, definitely not. That problem must be caused by something else. Perhaps you attempted to use `#{product}` elsewhere while it's not available in the scope at all. – BalusC Feb 13 '12 at 11:43
  • @BalusC so it means we should normally have three classes: 1. The view (Product), 2. The service, which will handle the DAO and 3. the controller which will be the one accessible by the XHTML. For every view, I should have a service and a controller, right? – Erick Apr 14 '15 at 18:02
  • 1
    @Erick: The service is not tied to the view. It's tied to the model. In other words, you can just reuse models and services on other views (and controllers). – BalusC Apr 14 '15 at 18:23
  • Yes sorry. I meant to say that for every model, I should have a service and a controller – Erick Apr 14 '15 at 18:25
  • @Erick I believe he meant that for every model you should have a service, period. The "controllers" (ie, the backing beans) are tied to the XHMTL views, and can import as many services as they need. A Sales view dealing with both Products and Clients will possibly call all 3 services. – MestreLion Jul 01 '20 at 09:55
0

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

Disper
  • 725
  • 12
  • 28