这两天有个朋友在做jsf自定义验证器时遇到了一些问题,问了我。我整了好久也没能搞明白,后来发现可能是他实现自定义验证器时使用的类太老(项目用的是jsf1.2,自定义验证器时却用的jsf1.1的类-ValidatorTag,这个类在jsf1.2中已经被建议不使用,这位朋友在实现时用的tld标签也是2.0之上的,我也不确定问题是否出在这里)。
下班后没事就在jsf1.2的库上,在参考网上一些资料的情况下,自己写了一个自定义验证器的demo。该例子用途是验证email格式。现贴出代码:)
package test.managebean下的class:
package test.managebean;
/**
*
* @author SailingLee
*/
public class TestManageBean {
private String postCode;
public void doSomeThing(){
System.out.println("============doSomeThing============");
}
/**
* @return the postCode
*/
public String getPostCode() {
return postCode;
}
/**
* @param postCode the postCode to set
*/
public void setPostCode(String postCode) {
this.postCode = postCode;
}
}
package test.validator下的class:
package test.validator;
import java.io.Serializable;
//import java.text.MessageFormat;
import java.util.regex.Pattern;
//import java.util.Locale;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import javax.faces.application.FacesMessage;
/**
*
* @author SailingLee
*/
public class CustomValidator implements Validator, Serializable {
private String length;
private String regex;
private String errorSummary;
private String errorDetail;
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
FacesMessage message = null;
if (value == null || value.toString().trim().length() == 0) {
message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "输入不能为空!", "您的输入为空,请输入!");
//return;
}else if (value.toString().length() > Integer.parseInt(getLength())) {
message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "输入长度超过" + getLength() + "位!", "输入长度超过" + getLength() + "位!请重新输入!");
//return;
}else if (regex != null && !regex.equals("")) {
boolean b = Pattern.matches(regex, value.toString());
if (!b) {
message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "输入的邮编格式错误!", "输入的邮编格式错误!请确认!");
//return;
}
}
throw new ValidatorException(message);
}
/**
* @return the errorSummary
*/
public String getErrorSummary() {
return errorSummary;
}
/**
* @param errorSummary the errorSummary to set
*/
public void setErrorSummary(String errorSummary) {
this.errorSummary = errorSummary;
}
/**
* @return the errorDetail
*/
public String getErrorDetail() {
return errorDetail;
}
/**
* @param errorDetail the errorDetail to set
*/
public void setErrorDetail(String errorDetail) {
this.errorDetail = errorDetail;
}
/**
* @return the regex
*/
public String getRegex() {
return regex;
}
/**
* @param regex the regex to set
*/
public void setRegex(String regex) {
this.regex = regex;
}
/**
* @return the length
*/
public String getLength() {
return length;
}
/**
* @param length the length to set
*/
public void setLength(String length) {
this.length = length;
}
}
package test.tag下的class:
package test.tag;
import test.validator.CustomValidator;
import javax.faces.validator.Validator;
import javax.faces.webapp.ValidatorELTag;
import javax.faces.context.FacesContext;
import javax.servlet.jsp.JspException;
import javax.el.ValueExpression;
import javax.el.ELContext;
/**
*
* @author SailingLee
*/
public class CustomValidatorTag extends ValidatorELTag {
private ValueExpression length;
private ValueExpression regex;
private ValueExpression errorSummary;
private ValueExpression errorDetail;
@Override
protected Validator createValidator() throws JspException {
CustomValidator validator = new CustomValidator();
ELContext elContext = FacesContext.getCurrentInstance().getELContext();
validator.setLength((String) length.getValue(elContext));
validator.setRegex((String) regex.getValue(elContext));
if (errorSummary != null)
validator.setErrorSummary((String) errorSummary.getValue (elContext));
if (errorDetail != null)
validator.setErrorDetail((String) errorDetail.getValue (elContext));
return validator;
}
/**
* @param length the length to set
*/
public void setLength(ValueExpression length) {
this.length = length;
}
/**
* @param regex the regex to set
*/
public void setRegex(ValueExpression regex) {
this.regex = regex;
}
/**
* @param errorSummary the errorSummary to set
*/
public void setErrorSummary(ValueExpression errorSummary) {
this.errorSummary = errorSummary;
}
/**
* @param errorDetail the errorDetail to set
*/
public void setErrorDetail(ValueExpression errorDetail) {
this.errorDetail = errorDetail;
}
public void release() {
length = null;
regex = null;
errorSummary = null;
errorDetail = null;
}
}
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="1.2"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean>
<managed-bean-name>testManageBean</managed-bean-name>
<managed-bean-class>test.managebean.TestManageBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<validator><!--看见网上很多例子都在这里加上下面两行,但经过测试,没有这两行也完全没影响。(看了下,发现这里的配置只在jsf1.1中需要) -->
<validator-id>test.validator.CustomValidator</validator-id>
<validator-class>test.validator.CustomValidator</validator-class>
</validator>
</faces-config>
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.0" 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">
<tlib-version>1.0</tlib-version>
<short-name>customvalidator</short-name>
<uri>/WEB-INF/CustomValidator</uri>
<tag>
<name>email</name>
<tag-class>test.tag.CustomValidatorTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>length</name>
<required>true</required>
<deferred-value>
<type>java.lang.String</type>
</deferred-value>
</attribute>
<attribute>
<name>regex</name>
<required>true</required>
<deferred-value>
<type>java.lang.String</type>
</deferred-value>
</attribute>
<attribute>
<name>errorSummary</name>
<deferred-value>
<type>java.lang.String</type>
</deferred-value>
</attribute>
<attribute>
<name>errorDetail</name>
<deferred-value>
<type>java.lang.String</type>
</deferred-value>
</attribute>
</tag>
</taglib>
<jsp-config>
<taglib>
<taglib-uri>http://email.com</taglib-uri>
<taglib-location>/WEB-INF/CustomValidator.tld</taglib-location>
</taglib>
</jsp-config>
页面代码:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<%@taglib prefix="custom" uri="http://email.com"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Custom Validate Email</title>
</head>
<body>
<f:view>
<h:form id="myform">
Email:<h:inputText id="postCode" value="#{testManageBean.postCode}">
<custom:email length="10" regex="[a-z]*@[a-z]*.[a-z]*"/>
</h:inputText>
<h:commandButton action="#{testManageBean.doSomeThing}" value="validate email"/>
</h:form>
<h:messages/>
</f:view>
</body>
</html>
使用类库是jsf1.2版本的。
效果图如下:
注:该例子中关于自定义errorSummary和errorDetail的功能没有实现。
- 大小: 7.8 KB
分享到:
相关推荐
使用jsf1.2+spring3.0+hibernate3.3实现集成,利用annotation实现自动对象管理注入,用户表的登录增删改查操作,包括验证码ajax集成,消息机制,国际化处理,自定义转换器,自定义验证器等 qq:38732796 欢迎讨论
文档目录如下: 1. 入门 1.1 简介JSF 1.2 第一个JSF程序 1.3简单的导航 Navigation ...3.4自定义验证器 3.5错误信息处理 3.6 自定义转换, 验证标签 4. 事件处理 4.1 动作事件 4.2 即时事件 4.3 值变事件 4.4 Phase事件
10.8.2 使用专门的监听器实现回调 448 10.8.3 为全部实体配置默认监听器 450 10.8.4 排除监听器 452 10.9 本章小结 455 第11章 JPA的查询支持 456 11.1 查询API 457 11.1.1 面向对象的JPQL 457 11.1.2 查询API简介 ...
3.2.2 自定义Java代码编辑器 45 3.2.3 Java代码编辑器相关视图 50 3.3 MyEclipse中的Java代码编译器 51 3.4 MyEclipse中的Java代码调试器 52 3.4.1 Java项目的运行 52 3.4.2 Java项目的调试 53 3.5 MyEclipse中的...
1.2开始spring之旅 1.3理解依赖注入 1.3.1依赖注入 1.3.2di应用 1.3.3企业级应用中的依赖注入 1.4应用aop 1.4.1aop介绍 1.4.2aop使用 1.5小结 第2章基本bean装配 2.1容纳你的bean 2.1.1beanfactory介绍 ...
1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP使用 1.5 小结 第2章 基本Bean装配 2.1 容纳你的Bean 2.1.1 ...
1.2 开始Spring之旅 1.3 理解依赖注入 1.3.1 依赖注入 1.3.2 DI应用 1.3.3 企业级应用中的依赖注入 1.4 应用AOP 1.4.1 AOP介绍 1.4.2 AOP使用 1.5 小结 第2章 基本Bean装配 2.1 容纳你的Bean 2.1.1 ...
1.2. 第一个例子:注册示例.............................................................................................................................................. 15 1.2.1. 了解代码.................
它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费...
它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费...
Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接与浏览,Java源码下载 1个目标文件 摘要:Java源码,网络相关,HTTP Java实现HTTP连接与浏览,Java源码下载,输入html文件地址或网址,显示页面和HTML源文件...
日历表格面板 [ConfigLine.java] 控制条类 [RoundBox.java] 限定选择控件 [MonthMaker.java] 月份表算法类 [Pallet.java] 调色板,统一配色类 Java扫雷源码 Java生成自定义控件源代码 2个目标文件 Java实现HTTP连接...
2.16 创建自定义属性编辑器 99 2.16.1 问题 99 2.16.2 解决方案 100 2.16.3 工作原理 100 2.17 使用TaskExecutor实现并发性 101 2.17.1 问题 101 2.17.2 解决方案 101 2.17.3 工作原理 102 2.18 小...
2.16 创建自定义属性编辑器 99 2.16.1 问题 99 2.16.2 解决方案 100 2.16.3 工作原理 100 2.17 使用TaskExecutor实现并发性 101 2.17.1 问题 101 2.17.2 解决方案 101 2.17.3 工作原理 102 2.18 小...
它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费...
它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费...
它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费...