您可以自订自己的验证器,所需要的是实作javax.faces.validator.Validator接口,例如我们实作一个简单的密码验证器,检查字符长度,以及密码中是否包括字符与数字:
PasswordValidator.java
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 {
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(!password.matches(".+[0-9]+")) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"密码必须包括字符与数字",
"密码必须是字符加数字所组成");
throw new ValidatorException(message);
}
}
}
您要实作javax.faces.validator.Validator接口中的validate()方法,如果验证错误,则丢出一个ValidatorException,它接受一个FacesMessage对象,这个对象接受三个参数,分别表示讯息的严重程度(INFO、 WARN、ERROR、FATAL)、讯息概述与详细讯息内容,这些讯息将可以使用<h:messages>或<h: message>卷标显示在页面上。
接下来要在faces-config.xml中注册验证器的识别(Validater ID),要加入以下的内容:
faces-config.xml
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>
要使用自订的验证器,我们可以使用<f:validator>卷标并设定validatorId属性,例如:
....
<h:inputSecret value="#{user.password}" required="true">
<f:validator validatorId="onlyfun.caterpillar.Password"/>
</h:inputSecret><p>
....
您也可以让Bean自行负责验证的工作,可以在Bean上提供一个验证方法,这个方法没有传回值,并可以接收FacesContext、UIComponent、Object三个参数,例如:
UserBean.java
UserBean.java
package onlyfun.caterpillar;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
public class UserBean {
....
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(!password.matches(".+[0-9]+")) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"密码必须包括字符与数字",
"密码必须是字符加数字所组成");
throw new ValidatorException(message);
}
}
}
接着可以在页面下如下使用验证器:
.....
<h:inputSecret value="#{user.password}"
required="true"
validator="#{user.validate}"/>
....
PasswordValidator.java
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 {
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(!password.matches(".+[0-9]+")) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"密码必须包括字符与数字",
"密码必须是字符加数字所组成");
throw new ValidatorException(message);
}
}
}
您要实作javax.faces.validator.Validator接口中的validate()方法,如果验证错误,则丢出一个ValidatorException,它接受一个FacesMessage对象,这个对象接受三个参数,分别表示讯息的严重程度(INFO、 WARN、ERROR、FATAL)、讯息概述与详细讯息内容,这些讯息将可以使用<h:messages>或<h: message>卷标显示在页面上。
接下来要在faces-config.xml中注册验证器的识别(Validater ID),要加入以下的内容:
faces-config.xml
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>
要使用自订的验证器,我们可以使用<f:validator>卷标并设定validatorId属性,例如:
....
<h:inputSecret value="#{user.password}" required="true">
<f:validator validatorId="onlyfun.caterpillar.Password"/>
</h:inputSecret><p>
....
您也可以让Bean自行负责验证的工作,可以在Bean上提供一个验证方法,这个方法没有传回值,并可以接收FacesContext、UIComponent、Object三个参数,例如:
UserBean.java
UserBean.java
package onlyfun.caterpillar;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
public class UserBean {
....
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(!password.matches(".+[0-9]+")) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"密码必须包括字符与数字",
"密码必须是字符加数字所组成");
throw new ValidatorException(message);
}
}
}
接着可以在页面下如下使用验证器:
.....
<h:inputSecret value="#{user.password}"
required="true"
validator="#{user.validate}"/>
....