当struts2 为我们提供的15个验证器不能满足我们的验证要求时,我们就要自定义验证器了?
如何自定义验证器呢?
我们可以按照下面的步骤,就可以自定义验证器了!
具体的实施 :以 18位身份证验证器为例,步骤如下:
示例如下:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
项目结构
类
验证器支持类 IDCard.java
自定义验证器:IDCardValidate.java
package com.baidu.IdCard; import com.opensymphony.xwork2.validator.ValidationException; import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; public class IDCardValidate extends FieldValidatorSupport { @Override public void validate(Object object) throws ValidationException { //1. 获取参数 String fieldName = getFieldName(); Object value = this.getFieldValue(fieldName, object); //2. 验证 IDCard idCard = new IDCard(); boolean result = idCard.Verify((String)value); //3. 返回验证结果 if (!result) { addFieldError(fieldName, object); } } }
package com.baidu.IdCard; import com.opensymphony.xwork2.ActionSupport; public class IDCardValidationAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String idCard; public String getIdCard() { return idCard; } public void setIdCard(String idCard) { this.idCard = idCard; } @Override public String execute() throws Exception { return super.execute(); } }
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> <validators> <field name="idCard"> <field-validator type="idcard"> <message>身份证号码输入错误</message> </field-validator> </field> </validators>
配置 :struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置全局的国际化资源文件 --> <constant name="struts.custom.i18n.resources" value="nihao"></constant> <package name="default" namespace="/" extends="struts-default"> <action name="testIDCard" class="com.baidu.IdCard.IDCardValidationAction"> <result>/success.jsp</result> <result name="input">/idcard.jsp</result> </action> </package> </struts>
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator Definition 1.0//EN" "http://struts.apache.org/dtds/xwork-validator-definition-1.0.dtd"> <!-- START SNIPPET: validators-default --> <validators> <validator name="idcard" class="com.baidu.IdCard.IDCardValidate"/> </validators></span>
页面:
idcard.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>Insert title here</title> </head> <body> <s:form action="testIDCard"> <s:textfield name="idCard" label="IDCard"></s:textfield> <s:submit></s:submit> </s:form> </body> </html>success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!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>Insert title here</title> </head> <body> <h3>恭喜你有为青年!</h3> </body> </html>
效果: