36.Struts2_自定义验证器
1、Struts实现验证的过程
通过对Struts源代码的学习,总结一下Struts如何实现验证。
在struts-default.xml文件中,有validator和workflow两个拦截器。
1
2
|
< interceptor name = "validation" class = "org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor" />
< interceptor name = "workflow" class = "com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" />
|
概括地来说,validation拦截器是做验证,而workflow拦截器是利用validator的验证规则来决定该怎么做:如果存在错误,则返回Action.INPUT,如果不返回错误,则继续执行ActionInvocation的invoke方法。
接下来,我们来仔细的探讨一下validation拦截器,它对应的类是org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor。
AnnotationValidatorInterceptor类继承自ValidationInterceptor类
1
2
3
|
public class AnnotationValidationInterceptor extends ValidationInterceptor {
//...
}
|
ValidatorInterceptor类继承自MethodFilterInterceptor类
在它的类的注释中有这样的话:This interceptor runs the action through the standard validation framework, which in turn checks the action against any validation rules (found in files such as ActionClass-validation.xml) and adds field-level and action-level error messages (provided that the action implements com.opensymphony.xwork2.ValidationAware).
也就是说,如果ValidatorInterceptor类作为拦截器的时候,它会让standard validation framework check the action any validation rules,当然这些validation rules包括在ActionClass-validation中做的验证规则。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/**
*ValidationInterceptor可以引导action经过the standard validation framework。
*如果the standard validation framework查找到任何与validation rules相违背的地方,
*就会添加field-level或者是action-level的错误信息(前提是实现了ValidationAware接口)。
*This interceptor runs the action through the standard validation framework,
*which in turn checks the action against any validation rules (found in files
*such as ActionClass-validation.xml) and adds field-level and action-level error messages
*(provided that the action implements com.opensymphony.xwork2.ValidationAware).
*这个拦截器常常是拦截器栈的最后一个。
*This interceptor is often one of the last (or second to last) interceptors applied in a stack,
*as it assumes that all values have already been set on the action.
*
*如果为当前拦截器指定了excludeMethods参数,excludeMethods参数里包含的方法就不会被拦截。
*This interceptor does nothing if the name of the method being invoked is specified
*in the "excludeMethods" parameter. "excludeMethods" accepts a comma-delimited list of method names.
*For example, requests to "foo!input.action" and "foo!back.action" will be skipped by this interceptor
*if you set the "excludeMethods" parameter to "input, back".
*
*The workflow of the action request does not change due to this interceptor.
*Rather, this interceptor is often used in conjuction with the workflow interceptor.
*/
public class ValidationInterceptor extends MethodFilterInterceptor {
//...
}
|
在上面,我们总结这样的结论:
如果ValidatorInterceptor类作为拦截器的时候,它会让standard validation framework check the action any validation rules,当然这些validation rules包括在ActionClass-validation中做的验证规则。
而AnnotationValidatorInterceptor类继承自ValidationInterceptor类,因此当java培训机构中的AnnotationValidatorInterceptor类注册为拦截器的时候,也会检验这些validation rules。
在这里要区分两个概念:验证拦截器和验证器。验证拦截器(ValidatorInterceptor),本质上就是拦截器,它的功能是:拦截住request请求,然后调用验证器(Validator)进行验证。
2、自定义验证规则
2.1、自定义验证规则的步骤
自定义验证规则,包括3个步骤:
(1)自定义验证程序必须实现 Validator 接口
Validator 接口有一个方法:
void validate(java.lang.Object object)
需要开发者进行覆盖。
另外ValidatorSupport 、 FieldValidatorSupport 实现了 Validator 接口
若需要普通的验证程序, 可以继承 ValidatorSupport 类
若需要field验证程序, 可以继承 FieldValidatorSupport 类
(2)注册
在类路径(src)下的 validators.xml 文件里注册
(3)应用
在Action的校验文件(Action类名-validation.xml)中添加校验器
Validator接口的源代码定义如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public interface Validator<T> {
/**
* The validation implementation must guarantee that setValidatorContext will
* be called with a non-null ValidatorContext before validate is called.
*
* @param object the object to be validated.
* @throws ValidationException is thrown if there is validation error(s).
*/
void validate(Object object) throws ValidationException;
//其它代码省略。。。
}
|
2.2、示例
a)自定义规则类
PositiveNumberValidator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.rk.strut.j_validation;
import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;
public class PositiveNumberValidator extends FieldValidatorSupport
{
@Override
public void validate(Object object) throws ValidationException
{
String fieldName = getFieldName();
Object fieldValue = this .getFieldValue(fieldName, object);
if (fieldValue instanceof java.lang.Integer)
{
Integer value = Integer.parseInt(fieldValue.toString());
//如果小于0,则添加Field Error
if (value<= 0 )
{
super .addFieldError(fieldName, object);
}
}
}
}
|
b)在src下的validators.xml**册该规则
1
2
3
4
5
6
7
8
|
<? 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">
< validators >
< validator name = "positivenum" class = "com.rk.strut.j_validation.PositiveNumberValidator" ></ validator >
</ validators >
|
c)使用校验规则
建立StudentAction.java,并在该StudentAction.java同一包下建立StudentAction-validation.xml文件中
StudentAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package com.rk.strut.j_validation;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
public class StudentAction extends ActionSupport
{
private int weight;
public int getWeight()
{
return weight;
}
public void setWeight( int weight)
{
this .weight = weight;
}
@Override
public String execute() throws Exception
{
return Action.SUCCESS;
}
}
|
StudentAction-validation.xml
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.dtd">
< validators >
< field name = "weight" >
< field-validator type = "positivenum" >
< message >必须输入正数</ message >
</ field-validator >
</ field >
</ validators >
|