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 request that get invoked before/after given phase ID


[OmniFaces utilities] The subscribeToRequestBeforePhase()/subscribeToRequestAfterPhase() methods subscribe the given callback (void without arguments/void with one argument) instance to the current request that get invoked before/after given phase ID.

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

Usage:

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


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 custom input component that uses these OmniFaces utilities to extract the submitted value (before Process Validations phase) and local value (after Process Validations phase) and use them in encodeEnd() method (in Render Response phase). For seeing the "difference" between submitted value and local value, we are using a simple custom converter that converts the user input to upper case (MyConverter):

JSF Page:

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

MyInput

import org.omnifaces.util.Callback;
import static org.omnifaces.util.Events.subscribeToRequestAfterPhase;
import static org.omnifaces.util.Events.subscribeToRequestBeforePhase;
import org.omnifaces.util.Faces;
...
@FacesComponent(value = MyInput.COMPONENT_TYPE, createTag = true)
public class MyInput extends UIInput {

 @SuppressWarnings("FieldNameHidesFieldInSuperclass")
 public static final String COMPONENT_TYPE = "jsf.my.input.MyInput";

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

  public MyInput() {      
       
   // void Callback no argument
   subscribeToRequestBeforePhase(PhaseId.PROCESS_VALIDATIONS, new Callback.Void() {
    @Override
    public void invoke() {
     System.out.println("BEFORE PROCESS_VALIDATIONS PHASE");
     submittedValue = (String)getSubmittedValue();
    }
   }); 
       
   /*
   // void Callback with argument
   subscribeToRequestBeforePhase(PhaseId.PROCESS_VALIDATIONS, new Callback.WithArgument<PhaseEvent>() {
    @Override
    public void invoke(PhaseEvent event) {               
     //do something ...
    }
   });
   */
       
   // void Callback no argument
   subscribeToRequestAfterPhase(PhaseId.PROCESS_VALIDATIONS, new Callback.Void() {
    @Override
    public void invoke() {
     System.out.println("AFTER PROCESS_VALIDATIONS PHASE");
     localValue = (String)getLocalValue();
    }
   });         
      
   /*
   // void Callback with argument
   subscribeToRequestAfterPhase(PhaseId.PROCESS_VALIDATIONS, new Callback.WithArgument<PhaseEvent>() {
    @Override
    public void invoke(PhaseEvent event) {               
     //do something ...
    }
   });
   */       
  }  

  @Override
  public void encodeEnd(FacesContext context) throws IOException {
   System.out.println("CURRENT PHASE = " + Faces.getContext().getCurrentPhaseId());
   System.out.println("SUBMITTED VALUE = " + submittedValue+ " | " + getSubmittedValue());
   System.out.println("LOCAL VALUE = " + localValue+ " | " + getLocalValue());
   super.encodeEnd(context);
 }
}

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

BEFORE PROCESS_VALIDATIONS PHASE
AFTER PROCESS_VALIDATIONS PHASE
Saving: RAFAEL NADAL
CURRENT PHASE = RENDER_RESPONSE 6
SUBMITTED VALUE = Rafael Nadal | null
LOCAL VALUE = RAFAEL NADAL | null

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