Struts2输入校验(XML方式)

本章主要介绍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 文件进行配置校验。

时间: 2024-10-14 11:12:23

Struts2输入校验(XML方式)的相关文章

Struts2之基于XML方式进行数据输入校验

使用基于XML配置方式实现输入校验时,action也要继承ActionSupport,并且提供校验文件,校验文件和action类放在同一个包下,文件的取名格式为:ActionClassName-validate.xml,其中ActionClassName为简单类名,-validate为固定写法. 输入页面index.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-

Struts2输入校验(编码方式)

struts2对用户输入数据的校验方法有两种方式,一种是通过编码的方式,另一种则是通过使用XML配置方式. 本章主要介绍struts2编码方式的输入校验.以下将结合一个实例程序进行说明. 代码结构: 关键代码: RegistAction.javapackage com.alfred.regist.action; import java.text.SimpleDateFormat; import java.util.Date; import com.opensymphony.xwork2.Acti

Struts2输入校验之validate输入校验方式

一.在Web系统项目中有大量的视图页面需要用户自行输入很多数据.这些数据的类型有很多种.为了防止某些客户的恶意输入以及对Web项目的恶意破坏,必须引入输入校验,像Windows操作系统的防火墙一样把一些垃圾数据过滤掉,挡在Web系统之外.接下来就来介绍一下validate输入校验方式: 1.validate方法进行输入校验:这里直接附上一个简单的用户注册功能具体介绍利用validate方法对数字.字符串.日期等类型数据进行输入校验方式介绍: (1).首先,新建一个Struts2项目InputVa

笔记:Struts2 输入校验

Struts2的输入校验包含了客户端校验和服务器端校验,通过编写校验规则文件来实现输入校验,需要增加 Convention 插件,将 struts2-convention-plugin-2.3.31.jar 复制到 WEB-INF/lib 目录中 校验规则文件 校验规则文件命名格式:ActionName-validation.xml,其中 ActionName 为校验的 Action名称,后面的 -validation.xml 为固定字符串,并将校验规则文件保持到 Action 相同的路径下 S

struts2对action中的方法进行输入校验---xml配置方式(3)

上面两篇文章已经介绍了通过编码java代码的方式实现action方法校验,这里我们介绍另外一种方式:xml配置文件 首先我们来看一个例子: ValidateAction.java: package com.itheima.action; import com.opensymphony.xwork2.ActionSupport; public class ValidateAction extends ActionSupport { private String username; private

struts2输入校验(附ActionName-validate.xml文件中正则表达式不起作用的解决方案),PS:有点问题希望知道的人指点下

PS:最后附上遇到的疑惑,希望知道的人指点下 前台页面(附代码): <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or

Struts2输入校验

regist.jsp <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = reques

struts2 学习(七) ----- 输入校验

struts2输入校验(服务器端验证) 一.使用validate方法进行输入校验 步骤:1) 重写Action的validate()函数 2) 在struts2.xml中配置Action的别名为"input"的result 3) 在输出页面上显示错误:使用<s:fielderror>标签 1. 新建LoginAction.java的时候要继承ActionSupport类: 2. 配置: 3.使用<s:fielderror>标签 二.使用struts2校验框架实现

struts2对action中的方法进行输入校验(2)

struts2输入校验流程: 1.类型转换器对请求参数执行类型转换,并把转换后的值赋给aciton中的属性 2.如果在执行类型转换的过程中出现异常,系统会将异常信息保存到ActionContext, conversionError拦截器将异常信息添加到fieldErrors里,不管类型转换是否出现异常,都会进入第三步 3.系统通过反射技术先调用action的validateXXX方法 4.再调用aciton中的validate方法 5.经过上述的4步,如果系统中的fieldErrors存在错误信