Show a JSF global error message on validation failure


Show a global error message to the user anytime the validation fails and there is no global message


Anytime a form is submitted and there are validation errors, it’s nice if the user is presented with a global error message (if no specific message is shown in this regard) that tells her that something failed. In JSF, with a postValidate event, this is easy to be implemented in a more or less crosscutting manner. This recipe will tell you how.

How to

The recipe consists on the following 3 steps:

  • A common backing bean from which all other backing beans extend (I usually use this mechanism to share common behaviour between all my JSF backing beans) containing the check validation failure method (the method addErrorMessage on the JsfUtil class is simply a utility method that creates a FacesContextMessage):
    public abstract class BaseBackingBean {
      public void validateForm(final ComponentSystemEvent event) {
        final FacesContext fc = FacesContext.getCurrentInstance();
        if (fc.isValidationFailed()) {
          final boolean existGlobalMessages = !fc.getMessageList(null).isEmpty();
          if (!existGlobalMessages) {
            JsfUtil.addErrorMessage(null, "Error", "There were validation errors. Please, check the submitted data");
  • Create a backing bean that extends from the previous bean (for instance, myRequestBean)
  • Add a postValidate event inside the submission form (or a global element within it, that wraps around all fields that should be validated on the form submission):
      <f:event listener="#{myRequestBean.validateForm}" type="postValidate" />


No further explanations seem necessary for this recipe.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s