在 自訂驗證器
中,我們的驗證器只能驗證一種pattern(.+[0-9]+),我們希望可以在JSF頁面上自訂匹配的pattern,然而由於我們使用<f:
validator>這個通用的驗證器標籤,為了要能提供pattern屬性,我們可以使用<f:attribute>標籤來設置,例
如:
....
<h:inputSecret value="#{user.password}" required="true">
<f:validator validatorId="onlyfun.caterpillar.Password"/>
<f:attribute name="pattern" value=".+[0-9]+"/>
</h:inputSecret><p>
....
使用<f:attribute>標籤來設定屬性,接著我們可以如下取得所設定的屬性:
....
public void validate(FacesContext context,
UIComponent component,
Object obj)
throws ValidatorException {
....
String pattern = (String)
component.getAttributes().get("pattern");
....
}
....
您也可以開發自己的一組驗證標籤,並提供相關屬性設定,這需要瞭解JSP Tag Library的撰寫,所以請您先參考 JSP/Servlet 中有關於JSP Tag Library的介紹。
要開發驗證器轉用標籤,您可以直接繼承javax.faces.webapp.ValidatorTag,這個類別可以幫您處理大部份的細節,您所需要的,就是重新定義它的createValidator()方法,我們以改寫 自訂驗證器 中的PasswordValidator為例:
package onlyfun.caterpillar;
import javax.faces.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException;
public class PasswordValidator implements Validator { private String pattern;
public void setPattern(String pattern) { this.pattern = pattern; } public void validate(FacesContext context, UIComponent component, Object obj) throws ValidatorException { String password = (String) obj; if(password.length() < 6) { FacesMessage message = new FacesMessage( FacesMessage.SEVERITY_ERROR, "字元長度小於6", "字元長度不得小於6"); throw new ValidatorException(message); } if(pattern != null && !password.matches(pattern)) { FacesMessage message = new FacesMessage( FacesMessage.SEVERITY_ERROR, "密碼必須包括字元與數字", "密碼必須是字元加數字所組成"); throw new ValidatorException(message); } } }
主要的差別是我們提供了pattern屬性,在validate()方法中進行驗證時,是根據我們所設定的pattern屬性,接著我們繼承 javax.faces.webapp.ValidatorTag來撰寫自己的驗證標籤:
- PasswordValidatorTag.java
package onlyfun.caterpillar;
import javax.faces.application.Application; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.webapp.ValidatorTag;
public class PasswordValidatorTag extends ValidatorTag { private String pattern; public void setPattern(String pattern) { this.pattern = pattern; } protected Validator createValidator() { Application application = FacesContext.getCurrentInstance(). getApplication(); PasswordValidator validator = (PasswordValidator) application.createValidator( "onlyfun.caterpillar.Password"); validator.setPattern(pattern); return validator; } }
application.createValidator()方法建立驗證器物件時,是根據在faces-config.xml中註冊驗證器的識別(Validater ID):
<?xml version="1.0"?> <!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config> .... <validator> <validator-id> onlyfun.caterpillar.Password </validator-id> <validator-class> onlyfun.caterpillar.PasswordValidator </validator-class> </validator> .... </faces-config>
剩下來的工作,就是佈署tld描述檔了,我們簡單的定義一下:
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd" version="2.0"> <description>PasswordValidator Tag</description> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>co</short-name> <uri>http://caterpillar.onlyfun.net</uri>
<tag> <description>PasswordValidator</description> <name>passwordValidator</name> <tag-class> onlyfun.caterpillar.PasswordValidatorTag </tag-class> <body-content>empty</body-content> <attribute> <name>pattern</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag>
</taglib>
而我們的index.jsp改寫如下:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="/WEB-INF/taglib.tld" prefix="co" %> <%@page contentType="text/html;charset=Big5"%> <html> <head> <title>驗證器示範</title> </head> <body> <f:view> <h:messages layout="table" style="color:red"/> <h:form> <h3>請輸入您的名稱</h3> <h:outputText value="#{user.errMessage}"/><p> 名稱: <h:inputText value="#{user.name}" required="true"/><p> 密碼: <h:inputSecret value="#{user.password}" required="true"> <co:passwordValidator pattern=".+[0-9]+"/> </h:inputSecret> <p> <h:commandButton value="送出" action="#{user.verify}"/> </h:form> </f:view> </body> </html>
主要的差別是,我們使用了自己的驗證器標籤:
<co:passwordValidator pattern=".+[0-9]+"/>
如果要自訂轉換器標籤,方法也是類似,您要作的是繼承javax.faces.webapp.ConverterTag,並重新定義其 createConverter()方法。 |