Struts2的服务器端验证框架有四种方式校验,一种是利用dojo插件使用ajax实现页面无刷新的校验,一种是调用验证框架即配置一个xml文件的形式,另一种是在Action中的validate()方法手动校验,还有一种是零配置里面用annotation的形式校验。
如果要使用Struts2的验证框架,首先要开启这个拦截器。在文档中是这样介绍的:
默认的拦截器堆栈defaultStack已经在开启验证。在创建自己的interceptor-stack时一定要包括validation和workflow拦截器。
<interceptor-stack name="defaultStack">
...
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
1.加上dojo plugin这个jar包使用struts2的ajax校验。
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="name">
<field-validator type="requiredstring">
<message>You must enter a name</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">13</param>
<param name="max">19</param>
<message>Only people ages 13 to 19 may take this quiz</message>
</field-validator>
</field>
</validators>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package>
<action name="quizAjax" class="org.apache.struts2.showcase.validation.QuizAction">
<interceptor-ref name="jsonValidationWorkflowStack"/>
<result name="input">quiz-ajax.jsp</result>
<result>quiz-success.jsp</result>
</action>
</package>
......
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<html>
<head>
<title>Validation - AJAX</title>
<sx:head />
</head>
<s:url id="url" namespace="/validation" action="quizAjax"/>
<body>
<s:form method="post" theme="xhtml" namespace="/nodecorate" action="quizAjax" id="form">
<s:textfield label="Name" name="name" />
<s:textfield label="Age" name="age" />
<s:textfield label="Favorite color" name="answer"/>
<sx:submit validate="true" />
</s:form>
需要注意的:
1.<sx:head /> must be in the page, to be able to use AJAX functionality.
2.The form tag does not have validate set to true, which would perform client validation before the AJAX validation.
3.The submit tag must have validate set to true in order to perform AJAX validation.
4.如果action实现了ModelDrive接口。返回的JSON数据里的"model."将从字段名称被切割。如果验证成功,讲返回空JSON字符串。
2.在Action中实现手动校验:
如果我们重写了validate方法,则该方法会应用于此Action中的所有提供服务的业务方法。例如某个Action中有一个login()业务方法,我们可以写一个validateLogin()方法来进行login()的特殊校验,那么Action里执行方法的顺序就是:
validateLogin()------->validate()------->login()
在validateXXX方法里可以用addFiledError()讲相应的错误信息保存到fieldError中并返回input视图用<s:if test="hasFieldErrors()"><s;filederror/><s;if/>来展示。
3.在xml文件里面配置:
当一个Action中有多个业务方法时,我们可能需要对其中的某个方法配置单独的校验规则,比如注册时的要求用户两次输入的密码必须相同等,这时我们可以配置一个单独的校验文件,命名规则为:<actionName>-<methodName>-validation.xml,可以看到这里多了一个方法名,这个方法名就是要校验的业务逻辑在struts.xml配置文件中配置的name,这个文件也要同Action放在同一个目录下。注意这个方法名不是指的Action里面的方法名,而是action请求的name
当有多个校验文件时:
例如有一个LoginAction继承BaseAction,这两个Action中都有业务方法login,并且存在4份校验规则文件如下:BaseAction-vadition.xml, BaseAction-login-validation.xml , LoginAction-validation.xml, LoginAction-login-validation.xml那么用户访问LoginAction的login方法里,会按照以上的顺序执行校验规则,实际的校验规则是以上四个校验规则的总和,但是如果存在冲突的情况下,后面的校验规则优先。
4.用注解配置:
<interceptor-ref name="validation">
<param name="validateAnnotatedMethodOnly">true</param>
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
An Annotated Interface
@Validation()
public interface AnnotationDataAware {
void setBarObj(Bar b);
Bar getBarObj();
@RequiredFieldValidator(message = "You must enter a value for data.")
@RequiredStringValidator(message = "You must enter a value for data.")
void setData(String data);
String getData();
}
An Annotated Class
@Validation()
public class SimpleAnnotationAction extends ActionSupport {
@RequiredFieldValidator(type = ValidatorType.FIELD, message = "You must enter a value for bar.")
@IntRangeFieldValidator(type = ValidatorType.FIELD, min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")
public void setBar(int bar) {
this.bar = bar;
}
public int getBar() {
return bar;
}
@Validations(
requiredFields =
{@RequiredFieldValidator(type = ValidatorType.SIMPLE, fieldName = "customfield", message = "You must enter a value for field.")},
requiredStrings =
{@RequiredStringValidator(type = ValidatorType.SIMPLE, fieldName = "stringisrequired", message = "You must enter a value for string.")},
emails =
{ @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "emailaddress", message = "You must enter a value for email.")},
urls =
{ @UrlValidator(type = ValidatorType.SIMPLE, fieldName = "hreflocation", message = "You must enter a value for email.")},
stringLengthFields =
{@StringLengthFieldValidator(type = ValidatorType.SIMPLE, trim = true, minLength="10" , maxLength = "12", fieldName = "needstringlength", message = "You must enter a stringlength.")},
intRangeFields =
{ @IntRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "intfield", min = "6", max = "10", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
dateRangeFields =
{@DateRangeFieldValidator(type = ValidatorType.SIMPLE, fieldName = "datefield", min = "-1", max = "99", message = "bar must be between ${min} and ${max}, current value is ${bar}.")},
expressions = {
@ExpressionValidator(expression = "foo > 1", message = "Foo must be greater than Bar 1. Foo = ${foo}, Bar = ${bar}."),
@ExpressionValidator(expression = "foo > 2", message = "Foo must be greater than Bar 2. Foo = ${foo}, Bar = ${bar}."),
@ExpressionValidator(expression = "foo > 3", message = "Foo must be greater than Bar 3. Foo = ${foo}, Bar = ${bar}."),
@ExpressionValidator(expression = "foo > 4", message = "Foo must be greater than Bar 4. Foo = ${foo}, Bar = ${bar}."),
@ExpressionValidator(expression = "foo > 5", message = "Foo must be greater than Bar 5. Foo = ${foo}, Bar = ${bar}.")
}
)
public String execute() throws Exception {
return SUCCESS;
}
}