RichFaces - Error Handling


In this chapter, we will learn about different error handling methods that can be implemented in RichFaces.

Server Side & Client Side Error Handling

We need to go through the pretty old Java technique (try/Catch) to handle the action class based exceptions. For client side, we can add one extra file, which will show the error message whenever an error has occurred on the client side.

Following code snippet can be added in web.xml in order to handle errors on the client side.


Note, the above exception will provide only static exception messages and we might have to use JSF “ExceptionHandler” class in order to use dynamic exception property. At runtime, RichFaces provides some features to validate the input fields, which can be used as a primary building block of the exception in the application.

Create a new file and place the following code in it.

<?xml version = "1.0" encoding = "UTF-8"?>  
<!DOCTYPE html> 
<html xmlns = ""   
   xmlns:h = ""   
   xmlns:f = ""   
   xmlns:ui = ""   
   xmlns:a4j = ""   
   xmlns:rich = ""> 
      <title>Error handling</title> 
      <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"/>
      <h:form id = "form"> 
            <f:facet name = "header"> 
                  <h:outputText value = "Student Registration" /> 
                     <f:facet name = "start"> 
                        <h:graphicImage value = "/images/ai.gif" style = "height:12px;width:12px;" alt = "ai" /> 
            <h:panelGrid columns = "3"> 
               <h:outputText value = "Name:" /> 
               <h:inputText value = "#{}" id = "name" label = "name"> 
                  <f:validateLength minimum = "3" maximum = "8" /> 
                  <f:validateRequired /> 
                  <rich:validator /> 
               <rich:message for = "name" /> 
               <h:outputText value = "Email" /> 
               <h:inputText value = "#{}" id = "email" 
                  validatorMessage = "Ivalid email address"> 
                     pattern = 
                     @([a-zA-Z0-9_\-\.]+)\.([a-zAZ]{2,5}){1,25})+)*$" /> 
                  <rich:validator /> 
               <rich:message for = "email" /> 
               <h:outputText value = "Age" /> 
               <h:inputText value = "#{student.age}" id = "age" label = "age"> 
                  <f:validateLongRange minimum = "18" maximum = "99" /> 
                  <rich:validator /> 
               <rich:message for = "age" /> 

Corresponding java class should be a normal bean class like the following.

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.RequestScoped;  


public class Student { 
   private String name; 
   private String email; 
   private int age;  
   public String getName() { 
      return name; 
   public void setName(String name) { = name; 
   public String getEmail() { 
      return email; 
   public void setEmail(String email) { = email; 
   public int getAge() { 
      return age; 
   public void setAge(int age) { 
      this.age = age; 

The above example will yield the following output in the browser, whenever there will be an error in the <h:form>.

Error Handling

Resource Loading

RichFaces improves standard resource handling procedure in JSF application. This can be implemented either by configuring ResourceServlet or by Resource optimization. To configure ResourceServlet, we need to add the following piece of code in web.xml.

   <servlet-name>Resource Servlet</servlet-name> 

   <servlet-name>Resource Servlet</servlet-name> 

We can also enable the optimization in the JSF application, which will optimize different JavaScript and CSS files. We need to add the following code in order to achieve the optimization in the application.