1
2
|
<
interceptor
name
=
"validation"
class
=
"org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"
/>
<
interceptor
name
=
"workflow"
class
=
"com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor"
/>
|
1
2
3
|
public
class
AnnotationValidationInterceptor
extends
ValidationInterceptor {
//...
}
|
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 {
//...
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public
interface
Validator
/**
* 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;
//其它代码省略。。。
}
|
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);
}
}
}
}
|
1
2
3
4
5
6
7
8
|
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
"-//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
>
|
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;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
"-//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
>
|