Custom Managed Bean Scopes

The JSF 2.0 specification has added custom scopes for managed beans.  I think this is something that will be quite useful to extension developers (think homegrown conversation scopes). 

In previous versions of JSF, managed bean scopes were restricted to application, session, request, and none.  JSF 2.0 adds one new custom scope called 'view' which I've touched on in previous blog entries.  But how does support for non-standard scopes work?

It's pretty simple actually.  The custom scope can be referenced in the managed-bean-scope element of the faces-config.xml or via the @CustomScoped annotation.  The value for this scope, in either the xml or annotation case, must be an EL expression that resolves to a Map.

For example, if you reference this custom scope using the 'managed-bean-scope' in xml, it could look something like:

<managed-bean>
   <managed-bean-name>bean</managed-bean-name>
   <managed-bean-class>somepackage.Bean</managed-bean-class>
   <managed-bean-scope>#{customScope}</managed-bean-scope>
</managed-bean>


If you're into using the annotations, then the @CustomScoped annotation might look like:

@CustomScoped(value="#{customScope}")

The managed bean facility in 2.0 will use the expression to resolve the Map that backs the custom scope.  If the Map doesn't already contain the bean, the managed bean facility will create the bean and push it to the custom scope for you.

This new functionality is available as of last night's build of Mojarra 2.0.0.

I've also written a simple example that shows how to write an ELResolver to resolve/manage a custom scope.

UPDATE (April 15, 2009 1:50 PM-ish)

The example has been updated to show how to use the PostConstructCustomScopeEvent and PreDestroyCustomScopeEvent system events.  PreDestroyCustomScopeEvent is particularly import for custom scope authors to publish if they want managed bean methods annotated with @PreDestroy to be invoked.

Comments:

Just because you wrote about "managed beans": If JSR 299 gets approved someday, do you think there will be a chance for JSF 2.x adopting bean and context (=scope) management done through JSR 299 or is this dependency to "heavy" for this purpose?

Posted by Sven Linstaedt on April 22, 2009 at 12:43 AM PDT #

Hi,

How can I use the nightly build jar files for JSF2 in Glassfish v3.
With the update tool I can only install v2.0.0.9.
Replacing the jsf-api.jar/jsf-impl.jar's inside <gf3>/modules and <gf3>/modules/web with the ones downloaded from https://javaserverfaces.dev.java.net/servlets/ProjectDocumentList?folderID=9814&expandFolder=9814&folderID=1703 give me compile errors in Eclipse 3.4 although the jars are listed in my GlassFish v3 Prelude Library path.

Any ideas?

Posted by Wim Bervoets on April 26, 2009 at 09:43 PM PDT #

For V3/V3 Prelude replacing the existing JARs with the newer versions is all you need to do.

Posted by Ryan Lubke on April 27, 2009 at 02:59 AM PDT #

Hi Ryan,

I found out I was having issues because some package names were changed and my imports weren't correct anymore.

Wim

Posted by Wim Bervoets (javablog.be) on April 27, 2009 at 07:32 PM PDT #

At runtime I'm now receiving the error "ClassNotFoundException: javax.validation.MessageInterpolator". I didn't have this with Mojarra 2.0.0.9.

ClassNotFoundException: javax.validation.MessageInterpolator
at org.apache.felix.framework.searchpolicy.R4SearchPolicyCore.findClassOrResource(R4SearchPolicyCore.java:486)
at org.apache.felix.framework.searchpolicy.R4SearchPolicyCore.findClass(R4SearchPolicyCore.java:185)
at org.apache.felix.framework.searchpolicy.R4SearchPolicy.findClass(R4SearchPolicy.java:45)
at org.apache.felix.framework.searchpolicy.ContentClassLoader.loadClass(ContentClassLoader.java:109)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2389)
at java.lang.Class.getConstructor0(Class.java:2699)
at java.lang.Class.newInstance0(Class.java:326)
at java.lang.Class.newInstance(Class.java:308)
at com.sun.faces.application.ApplicationImpl.newThing(ApplicationImpl.java:1593)
at com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:1452)
at javax.faces.component.UIInput.addDefaultValidators(UIInput.java:1607)

I do have an @Entity class where I've added some javax.validation.constraints. Is there already integration possible between Mojarra 2.x nightly and Hibernate Validator 4.0 beta 1?

Thanks,
Wim

Posted by Wim Bervoets (javablog.be) on April 27, 2009 at 08:59 PM PDT #

Post a Comment:
Comments are closed for this entry.
About

user12615560

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today