Problem with @ViewAccessScoped using Prettyfaces and Apache Myfaces CODI

Goal

To avoid @ViewAccessScoped beans being destroyed on each request to a new view, even when that bean is referenced in an application using Prettyfaces and Apache Myfaces CODI

Description

There is an issue (still to be fixed) with the integration of Prettyfaces and Apache Myfaces CODI, reported here, and a bit further explained and with a suggestion to fix it here.

This recipe documents the suggestion, proposed by Linkare’s CTO, José Pedro Pereira (in the second link shown before, he is the person with the nickname sunnymoon). That implementation, based on a JSF phase listener, has proved its usefulness in 2013. Today, in 2015, I have to use it in a new project so, it is still useful 🙂

How to

Basically, the problem is related to the order of Prettyfaces and Apache Myfaces CODI. In order to fix it, we implemented a JSF phase listener, whose code follows and that does the trick:

package com.linkare.myprj;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.apache.myfaces.extensions.cdi.core.api.scope.conversation.WindowContext;
import org.apache.myfaces.extensions.cdi.jsf.impl.util.ConversationUtils;

import com.ocpsoft.pretty.PrettyContext;

/**
 *
 * @author José Pedro Pereira
 *
 */
public class PrettyFacesToCODIViewAccessScopedAwarePhaseListener implements PhaseListener {

  private static final long serialVersionUID = 121463399007913590L;

  @Override
  public void afterPhase(PhaseEvent event) {
  }

  @Override
  public void beforePhase(PhaseEvent event) {
    if (PrettyContext.getCurrentInstance() != null && PrettyContext.getCurrentInstance().getCurrentViewId() != null) {
      WindowContext windowContext = ConversationUtils.getWindowContextManager().getCurrentWindowContext();
      ConversationUtils.storeViewIdAsNewViewId(windowContext, PrettyContext.getCurrentInstance().getCurrentViewId());
    }
  }

  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }
}

And finally, register it in the faces-config.xml of your application, similar to:

<?xml version="1.0"?>
<faces-config metadata-complete="false" version="2.2"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd">
  ...
  <lifecycle>
        <phase-listener>com.linkare.myprj.PrettyFacesToCODIViewAccessScopedAwarePhaseListener</phase-listener>
  </lifecycle>
</faces-config>

Explanations

And it’s done. All credits go to José Pedro, once again, to whom I say thanks for this great trick 🙂

Advertisements

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