Struts2提供了一种基于框架的校验方式,将校验规则保存在特定文件中,使Action和校验分离,从而提高系统的可维护性和可扩展性。
- 定义校验规则
Struts2扩家通过杜集区校验文件中定义的验证规则对输入数据进行校验,校验文件需要和Action类在相同的目录下。文件名为:ActionName-validation.xml或者ActionName-alias-validation.xml。如Action类为:loginAction,在struts.xml文件中的配置名字为login。则校验文件为:LoginAction-validateion.xml或者LoginAction-login-validation.xml,并且该校验文件应该和LoginAction.java放在同一个目录下。
- 字段校验器配置
Struts2提供两种方式来配置校验规则:字段校验器配置飞哥和非字段校验器。这两种风格的配置没有本质的区别,只是元素组织规则的方式和关注点不同。
字段校验先指定哪个字段要校验,再指定哪些校验器来校验该字段;非字段校验先指定校验器,再来指定用该校验器校验哪些字段,该方式也别称为全局字段校验。
字段校验器配置风格
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<!-- 被校验的字段userName -->
<field name="userName">
<!-- 校验器类型名 requiredString -->
<field-validator type="requiredstring">
<!-- 校验失败时的错误信息提示 -->
<message key="userName">用户名不能为空</message>
</field-validator>
<field-validator type="stringlength">
<!-- 校验时的参数名minLength与参数值4 -->
<param name="minLength">4</param>
<param name="maxLength">20</param>
<message>用户姓名的长度为${minLength}到${maxLength}之间</message>
</field-validator>
</field>
</validators>
非字段校验器配置风格
<?xml version="1.0" encoding="gb2312" ?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<!-- 校验器类型名 requiredString -->
<validator type="stringlength">
<!-- 被校验的字段userName -->
<param name="fieldName">userName</param>
<!-- 校验时的参数名minLength与参数值4 -->
<param name="minLength">4</param>
<param name="maxLength">20</param>
<message>用户姓名的长度为${minLength}到${maxLength}之间</message>
</validator>
<validator type="requiredstring">
<param name="fieldName">userName</param>
<message>用户名不能为空</message>
</validator>
</validators>
- 国际化校验配置
/src下创建国际化资源文件
globalMessages_zh_CN.properties
userName=\u56FD\u9645\u5316\u4FE1\u606F\uFF1A\u8054\u7CFB\u4EBA\u540D\u79F0\u4E0D\u80FD\u4E3A\u7A7A\uFF01
struts.xml配置国际化参数
<constant name="struts.custom.i18n.resources" value="globalMessages" />
在message标签中如下定义
<message key="userName"></message>
- 基本数据类型的检验实例
通过以下的实例可知。
1.strus2的校验顺序为:Struts2框架首先检索父类Action,然后检索子类Action。在检索Action类时,先搜素Action-validation.xml文件,然后搜素ActionName-alials-validation.xml文件。
2.基本的校验规则如何书写。
FatherAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
public class FatherAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 5698419467157626756L;
private String fatherName;
private int fatherAge;
public String getFatherName() {
return fatherName;
}
public void setFatherName(String fatherName) {
this.fatherName = fatherName;
}
public int getFatherAge() {
return fatherAge;
}
public void setFatherAge(int fatherAge) {
this.fatherAge = fatherAge;
}
public String execute() {
return SUCCESS;
}
}
SonAction.java
package action;
public class SonAction extends FatherAction{
private String sonName;
private int sonAge;
public String getSonName() {
return sonName;
}
public void setSonName(String sonName) {
this.sonName = sonName;
}
public int getSonAge() {
return sonAge;
}
public void setSonAge(int sonAge) {
this.sonAge = sonAge;
}
public String execute(){
return SUCCESS;
}
}
FatherAction-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="fatherName">
<field-validator type="requiredstring">
<message>FatherAction:fatherName不能为空!</message>
</field-validator>
</field>
</validators>
FatherAction-order-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="fatherAge">
<field-validator type="int">
<param name="min">25</param>
<param name="max">100</param>
<message>FatherAction-order:fatherAge为${min}-${max}之间</message>
</field-validator>
</field>
</validators>
SonAction-validation.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="sonName">
<field-validator type="requiredstring">
<message>SonAction:sonName不能为空!</message>
</field-validator>
</field>
</validators>
SonAction-order-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="sonAge">
<field-validator type="int">
<param name="min">1</param>
<param name="max">75</param>
<message>SonAction-order:sonAge为${min}-${max}之间</message>
</field-validator>
</field>
</validators>
struts.xml配置
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8" />
<constant name="struts.custom.i18n.resources" value="globalMessages" />
<package name="default" extends="struts-default">
<action name="order" class="action.SonAction">
<result name="success">/fatherSon.jsp</result>
<result name="input">/fatherSon.jsp</result>
</action>
</package>
</struts>
fathSon.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>校验顺序</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body >
<jsp:include page="index.html"></jsp:include>
<s:fielderror></s:fielderror>
<s:form action="order">
<s:textfield name="fatherName" label="父类名称" />
<s:textfield name="sonAge" label="子类年龄" />
<s:textfield name="fatherAge" label="父类年龄" />
<s:textfield name="sonName" label="子类名称" />
<s:submit value="提交"></s:submit>
</s:form>
</body>
</html>