You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Intro/Motivation

MyFaces CODI provides powerful scopes for JSF applications. The inspiration for most parts came from MyFaces Orchestra. If you already know MyFaces Orchestra you will be able to use the scopes provided by MyFaces CODI easily. Also the migration of existing applications shouldn't be that hard.

For users who aren't familiar with the conversation concepts provided by MyFaces Orchestra:
Orchestra allows to group beans within a (named) conversation. Furthermore, it's possible to use beans with different (conversation-) lifecycles in the same page. (Usually there isn't one big conversation - like the std. CDI conversation which is more like a bit smarter session for a window/tab.) Conversations are bound to a window/tab of a browser. That means you automatically get new conversations, if you open the application in a new tab/window. Conversations are started automatically as soon as a conversation scoped bean is used. A conversation ends based on a timeout (in case of the access scope - a conversation ends as soon as it wasn't used by a page). Furthermore, it's possible to end (or restart) conversations manually. That means via an API call it's possible to end a conversation immediately. The next access leads to a new conversation. If the whole session gets destroyed, also all conversations will be destroyed automatically.

MyFaces CODI provides all these principles of MyFaces Orchestra in combination with typesafe improvements as well as the support of CDI mechanisms like qualifiers.

And why do we need new scopes? CDI already provides conversations. That's right and CDI is a great spec. and the EG did a nice job, however, (compared to the concepts of MyFaces Orchestra) CDI std. conversations are not that nice. So we saw the need to port a lot of the concepts provided by MyFaces Orchestra to CDI.

MyFaces CODI provides the following types of conversations:

  • @ConversationScoped (!= the std. CDI scope)
  • @WindowScoped
  • @ViewAccessScoped

Simple Usage

Conversation Scoped

Usage of @ConversationScoped
@ConversationScoped
public class DemoBean1
{
  public String getWelcomeText()
  {
    return "Hello MyFaces CODI!";
  }
}

That's it!

As soon as you access the bean, the conversation gets started!

Hint

If you would like to use the bean within your JSF pages, you have to add @Named (javax.inject.Named ).

Injecting conversation scoped beans
@Named
@ConversationScoped
public class DemoBean2
{
  @Inject
  private DemoBean1 bean1;

  private String text;

  @PostConstruct
  protected void init()
  {
    text = this.bean1.getWelcomeText();
  }

  /*generated*/
  public String getText()
  {
    return text;
  }
}
Usage of conversation scoped beans in JSF pages
#{demoBean2.text}
//e.g.: <h:outputText value="#{demoBean2.text}"/>

Be Careful

If you are using MyFaces CODI you don't need std. CDI conversations any more. So it might be a good idea to exclude javax.enterprise.context.ConversationScoped and javax.enterprise.context.Conversation via IDE settings. If you are using those classes instead of the CODI classes, you will see different results.

@ConversationScoped means you are using a bean with a timeout (default: 30 minutes). As soon as a bean gets accessed, the bean is touched which leads to a reset of the timeout.

Hint

You can see the default values in: org.apache.myfaces.extensions.cdi.javaee.jsf.api.ConfigParameter

Injecting the current Conversation
@ConversationScoped
public class DemoBean3
{
  @Inject
  private Conversation conversation;

  public void save()
  {
    //your custom logic
    //...

    this.conversation.end();

    //or use:
    //conversation.restart();
    //... if you know that you will need new instances of the same beans e.g. in the next view
    //#restart it's just a possible performance improvement
  }
}

Window Scope

The usage is exactly the same like @ConversationScoped . The only difference is that there is no timeout.
You can compare it to a session per window. However, as we will see later on it's possible to reset the window-context manually.

Usage of @WindowScoped
@WindowScoped
public class DemoBean4
{
}

View-Access Scope

The usage is exactly the same like @ConversationScoped . The only difference is that there is no timeout. If you don't use @ViewAccessScoped beans within a view, they won't be available any longer. That means: They get cleaned up automatically, as soon as you don't use them.

Usage of @ViewAccessScoped
@ViewAccessScoped
public class DemoBean5
{
}
  • No labels