Question

I am using an h:messages tag on every page of my JSF2 project to show global messages. I'm also using mostly AJAX to update the page content. As it is right now, I have to include the ID of the h:messages tag in the "render" attribute of every AJAX tag so that it displays any messages that come as a result of that AJAX call. I am wondering, is there some way I can save myself from doing this to every AJAX tag and just tell it to always automatically refresh the messages on any AJAX call?

Was it helpful?

Solution

The easiest way is to go with Primefaces and its <p:messages /> component. Using autoUpdate="true" gives you the chance to have the message component updated everytime.

Alternatively, if not going to use Primefaces and want to avoid having to declare your h:messages id each time, you could use backing side updating. Just implement a JSF PhaseListener, which listens to INVOKE_APPLICATION phase and renders your component after it happens:

public class JsfPhaseListener implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent event) {
        FacesContext.getCurrentInstance().getPartialViewContext()
                .getRenderIds().add("header:messages");
    }

    @Override
    public void beforePhase(PhaseEvent event) {

    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.INVOKE_APPLICATION;
    }

}

This will cause your header:messages component to be rendered on every single request you make.

See also:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top