Skip to main content

@Model versus @Named versus @ManagedBean

9 replies [Last post]
alan94539
Offline
Joined: 2005-09-06

I am working up my first JSF 2.0 application under GF 3 and ran into this issue that is bugging me. There does not seem to be any definitive answer I can find on this and I want to follow best practices start to finish.

By experimentation and reading examples on the web I have found that I can create managed beans for my application by either using the annotations @java.faces.bean.ManagedBean, or by @javax.inject.Named, or by @javax.enterprise.inject.Model.

The JSF 2.0 books say to use @ManagedBean. The NetBeans template uses @Named. And then when I look into the CDI specification their JSF example uses @Model.

The same applies to using the bean, either @javax.inject.Inject, or JSF's @Resource.

Don't get me wrong I am happy they all seem to work. But what is the RIGHT one to use, i.e. "best practice?" For that matter what is the difference?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Craig Ringer

On 09/11/2010 07:32 PM, glassfish@javadesktop.org wrote:
> Take a look at this post where I explain the difference and when to use each one: http://www.germanescobar.net/2010/04/4-areas-of-possible-confusion-in-je...

That is an excellent and very clearly written summary of the points of
conflict and confusion in the Java EE Specification. Anyone from Oracle
reading should be getting in touch to license that as a official
documentation, *right* *now*.

If only that was in an appendix to the tutorial. I'd save people trying
to learn EE 6 about three days of pain and suffering. The CDI-vs-JSF DI
farce is an awfully confusing thing to deal with when learning Java EE,
and the way the various documentation totally fails to deal with it is
seriously annoying.

It took me quite some time to figure out what the relationship between
the JSF managed bean and scope annotations, and the CDI named and scope
annotations, was. The duplication of @ManagedBean in the annotation
package doesn't help any, nor does CDI's completely different use of the
@EJB annotation.

IMO, as someone who suffered through this recently, there is a fairly
urgent need for a Java EE 6.2 that addresses the worst areas of
confusion in the DI-related parts of the specification (created by the
late addition of CDI) by:

- Deprecating JSF injection in favour of CDI, making CDI a mandatory
dependency for JSF implementations

- Requiring JSF implementations to do DI on @FacesConverter objects and
similar JSF "helper" objects instantiated by the container

- Deprecating javax.annotation.ManagedBean or clearly defining its role
in the CDI-driven container

- Deprecating one of javax.inject.Singleton and
javax.enterprise.inject.ApplicationScoped.

- Deprecating @EJB. Except, because Weld has re-purposed it for
a producer method annotation, it might not be possible to cleanly
deprecate it, so this source of confusion may have to remain.

Right now, the spec is a learner's / beginner's nightmare, and will
cause people to give up on Java EE as a whole as an overly complex mess,
rather than persisting until they can see through it and understand
what's going on, how the history lead us to this point, etc.

WTF was going on with the parallel development of JSR-299 and JSR-330
anyway ... it's like they didn't talk to each other, or the JSF2 guys,
at all. The tools and technology are great, but the way it interacts and
duplicates functionality is a train-wreck.

(BTW, your article does need a note somewhere that CDI may have to be
enabled with beans.xml to get CDI-driven DI using the current-standard
implementation Weld.)

--
Craig Ringer

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

tomknud
Offline
Joined: 2014-05-16

What if you want to use JSF Conversation scope? Can you still stick to the CDI annotation?

alan94539
Offline
Joined: 2005-09-06

> I hope this helps,

This does help. I am using your book BTW. Excellent.

What is very confusing for a noob like me is that you have a number of specifications and projects all evolving on their own tracks and loosely coupled. Nowhere is it explained (at least that I can find) what specifications are dependent on what other specifications, and the roles of each. You just sort of have to be born to it -- watch it in progress.

So I can understand why your book does not mention CDI. At the time it was written I don't think that specification was widely enough implemented. But if you were going to do another edition, would you advise reliance on EE annotations for your JSF 2 backing beans and JPA beans?

From the answers here I gather the "JSF 2 answer" is to use @ManagedBean, and the "GlassFish 3-and-beyond answer" is to use @Model. I am not sure why I would use @Named instead and although I would like to know it is something I can ignore for now.

germanescobar
Offline
Joined: 2005-09-22

Take a look at this post where I explain the difference and when to use each one: http://www.germanescobar.net/2010/04/4-areas-of-possible-confusion-in-je...

alan94539
Offline
Joined: 2005-09-06

That's a good blog post, but I also found on your site http://www.germanescobar.net/2010/03/whats-so-wrong-with-ejbs.html and the comments session. There, your contributors go so far as to say "JSF Managed Beans Are Dead. Don't Use Them."

Is that a supportable position or too extreme?

germanescobar
Offline
Joined: 2005-09-22

> That's a good blog post, but I also found on your
> site
> http://www.germanescobar.net/2010/03/whats-so-wrong-wi
> th-ejbs.html and the comments session. There, your
> contributors go so far as to say "JSF Managed Beans
> Are Dead. Don't Use Them."
>
> Is that a supportable position or too extreme?

Well, the problem is that you can still use JSF alone, without CDI (e.g. in a standalone environment). So, in that case, it makes sense. However, this is not the normal use case, usually you use JSF inside an app server with CDI and other JEE technologies.

Dominik Dorn

afaik, @Model is just a wrapper for (javax.inject.)@Named
@RequestScoped @ManagedBean (->
http://javadoc.glassfish.org/javaee6/apidoc/javax/enterprise/inject/Mode...
)

The community tends to stay away from the JSF @ManagedBean and
@Request/Session/ApplicationScoped and uses the appropriate one from
javax.inject.

One thing where they still stick to jsf's annotations is @ViewScoped as its
still kind a hack to use that one with CDI but I think its working
with the current
Seam Faces beta build.

On Sat, Sep 11, 2010 at 5:19 AM, wrote:
> I am working up my first JSF 2.0 application under GF 3 and ran into this issue that is bugging me.  There does not seem to be any definitive answer I can find on this and I want to follow best practices start to finish.
>
> By experimentation and reading examples on the web I have found that I can create managed beans for my application by either using the annotations @java.faces.bean.ManagedBean, or by @javax.inject.Named, or by @javax.enterprise.inject.Model.
>
> The JSF 2.0 books say to use @ManagedBean.  The NetBeans template uses @Named.  And then when I look into the CDI specification their JSF example uses @Model.
>
> The same applies to using the bean, either @javax.inject.Inject, or JSF's @Resource.
>
> Don't get me wrong I am happy they all seem to work.  But what is the RIGHT one to use, i.e. "best practice?"   For that matter what is the difference?
> [Message sent by forum member 'alan94539']
>
> http://forums.java.net/jive/thread.jspa?messageID=482397
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

--
Dominik Dorn
http://dominikdorn.com
http://twitter.com/domdorn

Tausche Deine Lernunterlagen auf http://www.studyguru.eu !

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

edburns
Offline
Joined: 2004-02-11

First, note that @Model has the @Named annotation on it, so everything you give @Model, you also give @Named.

Secon, if you are on a certified EE container, such as GlassFish Server Open Source Edition, use @Named or @Model, as appropriate.

If you are not fortunate enough to be on an EE container, you can still get the annotation goodness using @javax.faces.bean.ManagedBean.

I hope this helps,

Ed Burns
JSF technical lead
Oracle America, Inc.

zepag
Offline
Joined: 2007-10-05

Is this annotation inheritance only understood for these annotations, or does it allow to create a custom annotation, annotating it with @ManagedBean and still benefit from said goodness? ;)