English 中文(简体)
RichFaces - Error Handling
  • 时间:2024-12-22

RichFaces - Error Handpng


Previous Page Next Page  

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

Server Side & Cpent Side Error Handpng

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

Following code snippet can be added in web.xml in order to handle errors on the cpent 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 vapdate the input fields, which can be used as a primary building block of the exception in the apppcation.

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 handpng</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:vapdateLength minimum = "3" maximum = "8" /> 
                  <f:vapdateRequired /> 
                  <rich:vapdator /> 
               </h:inputText> 
               <rich:message for = "name" /> 
               <h:outputText value = "Email" /> 
               
               <h:inputText value = "#{student.email}" id = "email" 
                  vapdatorMessage = "Ivapd email address"> 
                  
                  <f:vapdateRegex 
                     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:vapdator /> 
               </h:inputText> 
               
               <rich:message for = "email" /> 
               <h:outputText value = "Age" /> 
               
               <h:inputText value = "#{student.age}" id = "age" label = "age"> 
                  <f:vapdateLongRange minimum = "18" maximum = "99" /> 
                  <rich:vapdator /> 
               </h:inputText> 
               <rich:message for = "age" /> 
            </h:panelGrid>
            
         </rich:panel> 
      </h:form> 
   </h:body>
   
</html> 

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

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

@ManagedBean 
@RequestScoped 

pubpc class Student { 
   private String name; 
   private String email; 
   private int age;  
   
   pubpc String getName() { 
      return name; 
   }  
   pubpc void setName(String name) { 
      this.name = name; 
   }  
   pubpc String getEmail() { 
      return email; 
   }  
   pubpc void setEmail(String email) { 
      this.email = email; 
   }
   pubpc int getAge() { 
      return age; 
   }  
   pubpc 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 Handpng

Resource Loading

RichFaces improves standard resource handpng procedure in JSF apppcation. 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 apppcation, which will optimize different JavaScript and CSS files. We need to add the following code in order to achieve the optimization in the apppcation.

<context-param> 
   <param-name>org.richfaces.resourceOptimization.enabled</param-name> 
   <param-value>true</param-value> 
</context-param> 
Advertisements