I would like to do some processing in a JSF view-scoped backing bean when the form is first loaded before the Render Response Phase is reached. I cannot do this process in the Render Response Phase because I have a dynamic include that depends on the processing. Is there a event listener or phase listener that I can use for this?
1 Answers
0
votes
You can use programmatic phase listener I am assuming you are using JSF2. Below class will load phase listener in your application.
@ManagedBean(eager = true)
@ApplicationScoped
public class SomeClass implements Serializable {
@PostConstruct
private void initialize() {
LifecycleFactory factory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = factory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
lifecycle.addPhaseListener(new PhaseListenerImpl());
}
}
Below class you can define your logic inside beforePhase method.
public class PhaseListenerImpl implements PhaseListener {
@Override
public void afterPhase(PhaseEvent pe) {
return;
}
@Override
public void beforePhase(PhaseEvent pe) {
FacesContext fc = pe.getFacesContext();
String viewId = fc.getViewRoot().getViewId();
try {
if(viewId==blah){
//your logic goes here for your view
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
@PostConstruct
of the managed bean insufficient? – BalusC@PostConstruct
. – BalusC