My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 1 martie 2015

[OmniFaces utilities (2.0)] Subscribe the given callback instance to the current view that get invoked every time before/after given phase ID


[OmniFaces utilities] The subscribeToViewBeforePhase()/subscribeToViewAfterPhase() methods subscribes the given callback (void without arguments/void with one argument) instance to the current view that get invoked every time before/after given phase ID.

Methods:

·         subscribe to view before phase methods

·         subscribe to view after phase methods
Note The phase listener is added to the view. If you need to add a phase listener to the request scope then follow this.

Usage:

Before calling the Events#subscribeToViewBeforePhase(), Events#subscribeToViewAfterPhase() methods, let's have a quick overview of how these methods works. As the post title said, these methods allows to subscribe the given Callback instance to the current view that get invoked every time before/after given phase ID. So, we pass to these method the phase ID and one of the following OmniFaces Callbacks (this becomes the listener):


Basically, all we need to do is to indicate the desired PhaseId with a Callback instance and implement a behavior to the corresponding invoke() method. Behind the scene, OmniFaces takes the provided Callback and creates a custom phase listener for it. When the specified phase occurs (before or after it), OmniFaces will call our invoke() implementation. Knowing this, we can create a simple example. Below, we have a simple bean that uses these OmniFaces utilities to extract, in a @PostConstruct method, the submitted value (before Process Validations phase) and local value (after Process Validations phase), and use them in a method, named save() (in Invoke Application phase). For seeing the "difference" between submitted value and local value, we are using a simple custom converter that converts the input to upper case (MyConverter):

JSF Page:

<h:form id="formId">
 Name: <h:inputText id="nameId" value="#{playerBean.name}" converter="myConverter" />
 <h:commandButton value="Save" action="#{playerBean.save()}"/>
</h:form>

PlayerBean

import org.omnifaces.util.Callback;
import static org.omnifaces.util.Events.subscribeToViewAfterPhase;
import static org.omnifaces.util.Events.subscribeToViewBeforePhase;
import org.omnifaces.util.Faces;
...
@Named
@SessionScoped
public class PlayerBean implements Serializable {

 private String name;
 private String submittedValue = "";
 private String localValue = "";

 @PostConstruct
 public void initPlayerBean() {
  // void Callback no argument
  subscribeToViewBeforePhase(PhaseId.PROCESS_VALIDATIONS, new Callback.Void() {
   @Override
   public void invoke() {
    System.out.println("BEFORE PROCESS_VALIDATIONS PHASE");
    UIInput uiInput = (UIInput) Faces.getViewRoot().findComponent("formId:nameId");
    submittedValue = (String) uiInput.getSubmittedValue();
   }
  });

  /*
  // void Callback with argument
  subscribeToViewBeforePhase(PhaseId.PROCESS_VALIDATIONS, new Callback.WithArgument<PhaseEvent>() {
   @Override
   public void invoke(PhaseEvent event) {                
    //do something ...
   }
  });
  */
 
  // void Callback no argument
  subscribeToViewAfterPhase(PhaseId.PROCESS_VALIDATIONS, new Callback.Void() {
   @Override
   public void invoke() {
    System.out.println("AFTER PROCESS_VALIDATIONS PHASE");
    UIInput uiInput = (UIInput) Faces.getViewRoot().findComponent("formId:nameId");
    localValue = (String) uiInput.getLocalValue();
   }
  });

  /*
  // void Callback with argument
  subscribeToViewAfterPhase(PhaseId.PROCESS_VALIDATIONS, new Callback.WithArgument<PhaseEvent>() {
   @Override
   public void invoke(PhaseEvent event) {               
    //do something ...
   }
  });
  */
 }

  public String getName() {
   return name;
  }

  public void setName(String name) {
   this.name = name;
  }

  public void save() {
   System.out.println("CURRENT PHASE = " + Faces.getContext().getCurrentPhaseId());
   System.out.println("SUBMITTED VALUE = " + submittedValue);
   System.out.println("LOCAL VALUE = " + localValue);
   System.out.println("SAVING = " + name);
  }
}

If the user provide the value Rafael Nadal, then you will see the following output:

BEFORE PROCESS_VALIDATIONS PHASE
AFTER PROCESS_VALIDATIONS PHASE
CURRENT PHASE = INVOKE_APPLICATION 5
SUBMITTED VALUE = rafael nadal
LOCAL VALUE = RAFAEL NADAL
SAVING = RAFAEL NADAL

Complete code on GitHub

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors