- RichFaces 教程
- RichFaces - 主頁
- RichFaces - 概述
- RichFaces - 環境設定
- RichFaces - 架構
- RichFaces - 基本概念
- RichFaces - 豐富皮膚
- RichFaces - 輸入元件
- RichFaces - 輸出元件
- RichFaces - 迭代元件
- RichFaces - 選擇元件
- RichFaces - 選單元件
- RichFaces - Rich Tree
- RichFaces - 錯誤處理
- RichFaces 有用資源
- RichFaces - 快速指南
- RichFaces - 有用資源
- RichFaces - 討論
RichFaces - 錯誤處理
在本章中,我們將學習 RichFaces 中實現的不同錯誤處理方法。
伺服器端和客戶端錯誤處理
我們需要使用非常舊的 Java 技術(try/Catch)來處理基於 Action 類引發的異常。對於客戶端,我們可以新增一個額外的檔案,它將在客戶端發生錯誤時顯示錯誤資訊。
可在 web.xml 中新增以下程式碼片段來處理客戶端上的錯誤。
<error-page> <exception-type>java.lang.Throwable</exception-type> <location>/error.xhtml</location> </error-page>
請注意,上述異常將只提供靜態異常訊息,我們可能必須使用 JSF “ExceptionHandler”類才能使用動態異常屬性。在執行時,RichFaces 提供了一些驗證輸入欄位的功能,這些功能可以用作應用程式中異常的主要基礎構建模組。
建立新檔案並在其中放入以下程式碼。
<?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>
對應的 java 類應為如下所示的普通 bean 類。
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;
}
}
只要 < h:form > 中有錯誤,上述示例將在瀏覽器中產生以下輸出。
資源載入
RichFaces 改進了 JSF 應用程式中的標準資源處理過程。透過對 ResourceServlet 進行配置或透過資源最佳化,可以實現這一點。要配置 ResourceServlet,我們需要在 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>
我們還可以啟用 JSF 應用程式中的最佳化,這將最佳化不同的 JavaScript 和 CSS 檔案。我們需要新增以下程式碼才能在應用程式中實現最佳化。
<context-param> <param-name>org.richfaces.resourceOptimization.enabled</param-name> <param-value>true</param-value> </context-param>
廣告