Create FacesMessage within a get-method
Lately I had problems with displaying a FacesMessage. I suspected that it was due to creating it inside a property get-method. As a proof I created a simple demo application.
public class CreateFacesMessageController {
public String newFacesMessage() {
new FacesMessageSupport().error("TEST");
return "success"
}
public String getFacesMessage() {
System.out.println("get-method");
return newFacesMessage();
}
}
The class above contains an action method that creates FacesMessage instance and adds it to the FacesContext through some helper class. A get-method (for pseudo property facesMessage) calls this action method.
A JSP view is using these methods to show the effect described.
The JSP contains a button that triggers the action method, every time it is clicked a faces message is shown.
It also shows the property facesMessage which is bound to the method getFacesMessage.
So one would think that every time the view is show, it also produces a faces message on top of the form – it doesn’t. Here is why:
- getFacesMessage gets called (typically) during the phase RENDER_RESPONSE
- The messages tag is at the very top of the form whereas the outputText element that is bound to the facesMessage property is somewhere below
As a result the messages have alread been rendered when the new faces message is created. You only have 2 choices to remedy. Either you move the messages tag to the very bottom of the form or you make sure that any method that is possibly creating a faces message is invoked in an earlier phase than the RENDER_RESPONSE phase!

1 Trackback