In a jsf bean which implements PhaseListener interface, the beforePhase and afterPhase methods will be invoked only before and after the phase that getPhaseId method indicates. This mechanism only enables us to select only one phase which these two methods will be called before and after.
Is there any way to tell JSF to call these two methods (beforePhase and afterPhase) on a subset (not just one) of lifecycle methods (
- Restore view
- Apply request values
- Process validations
- Update model values
- Invoke application
- Render response phase
)
public class HelloBean implements PhaseListener {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void afterPhase(PhaseEvent phaseEvent) {
System.out.println("after phase "+phaseEvent.getPhaseId());
}
@Override
public void beforePhase(PhaseEvent phaseEvent) {
System.out.println("beforePhase "+phaseEvent.getPhaseId());
}
public void testMethod(){
System.out.println("Test Method");
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}
Unfortunately this is not possible. As a generic work around you can implement a custom extended PhaseListener like this:
And use it like this:
Note that the PhaseListener methods are called before and after any phase but the logic is only processed before and after the desired phases.
Furthermore, note that the implementation of the MultiplePhasesListener interface uses default methods which are part of Java 8. If you use a lower Java version you could use an abstract class instead of an interface and extend your PhaseListener implementation of that abstract class. Disadvantage is that you can't extend from another class anymore.