JSF Annotations
|
|
|
JSF Annotations introduction
Two of the most notable changes in JSF 2.0 are the introduction of annotations and the new convention used for navigation. These new features essentially make the faces-config.xml file optional. With JSF 2.0, you can use annotations in managed beans, registering listeners, resource rendering, etc. Now, any annotated POJO can be used as a managed bean .
Navigation in the new release has been completely redefined to make it simpler. With the new navigation model, the developer need not struggle with managing cumbersome XML configuration for navigation. From JSF 2.0, navigations can be implicit or conditional. In implicit navigation, the navigation happens to a view corresponding to the result of an action. In conditional navigation, a pre-condition needs to be met before navigation is enabled.
In this tutorial, we detail the new annotations and navigation features in JSF 2.0 with some code samples and configuration files using Maven
An effort is underway to unify these bean/scope annotations across specifications (eg. JSF , JCDI ) for Java EE 6 . In the meantime, the JSF 2 managed bean annotations are considered an optional part of the JSF 2 specification.
Mojarra and MyFaces already provide implementations of these optional annotations.
Component annotations
As part of the effort to prune down XML configuration bloat, JSF 2 also includes annotations targeted at authors of custom components (and associated objects). These annotations include:
- @ManagedBean
- @FacesComponent
- @FacesRenderer
- @FacesConverter
- @FacesValidator
- @FacesBehavior
Dependency configuration for JSF annotations using Maven
Annotations are part of the javax.faces.bean package. To include the jar in your project include the following snippet in the <dependencies> section of your project's web app pom.xml-file:
Annotations in Managed Beans
Managed beans are identified using the @ManagedBean annotation. The scope of the bean is also specified using annotations (@RequestScoped, @SessionScoped, @ApplicationScoped).
Let's take as an example a user backing bean providing user and state information during the session�scope .
The name for the managed bean is automatically derived from the name of the annotated class. For the above example, the presence of @ManagedBean annotation on the UserBB class makes a managed bean with the name �userBB� available by default.
Alternatively, the @ManagedBean annotation also allows a name to be specified explicitly .
Managed bean scopes
| Scope | Annotation | Description |
|---|---|---|
| None |
|
Objects with this scope are not visible in any JSF page. When used in the configuration file, they indicate managed beans that are used by other managed beans in the application. Objects with none scope can use other objects with none scope. |
|
Request scope |
Objects with this scope are visible from the start of the request until the end of the request. Request scope starts at the beginning of a request and ends when the response has been sent to the client. If the request is forwarded, the objects are visible in the forwarded page, because that page is still part of the same request/response cycle. Objects with request scope can use other objects with none, request, session, or application scope |
|
|
Session |
An object with session scope is visible for any request/response cycle that belongs to a session. Objects with this scope have their state persisted between
Session bean instance is reused if :
|
|
| Application |
An object with application scope is visible in all request/response cycles for all clients using the application, for as long as the application is active. Objects with application scope can use other objects with none or application scope. |
|
| Custom |
@CustomScoped |
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 |
JSF Tips and Tricks
Annotated bean in dependency jars
If you want to use in your jsf application a managed bean defined in a a dependent jar. You should add a faces-config.xml file compliant with JSF 2.0 in your /META-INF/ folder. According to the JSF specifications :
Section 10.4.2
At application startup time, before any requests are processed, the JSF implementation must process zero or more application configuration resources,
located according to the following algorithm:
-
-
Search for all resources named �META-INF/faces-config.xml� in the ServletContext resource paths for this web application, and load each as a JSF
configuration resource (in reverse order of the order in which they are returned by getResources() on the current Thread�s ContextClassLoader). -
Check for the existence of a context initialization parameter named javax.faces.CONFIG_FILES. If it exists, treat it as a comma-delimited list of
context relative resource paths (starting with a �/�), and load each of the specfied resources. - Check for the existence of a web application configuration resource named �/WEB-INF/faces-config.xml�, and load it if the resource exists.
-
Search for all resources named �META-INF/faces-config.xml� in the ServletContext resource paths for this web application, and load each as a JSF
Include the following snippet in the <build><plugins> section of your project's pom.xml-file:
Related articles
- JSR 303 Bean validations with annotations
- JSF 2.0 Configuration parameters
- JSF and Ajax
- JSF Project Stage
- JSF Lifecycle
If you have any remark or questions feel free to put a comment.If you enjoyed this tutorial and want to promote it don't hesitate to click on
Comments
RSS feed for comments to this post