struts2学习(12)struts2验证框架2.自定义验证
一、例子需求:
对敏感词进行验证;
将struts包中的validators.xml文件拷贝一份到src目录下,在最后面添加自己的验证器;
com.cy.validators.SensitiveWordValidators.java:
package com.cy.validators; import com.opensymphony.xwork2.validator.ValidationException; import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport; public class SensitiveWordValidators extends FieldValidatorSupport{ public void validate(Object obj) throws ValidationException { String fidleName = this.getFieldName(); //获取输入的控件input的name String value = this.getFieldValue(fidleName, obj).toString(); //获取该input的值 if(!check(value)){ this.addFieldError(fidleName, obj); } } public boolean check(String value){ String sensitiveWords[] = {"操", "你妈"}; for(int i=0; i<sensitiveWords.length; i++){ if(value.indexOf(sensitiveWords[i]) > -1) { return false; } } return true; } }
src目录下validators.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator Config 1.0//EN" "http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd"> <validators> <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/> <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/> <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/> <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/> <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/> <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/> <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/> <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/> <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/> <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/> <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/> <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/> <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/> <!-- 自定义的一条验证规格,用来验证敏感词汇 --> <validator name="sensitive" class="com.cy.validators.SensitiveWordValidators"></validator> </validators>
ValidationAction.java:
package com.cy.action; import com.opensymphony.xwork2.ActionSupport; public class ValidationAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String execute() throws Exception { System.out.println("传入的用户名:" + name); return SUCCESS; } }
ValidationAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?> <!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="name"> <field-validator type="requiredstring"> <message>请输入姓名</message> </field-validator> <field-validator type="sensitive"> <message>有敏感词汇</message> </field-validator> </field> </validators>
struts.xml中对ValidationAction的配置:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="manage" extends="struts-default"> <action name="registerAction" class="com.cy.action.RegisterAction"> <result name="input">register.jsp</result> <result name="success">success.jsp</result> </action> <action name="validationAction" class="com.cy.action.ValidationAction"> <result name="input">validation.jsp</result> <result name="success">success.jsp</result> </action> </package> </struts>
validation.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="/validationAction" method="post"> <s:textfield name="name" label="姓名"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body> </html>
测试结果:
输入带有定义好的含有敏感词汇的词,提交失败。