JSF自定义校验器标签

     在JSF中标签的使用和JSP中标签一样,必须在标签tld文件中配置。在页面使用必须添加导入相关的标签和信息。

首先编写

校验标签和校验器信息如下:

package com.easyway.jsf.commons;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;

import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/***
 * JSF自定义验证器的实现
 * 必须实现Validator的接口
 * public void validate(FacesContext fc, UIComponent uicom, Object obj)
 *			throws ValidatorException;
 *
 * @author longgangbai
 *  
 */
public class EmailValidator implements Validator{

	private String pattern;
	
	public String getPattern() {
		return pattern;
	}

	public void setPattern(String pattern) {
		this.pattern = pattern;
	}
	/**
	 * 添加一个校验器的方法
	 */
	public void validate(FacesContext fc, UIComponent uicom, Object obj)
			throws ValidatorException {
		String email=(String)obj;
		//验证电子邮件的长度是否符合条件
		String mymsg=(String)uicom.getAttributes().get("mymsg");
		//获取当前页面中属性的信息
		String emailpattern=(String)uicom.getAttributes().get("emailpattern");
		
		if(emailpattern!=null){
			pattern=emailpattern;
		}
		//添加一个FacesMessage 消息
		if(email.length()<5){
			FacesMessage message=new FacesMessage(FacesMessage.SEVERITY_ERROR,"电子邮件长度不足",mymsg);
            throw new ValidatorException(message);		
		}
		if(pattern!=null&&!email.matches(pattern)){
			FacesMessage message=new FacesMessage(FacesMessage.SEVERITY_ERROR,"电子邮件的格式不匹配","电子邮件的格式不匹配,请仔细检查");
            throw new ValidatorException(message);		
		}
	}

}


package com.easyway.jsf.commons;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.webapp.ValidatorTag;

/***
 * JSF自定义校验器标签的实现
 * @author longgangbai
 *
 */
public class EmailValidatorTag extends ValidatorTag{

		/**
		 * 
		 */
	    private static final long serialVersionUID = 1L;
		private String pattern;
		
		public void setPattern(String pattern) {
			this.pattern = pattern;
		}
		/**
		 * 创建自定义的校验器
		 */
		@Override
		protected Validator createValidator() {
			//获取当前应用程序的Application
			Application application =FacesContext.getCurrentInstance().getApplication();
			//获取应用的校验器的
			EmailValidator validator =
			(EmailValidator) application.createValidator(
			"com.easyway.jsf.email");
			//设置自定义的正则表达式
			validator.setPattern(pattern);
			return validator;
		}
}

 

在faces-config.xml配置校验器的消息:

    <!-- JSF 校验器的配置 -->
    <validator>
		<validator-id>com.easyway.jsf.email</validator-id>
		<validator-class>com.easyway.jsf.commons.EmailValidator</validator-class>
	</validator>

 在标签文件中配置如下:

<?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>EmailValidator Tag</description>
	<tlib-version>1.0</tlib-version>
	<short-name>co</short-name>
	<uri>http://easyway.net.cn</uri>
	<tag>
		<description>EmailValidator</description>
		<name>EmailValidator</name>
		<tag-class>
			com.easyway.jsf.commons.EmailValidatorTag
		</tag-class>
		<body-content>empty</body-content>
		<attribute>
		<name>pattern</name>
		<required>true</required>
		<rtexprvalue>false</rtexprvalue>
		</attribute>
	</tag>
</taglib>

 

 

 

在页面使用标签:

导入标签

<%@ taglib uri="/WEB-INF/taglib.tld" prefix="co" %>

 使用标签:

	  
		    <!-- 自定义标签的校验器的使用 -->
		     <h:outputLabel value="用户账号"></h:outputLabel>
			<h:inputSecret id="password" value="#{mathService.password}" required="true">
			    <co:EmailValidator pattern=".+[0-9]+"/>
			 </h:inputSecret>
			 <h:message for="password" ></h:message>
			 
		    <h:commandButton value="#{msgs.math_result}" action="#{mathService.resultSum}"></h:commandButton>
			

 

你可能感兴趣的:(xml,Web,正则表达式,JSF,sun)