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: