1
votes

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
How exactly is @PostConstruct of the managed bean insufficient?BalusC
The include will be dynamic based on a get parameter I'm passing into the bean, which is set into the managed bean in the UpdateModelView phase. I'm not sure whether @PostConstruct will be called before this phase.futureelite7
Just make sure param is set and available in @PostConstruct.BalusC
The property is current set automatically via f:viewParam. Do I have to manually extract the request parameter in this case?futureelite7

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;
}