【Struts2十】校验表达二:使用xml配置validator来校验表单

前端之家收集整理的这篇文章主要介绍了【Struts2十】校验表达二:使用xml配置validator来校验表单前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
手动校验还是不大优雅,Struts2还提供了另外一种xml配置的方式校验表单!

在Action的同目录下添加一个xml文件文件名严格限制为:Action类名-Action别名_方法名-validation.xml:
如:ValidateXMLAction-validateXMLAction_aaa-validation.xml

1.ValidateXMLAction:
public classValidateXMLActionextendsActionSupport{
private Stringusername;
private Stringpassword;
private Integerage;
public Integer getAge() {
returnage;
}
public voidsetAge(Integer age) {
this.age= age;
}
public String getUsername() {
returnusername;
}
public voidsetUsername(String username) {
this.username= username;
}
public String getPassword() {
returnpassword;
}
public voidsetPassword(String password) {
this.password= password;
}
public String testValidate(){
return"success";
}
public String aaa(){
return"success";
}
}

2.需要校验的表单jsp:
validate_xml.jsp:
<%@ pagelanguage="java"import="java.util.*"pageEncoding="UTF-8"%>
<%@ tagliburi="/struts-tags"prefix="s"%>

<! DOCTYPE HTMLPUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
< html >
<head>

</head>
<body>
< s:fielderror ></s:fielderror>
< form action="validate/validateXMLAction_aaa.action"method="post">
用户名: <inputtype="text"name="username"><br>
密码: <inputtype="text"name="password"><br>
年龄: <inputtype="text"name="age"><br>
<inputtype="submit">
</ form >
</body>
</ html >

3.配置validator的xml文件
ValidateXMLAction-validateXMLAction_aaa-validation.xml:
<? xml version="1.0"encoding="UTF-8"?>
<! DOCTYPE validatorsPUBLIC
"-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
< validators >
<!--
指明action中要校验的属性
-->
< field name="username">
<!--
field-validator指定校验器
type为校验规则
-->
<field-validatortype="requiredstring">
<paramname="trim">true</param>
<message><![CDATA[用户名不能为空]]></message>
</field-validator>
</ field >
< field name="password">
<field-validatortype="requiredstring">
<paramname="trim">true</param>
<message><![CDATA[密码不能为空]]></message>
</field-validator>
<field-validatortype="regex">
<paramname="trim">true</param>
<paramname="expression"><![CDATA[^[a-zA-Z0-9]{6,12}$]]></param>
<message><![CDATA[密码长度应该在6到12之间]]></message>
</field-validator>
</ field >
< field name="age">
<!--
<field-validatortype="int">
<param name="max">100</param>
<param name="min">1</param>
<param name="trim">true</param>
<message><![CDATA[年龄应该在1到100之间]]></message>
</field-validator>
-->
<!-- 使用自定义的校验器 -->
<field-validatortype="age">
<message><![CDATA[年龄应该大于1]]></message>
</field-validator>
</ field >
</ validators >

4.上述xml文档中用到了自定义校验age的校验器,需要实现以下步骤才能正常使用:
(1).src根目录下新建一个validators.xml的文件
<? xml version="1.0"encoding="UTF-8"?>
<! DOCTYPE validatorsPUBLIC
"-//Apache Struts//XWork Validator Config 1.0//EN"
"http://struts.apache.org/dtds/xwork-validator-config-1.0.dtd">

<!-- START SNIPPET:validators-default -->
< validators >
<validatorname="age"class="cn.itcast.struts2.action.validate.AgeValidate"></validator>
</ validators >
(2).写一个AgeValidate的类:
package cn.itcast.struts2.action.validate;

import com.opensymphony.xwork2.validator.ValidationException;
import com.opensymphony.xwork2.validator.validators.FieldValidatorSupport;

public classAgeValidateextendsFieldValidatorSupport{
public voidvalidate(Object object)throwsValidationException {
System. out.println("错误信息:"+this.getMessage(object));
String fieldName = this.getFieldName();
System. out.println("字段名称:"+fieldName);
Object o = this.getFieldValue(fieldName,object);
if(oinstanceofjava.lang.Integer){
Integer len = (Integer)o;
if(len<1){
this.addFieldError(fieldName,object);
}
}
}

}

5.struts-validate.xml
<? xml version="1.0"encoding="UTF-8"?>
<! DOCTYPE strutsPUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
< struts >
< package name="validate"namespace="/validate"extends="struts-default">
<actionname="validateXMLAction_*"method="{1}"class="cn.itcast.struts2.action.validate.ValidateXMLAction">
<resultname="input">/validate/validate_xml.jsp</result>
<resultname="success">/validate/success.jsp</result>
</action>
</ package >
</ struts >

猜你在找的XML相关文章