wood burning stoves 2.0*
The moose likes JSF and the fly likes @Named vs @ManagedBean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "@Named vs @ManagedBean" Watch "@Named vs @ManagedBean" New topic
Author

@Named vs @ManagedBean

Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Hi,

I have read that the @Named annotation is used to specify CDI and is preferable over the @ManagedBean annotation when creating beans (if someone can clarify this it would be appreciated, I haven't got the book I read this from on hand at the moment to give the exact reasons). For some reason though I can't switch between the two in my project (using netbeans) without an error being generated when I process a form. I started my project with CDI enabled, which inserts the required beans.xml file into my WEB-INF folder. Everything was working with the @Named annotation but when I switch to the @ManagedBean annotation I get an error on clicking my command button. I have restarted the server, redeployed my war file, rebuilt the project but nothing seems to work. Is it a case that you have to pick one way of annotating your beans and stick with it? My code is extremely simple and just for testing purposes:

Person.java (imports, getters, setters excluded)


index.xhtml


This works ok but when I switch the @Named annotation to @ManagedBean I get the following error on clicking the command button...
/index.xhtml @13,76 value="#{person.name}": Target Unreachable, identifier 'person' resolved to null

It works vice versa as well i.e. when I start my project without CDI enabled and no beans.xml file and use the @ManagedBean annotation it works; but if I then add a beans.xml file to WEB-INF and switch to the @Named annotation I get the same error.

Any ideas?

Thanks,
Alan
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

@Named is preferred over @ManagedBean because @Named is a more general designation for a re-usable JavaBean. @ManagedBean is specific to JSF. The net effect is about the same, however.

You do not have to include a "value=" attribute on the annotation. If you do not, a name will automatically be generated by folding the first letter of the bean's classname (Person) to lower-case (person).

XML files override annotations. Annotations are to minimize the amount of XML that has to be maintained separate from the code, but the XML allows you to override the annotation-provided definition without having to make source code changes. That makes the beans more re-usable and also permits having more than one bean of a different class type (with different names).


Customer surveys are for companies who didn't pay proper attention to begin with.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Tim Holloway wrote:@Named is preferred over @ManagedBean because @Named is a more general designation for a re-usable JavaBean. @ManagedBean is specific to JSF. The net effect is about the same, however.

You do not have to include a "value=" attribute on the annotation. If you do not, a name will automatically be generated by folding the first letter of the bean's classname (Person) to lower-case (person).

XML files override annotations. Annotations are to minimize the amount of XML that has to be maintained separate from the code, but the XML allows you to override the annotation-provided definition without having to make source code changes. That makes the beans more re-usable and also permits having more than one bean of a different class type (with different names).


Hi Tim,

thanks for that but how come I can't switch between annotations? Once I create a bean and deploy it can I not change the annotation again? I am just wondering is there somewhere else the annotations are specified where I have to change them apart from on the bean itself. If I switch annotations the bean isn't recognised.

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15962
    
  19

I can't tell. You didn't actually show a sample of your ManagedBean annotation.

I suspect that your XML file is probably overriding something, though.
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Tim Holloway wrote:I can't tell. You didn't actually show a sample of your ManagedBean annotation.

I suspect that your XML file is probably overriding something, though.


I simply switched the annotation from @Named(value="person") to @ManagedBean. No worries, I'll keep having a look.

Thanks.
Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
Hi - I had a similar question on stackoverflow

http://stackoverflow.com/questions/9861144/why-is-my-viewscoped-bean-not-surviving-hcommandbutton

the answer to the question is related to the @Named vs @ManagedBean discussion.

TDR
Alan Smith
Ranch Hand

Joined: Oct 19, 2011
Posts: 152

Tanya Ruttenberg wrote:Hi - I had a similar question on stackoverflow

http://stackoverflow.com/questions/9861144/why-is-my-viewscoped-bean-not-surviving-hcommandbutton

the answer to the question is related to the @Named vs @ManagedBean discussion.

TDR


I managed to figure it out which I posted here. Thanks though. It's usually something simple!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: @Named vs @ManagedBean
 
Similar Threads
JSF 2.0 Login
Problem with navigation in JSF with Filter
Issue with my webApp
Session Scope JSF
Button action problem in richfaces