这节介绍如何在SSH框架中使用Validator框架,Validator框架采用基于xml文件来配置验证规则,它主要依赖两个jar包:commons-validator-1.3.0.jar和oro-2.0.8.jar,要在SSH使用Validator框架,必须把这两个jar包引用进来,其实Struts框架已包含这两个jar包,所以我们不用再单独引入了。
要在SSH框架中使用Validator,更确切的说是在Struts框架中使用,可以采用插件机制把Validator加入到框架,这需要修改原来的struts-config.xml文件,增加配置ValidatorPlugIn插件,代码如下:
validator-rules.xml文件是Validator框架自带的,它定义了一些常有的校验规则,而validation.xml是我们自己定义的,它针对某个应用,内容如下:
<? xml version="1.0" encoding="iso-8859-1" ?>
<! DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd" >
< form-validation >
< formset >
< form name ="registerForm" >
< field property ="register.username" depends ="required" >
< arg key ="register.username" position ="0" />
</ field >
< field property ="register.password" depends ="required,minlength" >
< arg key ="register.password" position ="0" />
< arg name ="minlength" key ="${var:minlength}" resource ="false" position ="1" />
< var >
< var-name > minlength </ var-name >
< var-value > 6 </ var-value >
</ var >
</ field >
< field property ="register.email" depends ="required,email" >
< arg key ="register.email" position ="0" />
</ field >
</ form >
</ formset >
</ form-validation >
此外,我们还要修改struts-config.xml文件,修改内容见粗体:
修改错误显示的样式,我们在register.jsp中,替换原来<html/errors>为
注意在页面上加入
这样,我们运行程序,校验失败的效果如图:
要在SSH框架中使用Validator,更确切的说是在Struts框架中使用,可以采用插件机制把Validator加入到框架,这需要修改原来的struts-config.xml文件,增加配置ValidatorPlugIn插件,代码如下:
<
plug-in
className
="org.apache.struts.validator.ValidatorPlugIn"
>
< set-property property ="pathnames" value ="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml" />
< set-property property ="stopOnFirstError" value ="true" />
</ plug-in >
这样当应用启动时,Struts框架就会加载ValidatorPlugIn插件。
< set-property property ="pathnames" value ="/org/apache/struts/validator/validator-rules.xml,
/WEB-INF/validation.xml" />
< set-property property ="stopOnFirstError" value ="true" />
</ plug-in >
validator-rules.xml文件是Validator框架自带的,它定义了一些常有的校验规则,而validation.xml是我们自己定义的,它针对某个应用,内容如下:
<? xml version="1.0" encoding="iso-8859-1" ?>
<! DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd" >
< form-validation >
< formset >
< form name ="registerForm" >
< field property ="register.username" depends ="required" >
< arg key ="register.username" position ="0" />
</ field >
< field property ="register.password" depends ="required,minlength" >
< arg key ="register.password" position ="0" />
< arg name ="minlength" key ="${var:minlength}" resource ="false" position ="1" />
< var >
< var-name > minlength </ var-name >
< var-value > 6 </ var-value >
</ var >
</ field >
< field property ="register.email" depends ="required,email" >
< arg key ="register.email" position ="0" />
</ field >
</ form >
</ formset >
</ form-validation >
form元素的name属性指定要进行验证的表单,field元素的property属性指定要验证属性,depends定义了验证方式,在这里,我们定义了username属性不能为空,password属性不能为空,并且最小长度不能小于6,email属性必须是合法的邮件地址。定义验证方式之后,我们还必须修改资源文件registermessages.properties,增加以下内容:
# Errors
errors.header= < h3 >< fontcolor ="red" > ValidationError </ font ></ h3 > Youmustcorrectthefollowingerror(s)beforeproceeding:
errors.required={0}isrequired.
errors.minlength={0}cannotbelessthan{1}characters.
errors.email={0}isaninvalide-mailaddress.
这资源文件中{0}对应着<arg key="" position="0"/>,{1}对应着<arg key="" position="1"/>,以此类推,如果有验证错误产生,将用key的值替换{n}
errors.header= < h3 >< fontcolor ="red" > ValidationError </ font ></ h3 > Youmustcorrectthefollowingerror(s)beforeproceeding:
errors.required={0}isrequired.
errors.minlength={0}cannotbelessthan{1}characters.
errors.email={0}isaninvalide-mailaddress.
此外,我们还要修改struts-config.xml文件,修改内容见粗体:
//把原来的DynaActionForm 改为DynaValidatorForm
< form-beans >
< form-bean name ="registerForm"
type ="org.apache.struts.validator.DynaValidatorForm" >
< form-property name ="register" type ="test.register.entity.Register" />
</ form-bean >
</ form-beans >
//把原来的validate="false"改为validate="true"
< action-mappings >
< action name ="registerForm" path ="/register" scope ="request"
type ="test.register.action.RegisterAction" validate ="true"
input ="/register.jsp" >
< forward name ="success" path ="/registerSuccess.jsp" />
</ action >
</ action-mappings >
< form-beans >
< form-bean name ="registerForm"
type ="org.apache.struts.validator.DynaValidatorForm" >
< form-property name ="register" type ="test.register.entity.Register" />
</ form-bean >
</ form-beans >
//把原来的validate="false"改为validate="true"
< action-mappings >
< action name ="registerForm" path ="/register" scope ="request"
type ="test.register.action.RegisterAction" validate ="true"
input ="/register.jsp" >
< forward name ="success" path ="/registerSuccess.jsp" />
</ action >
</ action-mappings >
修改错误显示的样式,我们在register.jsp中,替换原来<html/errors>为
<
logic:messagesPresent
>
< bean:message key ="errors.header" />
< ul >
< html:messages id ="error" >
< li >< bean:write name ="error" /></ li >
</ html:messages >
</ ul >< hr />
</ logic:messagesPresent >
< bean:message key ="errors.header" />
< ul >
< html:messages id ="error" >
< li >< bean:write name ="error" /></ li >
</ html:messages >
</ ul >< hr />
</ logic:messagesPresent >
注意在页面上加入
<
%@ taglib
uri
="http://struts.apache.org/tags-logic"
prefix
="logic"
%
>
这样,我们运行程序,校验失败的效果如图: