JSF 2.2: View Scope for CDI

JSF and CDI are a very fine and powerful combination. But until recently there was something missing in this relationship: the view scope. In this post of the series on JSF 2.2 I will quickly show the new JSF 2.2 view scope implementation for CDI.

By default CDI does not have something like the JSF view scope. But as CDI is a highly extensible framework, JSF 2.2 helps out and implements the view scope on its own. For this, JSF provides the annotation javax.faces.view.ViewScoped.

The following listing shows a CDI bean in the new JSF 2.2 view scope.

@javax.inject.Named
@javax.faces.view.ViewScoped
public class PersonBean {
  ...
}

If you already have CDI and JSF 2.2 running in your project, this should work out of the box. There is no special configuration required.

One downside of this solution is, that it provides yet another source of scope annotations (besides CDI annotations in javax.enterprise.context, JSF managed bean annotations in javax.faces.bean and JSR-330 annotations in javax.inject). But it gets the job done.

Pay special attention that you don’t mix up javax.faces.view.ViewScoped with the JSF view scope annotation javax.faces.bean.ViewScoped (who finds the difference).

If you use Apache MyFaces CODI in your project you should have a look at the alternative view access scope. For details regarding CODI and its special scopes visit the CODI JSF module documentation.

The source code for the JSF 2.2 series examples can be found in the JSFlive Github repository jsf22-examples (module jsf22-cdi-view-scope).

Further official details about JSF 2.2 can be found in the JSR 344: JavaServer Faces 2.2.

Advertisements

4 responses to “JSF 2.2: View Scope for CDI

  1. Hi!
    With JSF 2.2 I do’nt have the option to set the view scope on faces-config. If I set the view scope on the bean class, as you showed above, what scope should I set on faces-config? The available options are: request, session, application, none. Thank you!

    • If you define your managed beans with annotations, you don’t have to define them in the faces-config.xml.
      Nevertheless, the view scope should be available in the faces-config.xml. Maybe your IDE just does not give you the option?

    • Additionally, please be aware that a managed bean can either be a CDI managed bean (by using the CDI annotations) or a JSF managed bean (by using JSF annotations or a config in the faces-config.xml). A bean with CDI annotations will always be handled as CDI bean by JSF (unless you specify a custom bean name).

  2. I declare my bean like this, but I have javax.el.PropertyNotFoundException

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s