• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I make a bean available to JSF? (Own JSR330 implementation)

 
Ranch Hand
Posts: 47
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am working on an own implemetation of the JSR330 API (for learning purpose). Now I wonder, how do I make @Named annotated beans available to the JSFs?
Of course I do plan to save the instances in an map, like TinyDI (they have maps in the DependecyReposity.java file), but how do I pass them to the Java EE server?

I have found this so far: https://stackoverflow.com/questions/6746149/how-to-register-a-jsf-managed-bean-programmatically
Is this the way to go?

Thanks!
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Naming a bean just associates a default bean name with the class. Default would be to take the simple classname and convert the first letter to lower-case.

For JSF to use a bean, you have to define it to a JSF scope. Each of the J2EE standard scopes (page, request, session, and application) have dictionaries and JSF will construct (if necessary) and store the named bean in the dictionary for the selected scope.

JSF2 added some new scopes, but they're really piggybacked onto the stock J2EE scopes. For example, View Scope is actually Session Scope with the understanding that the bean in question will be automatically removed from the user's HttpSession when JSF navigates to a different View.
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that didn't make sense. A JavaBean carries a name by default, and that's based on the classname. The @Named attribute is merely an override of the default default bean name with a user-defined default bean name.

Explicit instantiation of a class allows the instantiator to override that name.
 
Christian Wansart
Ranch Hand
Posts: 47
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I thought it was necessary to make a bean available in the JSF. So I don't need to do anything in my DI container -- the server will do that?

So I only need to put the @Named annotated objects into the fields that have the same name, right?
 
Tim Holloway
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need to annotate items with "@Named" if you want to override the default name.

JSF taps into the EL processor and has a dictionary that EL uses to resolve JSF objects. I used JSF with Spring, so my faces-config.xml class includes a "bridge" class that allows JSF to find Spring objects by name in EL expressions, effectively merging JSF's factory dictionary with Spring's.
reply
    Bookmark Topic Watch Topic
  • New Topic