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>

 

结果:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值