struts学习4---XML输入校验框架

背景:1个Action类,名称为RegisterAction,其中包含的属性有:

string username;

int age;

string password;

string repassword;

date birthday;

date graduation;

 

需求: 如今需要对以上的这些属性进行输入校验,

1:所有都不准为空,

2:username, password. repassword都至少是6位,最多10位。

3:对birhtday,以及graduation 都给出一个明显的期限例如都是从xxxx/x/xx----yyyyy/yy/yy之内有效。

4.:age只能是1-150之间。

 

理论:

struts2包含了XML的输入校验框架,其中在Referenced Libraries中的xwork-core-2.1.6.jar——————>中的com.opensymphony.xwork2.validator.validators中就包含里所有相关的类文件。查看其中的default.xml对理解很有帮助

 

操作:

1. 在RegisterAction同处的目录下,建立一个xml文件,名为RegisterAction-validation.xml(其中-validation.xml是固定的写法,“-”的左边是相对应的Action类的类名)。

 

2. 打开这个xml文件,添加对应的DTD:

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

这个不用记,直接可以到下载下来的struts2文件的app文件中的例子里copy.   用firefox点击上述蓝色的网址直接查看DTD文档。

 

3. 在XML校验文件中,为RegisterAction.class中的每一个属性编写代码,其中代码如下:

<validators>
	<field name="username">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>username should not be blank!</message>
		</field-validator>

		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">10</param>
			<message>
				username shuold be between ${minLength} and ${maxLength}
			</message>
		</field-validator>
	</field>


	<field name="password">
		<field-validator type="requiredstring">
			<message>password should not be ignored!</message>
		</field-validator>

		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">10</param>
			<message>
				password shuold be between ${minLength} and ${maxLength}
			</message>
		</field-validator>
	</field>
	
	
	<field name="repassword">
		<field-validator type="requiredstring">
			<message>password should not be ignored!</message>
		</field-validator>

		<field-validator type="stringlength">
			<param name="minLength">6</param>
			<param name="maxLength">10</param>
			<message>
				repassword shuold be between ${minLength} and ${maxLength}
			</message>
		</field-validator>
	</field>
	
	
	<field name="age">
		<field-validator type="int">
			<param name="min">1</param>
			<param name="max">150</param>
			<message>
				The age should be the range of ${min} to ${max} years old!
			</message>
		</field-validator>
	</field>
	
	
	<field name="birthday">
		<field-validator type="required">
			<message>The birthday should bot be ignored!</message>
		</field-validator>

		<field-validator type="date">
			<param name="min">1900-01-01</param>
			<param name="max">2010-12-30</param>
			<message>
				The birthday should be the range of ${min} to ${max}
			</message>
		</field-validator>
	</field>
	
	<field name="graduation">
		<field-validator type="required">
			<message>The graduation has to be filled!</message>
		</field-validator>

		<field-validator type="date">
			<param name="min">1900-01-01</param>
			<param name="max">2010-12-30</param>
			<message>
				The graduation date should be the range of ${min} to ${max}
			</message>
		</field-validator>
	</field>
	
</validators>

 

结果:

运行之后,结果都正常,在出现错误输入的时候,有相应的提示出现!

你可能感兴趣的:(xml,框架,struts,firefox)