Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Christian Wansart
Ranch Hand
Posts: 32
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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!
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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: 32
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!