在项目 HeadFirstStruts2chapter07 下,在src下导入validator.xml文件,
新建一个com.cruise.validators包,新建SensitiveWorkds类,继承,写验证方法,
package com.cruise.validators;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class SensitiveWordValidators extends FieldValidatorSupport{
@Override
public void validate(Object arg0) throws ValidationException {
String fieldName2 = this.getFieldName();//获取字段名称
String value = this.getFieldValue(fieldName2, arg0).toString();//获取字段的值
if(!check(value)){
this.addFieldError(fieldName2, arg0);
}
}
public boolean check(String value){
String[] sensitivewords = new String[]{"操","你妈"};
for(String i :sensitivewords){
if(value.indexOf(i)>-1){
return false;
}
}
return true;
}
}
2在validator.xml中添加SensitiveWorkds类的引用,
<?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.cruise.validators.SensitiveWordValidators"/>
</validators>
3新建ValidationAction类,name属性,get() set()
package com.cruise.action;
import com.opensymphony.xwork2.ActionSupport;
public class ValidationAction extends ActionSupport{
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;
}
}
4新建ValidationAction-validation.xml文件,放在src下,(文件名写法:相同Action名-validation.xml)
sensitive的名字要与validation.xml中自己定义的验证器的name属性的值一致
<?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>
5配置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="manager" extends="struts-default">
<action name="validationAction" class="com.cruise.action.ValidationAction">
<result name="input">/validation.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
6新建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:actionerror/>
注册
<s:form action="/validationAction" method="post">
<s:textfield name="name" label="名字"></s:textfield>
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>
测试
http://localhost:8080/HeadFirstStruts2chapter07/validation.jsp