EJB 3.1 or CDI Managed Bean As JSF Backing Bean

Got interesting questions:

- I can use a ejb as a backing bean for Jsf(a session bean with @Named), but, is this useful I mean, have advantages against the @ManagedBean or @Named annotations directly in a class?

Answer: You can directly expose EJB 3.1 to JSF or JSP by annotating the class with @Named (see: http://www.adam-bien.com/roller/abien/entry/ejb_3_1_killed_the). However: in all non-trivial projects your EJBs will contain a significant amount of presentation logic and dependencies to JSF / JAX-RS APIs. Mixing presentation and business logic makes your code more complex, harder to test and therefore harder to maintain.

You can start with an EJB 3.1 exposed with @Named to JSF and on the first occurrence of any dependency to the presentation layer just refactor the presentation logic into a dedicated @RequestScoped @Named bean (I would not use @ManagedBean any more.)

- A bit far, I can annotate an interface of my Ejb with @Named. These approach is cool, but again, is this better?

Answer: It is hard to find a reason to use an interface for an EJB 3.1. I would start with no-interface view EJB 3.1 (a Java class annotated with @Stateless).

- Why not use directly an EJB project with all my Session Beans and only access the beans there? (consider an enterprise application with and ejb module and a web module - actual stage)

Answer: In Java EE 6 I always used WAR projects so far. You will have everything you need in one place. EAR-packaging works as well, but I would still separate the presentation from the business logic.


Adam, could you explain what you mean by "I would not use @ManagedBean any more."? Is JSF @ManagedBean deprecated? When, exactly, that happened, and what are the reasons? Am I missing something?


Posted by Davi on January 07, 2012 at 03:28 AM CET #

a.) there are multiple @ManagedBean annotations in the JavaEE space (eg. javax.faces.bean.ManagedBean, javax.annotation.ManagedBean)

b.) all of them suck ;)

Serious, I would not use any of them. The one from the EE6 umbrella spec is completely ignored in all other specs, and the JSF ManagedBean is only for JSF backing beans. The JSF EG is discussing of getting rid of it at all.

I consider this bad timing - the JSF EG was too quick, the EE EG was too slow :)

Just use EJB3 and CDI Annotations and you're done.


Posted by struberg on January 07, 2012 at 03:55 PM CET #

I have been migrating a seam2 application to Seam3/JEE6(JSF2, CDI, EJB3.1 none interface session) in 2011.

But many problem when I used CDI bean( I like used the Conversation based stateful Session Bean in most case) as JSF backend, especially the JSF2 ajax(paritial page rendering), I have complained many times in Jboss Seam3 forum, but the JBoss guy considered they were the problems from the JSF impl.

Do you have some experience to share with us about this?

Posted by hantsy on January 17, 2012 at 04:48 AM CET #

Adam is there a good idea to use Stateful ejb as jsf backed bean if i want some state preserve during navigation ? I faced a scenario where its required to support multi tab browsing and the Conversation scope beans are not helpful

Posted by Sam on November 12, 2013 at 01:17 AM CET #

Thanks, nice explanation

Posted by Binh Nguyen on June 18, 2015 at 08:37 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments