In this chapter, we will learn about different error handling methods that can be implemented in RichFaces.
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.
<error-page> <exception-type>java.lang.Throwable</exception-type> <location>/error.xhtml</location> </error-page>
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 = "http://www.w3.org/1999/xhtml" xmlns:h = "http://java.sun.com/jsf/html" xmlns:f = "http://java.sun.com/jsf/core" xmlns:ui = "http://java.sun.com/jsf/facelets" xmlns:a4j = "http://richfaces.org/a4j" xmlns:rich = "http://richfaces.org/rich"> <h:head> <title>Error handling</title> <meta name = "viewport" content = "width = device-width, initial-scale = 1.0"/> </h:head> <h:body> <h:form id = "form"> <rich:panel> <f:facet name = "header"> <h:panelGroup> <h:outputText value = "Student Registration" /> <a4j:status> <f:facet name = "start"> <h:graphicImage value = "/images/ai.gif" style = "height:12px;width:12px;" alt = "ai" /> </f:facet> </a4j:status> </h:panelGroup> </f:facet> <h:panelGrid columns = "3"> <h:outputText value = "Name:" /> <h:inputText value = "#{student.name}" id = "name" label = "name"> <f:validateLength minimum = "3" maximum = "8" /> <f:validateRequired /> <rich:validator /> </h:inputText> <rich:message for = "name" /> <h:outputText value = "Email" /> <h:inputText value = "#{student.email}" id = "email" validatorMessage = "Ivalid email address"> <f:validateRegex pattern = "^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+) \.([a-zAZ]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+) @([a-zA-Z0-9_\-\.]+)\.([a-zAZ]{2,5}){1,25})+)*$" /> <rich:validator /> </h:inputText> <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 /> </h:inputText> <rich:message for = "age" /> </h:panelGrid> </rich:panel> </h:form> </h:body> </html>
Corresponding java class should be a normal bean class like the following.
import javax.faces.bean.ManagedBean; import javax.faces.bean.RequestScoped; @ManagedBean @RequestScoped public class Student { private String name; private String email; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.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>.
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> <servlet-name>Resource Servlet</servlet-name> <servlet-class>org.richfaces.webapp.ResourceServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Resource Servlet</servlet-name> <url-pattern>/org.richfaces.resources/*</url-pattern> </servlet-mapping>
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.
<context-param> <param-name>org.richfaces.resourceOptimization.enabled</param-name> <param-value>true</param-value> </context-param>