主要介绍struts2的XML配置方式输入校验,结合实例进行说明。
代码结构:
关键代码:
RegistAction.javapackage com.alfred.regist.action;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class RegistAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String repassword;
private Integer age;
private Date birthday;
private Date graduation;
public String toRegist() {
System.out.println("run toRegist");
return SUCCESS;
}
@Override
public void validate() {
System.out.println("run validate");
}
public void validateToRegist() {
if (this.hasFieldErrors()) {
System.out.println("already has field error");
}
System.out.println("run validateToRegist");
if (age != null && (age < 20 || age > 100)) {
this.addFieldError("age", "(code)年龄错误");
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRepassword() {
return repassword;
}
public void setRepassword(String repassword) {
this.repassword = repassword;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Date getGraduation() {
return graduation;
}
public void setGraduation(Date graduation) {
this.graduation = graduation;
}
}
regist.jsp<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<!--
校验规则:
username 不为空 2~5位
password 不为空 3~6位
repassword 与password一致
age 介于20~100岁
birthday 不会空 大于2000-01-01年
graduation 不会空 大于2010-01-01年
-->
<s:actionmessage cssStyle="color:red" />
<s:actionerror cssStyle="color:green;" />
<s:fielderror cssStyle="color:blue;" />
===============
<br />
<s:form action="regist">
<s:textfield label="username" id="username" name="username"></s:textfield>
<s:password label="password" id="password" name="password"></s:password>
<s:password label="repassword" id="repassword" name="repassword"></s:password>
<s:textfield label="age" id="age" name="age"></s:textfield>
<s:textfield label="birthday" id="birthday" name="birthday"></s:textfield>
<s:textfield label="graduation" id="graduation" name="graduation"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
struts.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="regist"
class="com.alfred.regist.action.RegistAction" method="toRegist">
<result name="success">WEB-INF/jsp/main.jsp</result>
<result name="input">regist.jsp</result>
</action>
</package>
</struts>
使用XML方式检验需创建一个XML配置文件,位置在对应的Action同级目录下,命名规则为:Action名-validatioin.xml。
校验文件可以有两种写法,一种是字段校验,一种是非字段校验。字段校验就是以字段为主,在对应字段配置下添加该字段的校验器,非字段校验就是先定义好校验器,在校验器下添加字段。如下:
RegistAction-validation.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="username">
<field-validator type="requiredstring">
<message>(XML)username is not blank</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">2</param>
<param name="maxLength">5</param>
<message>
(XML)username is ${minLength}~${maxLength}
</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>(XML)password is not blank</message>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">3</param>
<param name="maxLength">6</param>
<message>
(XML)password is ${minLength}~${maxLength}
</message>
</field-validator>
</field>
<field name="repassword">
<field-validator type="fieldexpression">
<param name="expression">
<![CDATA[(password==repassword)]]>
</param>
<message>(XML)two password should be the same!</message>
</field-validator>
</field>
<field name="age">
<field-validator type="required">
<message>(XML)age is not blank</message>
</field-validator>
<field-validator type="int">
<param name="min">20</param>
<param name="max">100</param>
<message>(XML)age is ${min}~${max}</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="required">
<message>(XML)birthday is not blank</message>
</field-validator>
<field-validator type="date">
<param name="min">2000-01-01</param>
<message>
(XML)birthday is date and greater than ${min}
</message>
</field-validator>
</field>
<field name="graduation">
<field-validator type="required">
<message>graduation is not blank</message>
</field-validator>
<field-validator type="date">
<param name="min">2010-01-01</param>
<message>
(XML)graduation is date and greater than ${min}
</message>
</field-validator>
</field>
</validators>
RegistAction-validation2.xml<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<validator type="requiredstring">
<param name="fieldName">username</param>
<message>(XML2)username is not blank</message>
</validator>
<validator type="requiredstring">
<param name="fieldName">password</param>
<message>(XML2)password is not blank</message>
</validator>
<validator type="required">
<param name="fieldName">age</param>
<message>(XML2)age is not blank</message>
</validator>
<validator type="required">
<param name="fieldName">birthday</param>
<message>(XML2)birthday is not blank</message>
</validator>
<validator type="required">
<param name="fieldName">graduation</param>
<message>(XML2)graduation is not blank</message>
</validator>
<validator type="stringlength">
<param name="fieldName">username</param>
<param name="minLength">2</param>
<param name="maxLength">5</param>
<message>(XML2)username is ${minLength}~${maxLength}</message>
</validator>
<validator type="stringlength">
<param name="fieldName">password</param>
<param name="minLength">3</param>
<param name="maxLength">6</param>
<message>(XML2)password is ${minLength}~${maxLength}</message>
</validator>
<validator type="fieldexpression">
<param name="fieldName">repassword</param>
<param name="expression">
<![CDATA[(password==repassword)]]>
</param>
<message>(XML2)two password should be the same!</message>
</validator>
<validator type="int">
<param name="fieldName">age</param>
<param name="min">20</param>
<param name="max">100</param>
<message>(XML2)age is ${min}~${max}</message>
</validator>
<validator type="date">
<param name="fieldName">birthday</param>
<param name="min">2000-01-01</param>
<message>
(XML2)birthday is date and greater than ${min}
</message>
</validator>
<validator type="date">
<param name="fieldName">graduation</param>
<param name="min">2010-01-01</param>
<message>
(XML2)graduation is date and greater than ${min}
</message>
</validator>
</validators>
同时字段校验和非字段校验这两种方法也可以混搭使用,即在同一个校验配置文件中配置这两种。
校验器名词type可选值可以通过查找xwork的jar包下的default.xml,位置在:展开xwork-core-2.3.24.1.jar->com.opensymphony.xwork2.validator.validators.default.xml。
default.xml<?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">
<!-- START SNIPPET: validators-default -->
<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"/>
</validators>
<!-- END SNIPPET: validators-default -->
文件中name就是校验器名称,对应的就是type可选值,class是校验器对应的实现类,校验器配置的param参数就是实现类中的属性。
以下是实例程序运行结果:
校验的执行顺序:内置类型转换->XML校验器校验->validateToRegist->validate->toRegist
校验文件中message的国际化方式:
在Action同级目录下添加一个ActionName.properties文件,定义key-value,然后,在message标签的key属性中填充key值,例如:
结果如下:
这时可以给定义多个ActionName.properties文件以适应浏览器使用的不同语言包,例如:ActionName_en_US.properties、ActionName_zh_CN.properties,这样程序就会根据浏览器选择的语言包自适应提示信息,如果找不到对应语言包的properties文件,将使用默认ActionName.properties的配置。
运行结果:
原理解析:
校验器基类FieldValidatorSupport,是所有校验器的父类,提供了fieldName和type属性。
FieldValidatorSupport.classpackage com.opensymphony.xwork2.validator.validators;
import com.opensymphony.xwork2.validator.FieldValidator;
/**
* Base class for field validators.
*
* @author Jason Carreira
*/
public abstract class FieldValidatorSupport extends ValidatorSupport implements FieldValidator {
private String fieldName;
private String type;
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
public String getFieldName() {
return fieldName;
}
@Override
public void setValidatorType(String type) {
this.type = type;
}
@Override
public String getValidatorType() {
return type;
}
}
校验器stringlength,对应com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator,继承自FieldValidatorSupport,提供以下属性可配置:
private boolean trim = true;
private int maxLength = -1;
private int minLength = -1;
private String maxLengthExpression;
private String minLengthExpression;
private String trimExpression;
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
String val = (String) getFieldValue(fieldName, object);
if (val == null || val.length() <= 0) {
// use a required validator for these
return;
}
if (isTrim()) {
val = val.trim();
if (val.length() <= 0) {
// use a required validator
return;
}
}
int minLengthToUse = getMinLength();
int maxLengthToUse = getMaxLength();
if ((minLengthToUse > -1) && (val.length() < minLengthToUse)) {
addFieldError(fieldName, object);
} else if ((maxLengthToUse > -1) && (val.length() > maxLengthToUse)) {
addFieldError(fieldName, object);
}
}
默认trim为true,即校验字符串类型时默认去除前后空格,maxLength和minLength默认值-1,不判断最大最小长度。
由此可见,通过继承FieldValidatorSupport类,我们也可以自定义校验器,从而实现更符合我们业务逻辑的校验。
另外在校验过程中还要注意一些问题:
多方法Action情况如何使用配置文件进行校验?
Action可能会有多个方法,例如我们上面的toRegist,如果存在多方法的情况下,需要进行校验的信息肯定是不一样的,一个配置文件肯定解决不了问题,所有在多方法需要校验的情况下,我们需要在Action同级别的目录下,创建命名规则如下的配置文件:ActionName-method-validation.xml 配置文件,比如:RegisterAction中有个toRegist()方法需要校验,可以创建RegisterAction-toRegist-validation.xml 配置文件进行配置,这个文件就会针对toRegist()方法进行校验,但是需要注意的是:在调用toRegist()方法时,使用RegisterAction-toRegist-vaidation.xml文件进行校验后,还会调用RegisterAction-vaidation.xml 文件进行校验(这点和编码方式校验中validate和validateToRegist有些相似),这样的话,肯定会产生干扰,为了解决这样问题,可以在开发过程中,不提供RegisterAction-validtion.xml 这样的文件,对于execute()方法,可以提供一个RegisterAction-execute-validation.xml 文件进行配置校验。