File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

@Named vs @ManagedBean

 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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).
 
Alan Smith
Ranch Hand
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17616
39
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 185
Firefox Browser Linux Netbeans IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic