File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes Why @ManagedBean works while @Named doesn't? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Why @ManagedBean works while @Named doesn Watch "Why @ManagedBean works while @Named doesn New topic

Why @ManagedBean works while @Named doesn't?

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I am reading the book Core Java Server Faces 3rd Edition.
There is a sample of JSF ajax feature and I tried it with NetBeans9.1 and GlassFish V3.
However only when I use @ManagedBean, it works well. If I use @Named annotation, it seems that the @SessionScoped annotation doesn't work.
Because I saw that the default constructor of User bean was called four times when I click the Login button and the getGreeting() cannot catch the filed "name"'s value.

Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
When I use @ManagedBean everything is ok:

When I use @Named instead, it is not ok anymore:

too many times the constructor method was called in each request.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 199
I might be on the wrong track here but here goes

Perhaps you use the same @SessionScoped annotation on both cases? Does the example you use do the same thing?

I have understood that if you use the @Named annotation (and are using the J2EE CDI (Contexts and Dependency Injection)) you should use the annotation used with enterprise beans i.e.

and if you use the @ManagedBean annotation (that is a JSF thing and not a "J2EE wide annotation") you use the JSF annotation meant for JSF beans i.e.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

@ManagedBean is a JSF annotation and it wires itself into the faces-config. @Named is not. Although I believe that @Named can be used to set the management name of the bean instance, it doesn't inherently wire in to the JSF bean management subsystem.

An IDE is no substitute for an Intelligent Developer.
Carlos Jorge Tavares Ferreira

Joined: Dec 10, 2007
Posts: 17
I had the same issue and the @Named annotation started working correctly after I've included an empty beans.xml file under WEB-INF. Now I just use the @Named annotation and don't use @ManagedBean no longer.
Mellon Sun
Ranch Hand

Joined: Feb 20, 2003
Posts: 126
I use NetBeans IDE, so a beans.xml was automatically included in WEB-INF directory. I guess this is not the point.
I tried to replace the import declaration of SessionScope from javax.faces.bean.SessionScoped to javax.enterprise.context.SessionScoped and it worked.

Thanks for your help.
I agree. Here's the link:
subject: Why @ManagedBean works while @Named doesn't?
It's not a secret anymore!