package validator; import javax.faces.component.UIComponent; import javax.faces.context.FacesContext; import javax.faces.validator.Validator; import javax.faces.validator.ValidatorException; public class MyValidator implements Validator { /* (non-Javadoc) * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object) */ public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException { // TODO Auto-generated method stub } public void validate(FacesContext arg0, UIComponent arg1, Object arg2) throws ValidatorException { System.out.println("my custom validator"); //TODO: Add your custom validation code here. This code will be execured during the validation //phase of the JSF lifecycle. If any validation errors occur you can throw an exception message //that in turn can be displayed using a h:message (or h:messages) component. }
<validator> <validator-id>MyValidator</validator-id> <validator-class>validator.MyValidator</validator-class> </validator>
保存 faces-config.xml。
<h:inputText styleClass="inputText" id="text1"><f:validator validatorId="MyValidator"></f:validator></h:inputText>