校验框架:双校验
使用校验框架:
1.在struts-config.xml中加入validate plug-in(copy)
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml"/> </plug-in>
|
2.在WEB-INF跟目录下,建立validation.xml,并描述校验规则
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "">
<form-validation> <formset> <form name="loginForm"> <field property="userName" depends="required,minlength,maxlength,integer"> <arg0 key="userName"/> <arg1 key="${var:minlength}" name="minlength" resource="false"/> <var> <var-name>minlength</var-name> <var-value>3</var-value> </var> <arg1 key="${var:maxlength}" name="maxlength" resource="false"/> <var> <var-name>maxlength</var-name> <var-value>8</var-value> </var> </field> <field property="passWord" depends="required,minlength,maxlength"> <arg0 key="passWord"/> <arg1 key="${var:minlength}" name="minlength" resource="false"/> <var> <var-name>minlength</var-name> <var-value>3</var-value> </var> <arg1 key="${var:maxlength}" name="maxlength" resource="false"/> <var> <var-name>maxlength</var-name> <var-value>8</var-value> </var> </field> </form> </formset> </form-validation>
|
3.copy error message to resource
userName=userName
passWord=passWord
errors.footer=</span>
errors.header=<span style="color:red;">
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address.
|
4.form类必须继承ValidatorForm 并且要干掉其中的validate方法!
OK 完成!
阅读(1491) | 评论(0) | 转发(0) |