Java Struts2 自定义validation验证

定义验证类,继承FieldValidatorSupport,重写validate方法即可。

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public class BadwordFilter extends FieldValidatorSupport{

	@Override
	public void validate(Object obj) throws ValidationException {
		String fieldName = getFieldName();
		Object object = getFieldValue(fieldName, obj);
		if(object == null)
			return ;
		String value = object.toString();
		if (value == null || value.length() <= 0)
            return;

		if(isValidWord(value,11) == true){
			this.addFieldError(fieldName, obj);
		}
	}
	
	public  boolean isValidWord(String str1) {
		boolean isExist = false;
		if(str1 == "123456")
			isExist = true;
		return isExist;
	}
}



配置validators.xml
这个文件要和struts.xml放在同一个目录下,因为启动要加载嘛。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "http://www.opensymphony.com/xwork/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="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
    <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="badword" class="com.hx.corebase.validtor.BadwordFilter"/>
</validators>



这样就可以跟struts validator的一样配置了验证了。
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0//EN" 
	"http://www.opensymphony.com/xwork/xwork-validator-1.0.dtd">
	
<validators>
	<field name="badword">
		<field-validator type="requiredstring">
			<message >请输入昵称</message>
		</field-validator>
		<field-validator type="badword">
			<message >存在过滤词</message>
		</field-validator>
    </field>
 </validators>

你可能感兴趣的:(java,xml,struts)