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