1

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?

eljstonge
  • 45
  • 2
  • 11

1 Answers1

2

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:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217