• JSF>自订验证器


    一、定义自己的验证器

    您可以自订自己的验证器,所需要的是实现javax.faces.validator.Validator接口,例如我们实作一个简单的密码验证器,检查字元长度,以及密码中是否包括字元与数字:
    PasswordValidator.java
    package wsz.ncepu;
    
    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);
    		}
    	}
    }

    如果验证错误,则丢出一个ValidatorException,它接受一个FacesMessage物件,这个物件接受三个参数,分别表示讯息的严重程度(INFO、 WARN、ERROR、FATAL)、讯息概述与详细讯息内容,这些讯息将可以使用<h:messages>或<h: message>标籤显示在页面上。

    接下来要在faces-config.xml中注册验证器的识别(Validater ID),要加入以下的内容:
    faces-config.xml
    	<validator>
    		<validator-id>passwordValidator</validator-id>
    		<validator-class>wsz.ncepu.PasswordValidator</validator-class>
    	</validator>

    要使用自订的验证器,我们可以使用<f:validator>标签并设定validatorId属性,例如:

    <h:inputSecret value="#{user.password}" required="true">
        <f:validator validatorId="passwordValidator"/>
    </h:inputSecret>
    运行效果如下:
     

    二、让Bean自行负责验证的工作

    您也可以让Bean自行负责验证的工作,可以在Bean上提供一个验证方法,这个方法没有传回值,并可以接收FacesContext、UIComponent、Object三个参数,例如:
    UserBean.java
    package wsz.ncepu;
    
    import javax.faces.application.FacesMessage;
    import javax.faces.component.UIComponent;
    import javax.faces.context.FacesContext;
    import javax.faces.validator.ValidatorException;
    
    public class UserBean {
    	private String name;
    	private String password;
    	private String errMessage;
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setPassword(String password) {
    		this.password = password;
    	}
    
    	public String getPassword() {
    		return password;
    	}
    
    	public void setErrMessage(String errMessage) {
    		this.errMessage = errMessage;
    	}
    
    	public String getErrMessage() {
    		return errMessage;
    	}
    
    	public String verify() {
    		if (!name.equals("justin") || !password.equals("123456")) {
    			errMessage = "名称或密码错误";
    			return "failure";
    		} else {
    			return "success";
    		}
    	}
    
    	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}"/> 

    运行效果和上面一模一样


     

  • 相关阅读:
    Dev Express WPF GridControl 数据导出到Excel
    WPF DockLayoutManager布局后的布局重置
    几个平台视频,音频下载(批量下载)到本地的方式
    CSS 清除内外边距
    盒子模型外边距合并--塌陷问题
    log4net 配置数据库连接
    Common.Logging+log4net搭建项目日志框架
    VS2019 找不到资产文件 “xxxxobjproject.assets.json”运行NuGet包还原以生成此文件
    javascript的数组之slice()
    javascript的数组之includes()
  • 原文地址:https://www.cnblogs.com/xqzt/p/5637355.html
Copyright © 2020-2023  润新知