[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
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