日期:2014-05-16 浏览次数:20498 次
这两天有个朋友在做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