
.... <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>標(biāo)籤來(lái)設(shè)定屬性,接著我們可以如下取得所設(shè)定的屬性:
.... public void validate(FacesContext context, UIComponent component, Object obj) throws ValidatorException { .... String pattern = (String) component.getAttributes().get("pattern"); .... } ....
您也可以開(kāi)發(fā)自己的一組驗(yàn)證標(biāo)籤,並提供相關(guān)屬性設(shè)定,這需要瞭解JSP Tag Library的撰寫(xiě),所以請(qǐng)您先參考 JSP/Servlet 中有關(guān)於JSP Tag Library的介紹。
要開(kāi)發(fā)驗(yàn)證器轉(zhuǎn)用標(biāo)籤,您可以直接繼承javax.faces.webapp.ValidatorTag,這個(gè)類別可以幫您處理大部份的細(xì)節(jié),您所需要的,就是重新定義它的createValidator()方法,我們以改寫(xiě) 自訂驗(yàn)證器 中的PasswordValidator為例:
- PasswordValidator.java
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 { privateString 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, "字元長(zhǎng)度小於6", "字元長(zhǎng)度不得小於6"); thrownew ValidatorException(message); } if(pattern != null && !password.matches(pattern)) { FacesMessage message = new FacesMessage( FacesMessage.SEVERITY_ERROR, "密碼必須包括字元與數(shù)字", "密碼必須是字元加數(shù)字所組成"); thrownew ValidatorException(message); } } }
主要的差別是我們提供了pattern屬性,在validate()方法中進(jìn)行驗(yàn)證時(shí),是根據(jù)我們所設(shè)定的pattern屬性,接著我們繼承javax.faces.webapp.ValidatorTag來(lái)撰寫(xiě)自己的驗(yàn)證標(biāo)籤:
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 { privateString 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()方法建立驗(yàn)證器物件時(shí),是根據(jù)在faces-config.xml中註冊(cè)驗(yàn)證器的識(shí)別(Validater ID):
- faces-config.xml
<?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>
剩下來(lái)的工作,就是佈署tld描述檔了,我們簡(jiǎn)單的定義一下:
- taglib.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改寫(xiě)如下:
- 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>驗(yàn)證器示範(fàn)</title> </head> <body> <f:view> <h:messages layout="table" style="color:red"/> <h:form> <h3>請(qǐng)輸入您的名稱</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>
主要的差別是,我們使用了自己的驗(yàn)證器標(biāo)籤:
<co:passwordValidator pattern=".+[0-9]+"/>
如果要自訂轉(zhuǎn)換器標(biāo)籤,方法也是類似,您要作的是繼承javax.faces.webapp.ConverterTag,並重新定義其createConverter()方法。