在Struts中对用户输入信息的校验一般在FromBean中进行(除非需要访问数据库进行诸如登录信息的校验,因为这是Action的工作),本文将阐述如何在Struts中实现可配置的信息校验。
一、在FormBean中手工实现
最简单的方法是直接在FormBean中重写ActionForm类的validate方法,validate方法签名如下:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest req)
比如需要校验age字段必须填写数字:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest req){
ActionErrors errors = new ActionErrors();
String age = this.getAge();
if(!this.isNumber(age)){ // isNumber() is not implemented
errors.add(... , ...);
}
return errors;
}
在form提交后,容器会调用validate方法对表单数据进行校验,如果返回的ActionErrors为空(即校验通过),则将FormBean提交Action,否则重定向到提交form的页面。
这种方法实现简单,直观,容易测试、调试,但不可避免地存在以下缺点:
1、很难重用,导致重复开发
有很多校验逻辑在整个网站中是相同的,比如上述的数字校验,还有email校验、长度校验等等,而通过覆盖validate方法很难对这些校验过程进行重用,除非定义一些helper类封装校验方法(比如上述的isNumber())。而当需要为另一个FormBean加入相同的校验逻辑时必须重复地覆盖validate方法
2、难于扩展
当要对一个表单增、删、改校验逻辑时必须修改validate方法,重新打包、部署
3、不可配置
因为校验逻辑硬编码于class文件中,运行时不可能做到灵活地配置校验逻辑
因此,Struts中加入了另一种更灵活的校验机制:
二、使用Validator
Validator提供了一种基于xml配置文件的校验模型,要使用这一模型必须做如下实现:
1、FormBean继承org.apache.struts.validator.ValidatorForm而不是ActionForm
2、不覆盖validate方法
3、创建validator-rules.xml及validation.xml文件
validator-rules.xml定义了可用来配置的校验逻辑,如:
<form-validation>
<global>
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
<validator name="requiredif"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequiredIf"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
<validator name="validwhen"
msg="errors.required"
classname="org.apache.struts.validator.validwhen.ValidWhen"
method="validateValidWhen"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"/>
<validator name="minlength"
classname="org.apache.struts.validator.FieldChecks"
method="validateMinLength"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.minlength"
jsFunction="org.apache.commons.validator.javascript.validateMinLength"/>
<validator name="maxlength"
classname="org.apache.struts.validator.FieldChecks"
method="validateMaxLength"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.maxlength"
jsFunction="org.apache.commons.validator.javascript.validateMaxLength"/>
<validator name="mask"
classname="org.apache.struts.validator.FieldChecks"
method="validateMask"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.invalid"/>
<validator name="byte"
classname="org.apache.struts.validator.FieldChecks"
method="validateByte"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.byte"
jsFunctionName="ByteValidations"/>
<validator name="short"
classname="org.apache.struts.validator.FieldChecks"
method="validateShort"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.short"
jsFunctionName="ShortValidations"/>
<validator name="integer"
classname="org.apache.struts.validator.FieldChecks"
method="validateInteger"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.integer"
jsFunctionName="IntegerValidations"/>
<validator name="long"
classname="org.apache.struts.validator.FieldChecks"
method="validateLong"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.long"/>
<validator name="float"
classname="org.apache.struts.validator.FieldChecks"
method="validateFloat"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.float"
jsFunctionName="FloatValidations"/>
<validator name="double"
classname="org.apache.struts.validator.FieldChecks"
method="validateDouble"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.double"/>
<validator name="date"
classname="org.apache.struts.validator.FieldChecks"
method="validateDate"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.date"
jsFunctionName="DateValidations"/>
<validator name="intRange"
classname="org.apache.struts.validator.FieldChecks"
method="validateIntRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends="integer"
msg="errors.range"/>
<validator name="floatRange"
classname="org.apache.struts.validator.FieldChecks"
method="validateFloatRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends="float"
msg="errors.range"/>
<validator name="doubleRange"
classname="org.apache.struts.validator.FieldChecks"
method="validateDoubleRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends="double"
msg="errors.range"/>
<validator name="creditCard"
classname="org.apache.struts.validator.FieldChecks"
method="validateCreditCard"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.creditcard"/>
<validator name="email"
classname="org.apache.struts.validator.FieldChecks"
method="validateEmail"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.email"/>
<validator name="url"
classname="org.apache.struts.validator.FieldChecks"
method="validateUrl"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.url"/>
<!--
This simply allows struts to include the validateUtilities into a page, it should
not be used as a validation rule.
-->
<validator name="includeJavaScriptUtilities"
classname=""
method=""
methodParams=""
depends=""
msg=""
jsFunction="org.apache.commons.validator.javascript.validateUtilities"/>
<validator name="codeinput"
classname="consultII.web.utils.MyValidator"
method="validateCodeInput"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="errors.code"/>
<validator name="userinfo"
classname="consultII.web.utils.MyValidator"
method="validateUserInfo"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="errors.info"/>
</global>
</form-validation>
该配置文件的具体格式请查阅Struts的官方文档。可以看出其中对很多常用的校验逻辑进行了封装,比如数字校验(92行到103行),长度校验(33行到56行)等。
而validation.xml则是Validator的核心,其中可对各个FormBean中需要校验字段及校验逻辑进行灵活的配置:
<form-validation>
<formset>
<form name="login">
<field property="username" depends="required"/>
<field property="password" depends="required"/>
<field property="input" depends="required,codeinput"/>
</form>
<form name="search">
<field property="keyword" depends="required"/>
</form>
<form name="signin">
<field property="username" depends="required"/>
<field property="password" depends="required"/>
<field property="passagain" depends="required"/>
<field property="question" depends="required,userinfo"/>
<field property="answer" depends="required,userinfo"/>
<field property="age" depends="required,integer"/>
<field property="input" depends="required,codeinput"/>
<field property="email" depends="email"/>
</form>
<form name="ask">
<field property="content" depends="required"/>
</form>
</formset>
</form-validation>
具体格式可查阅官方文档,如上例,3到5行定义了需要对名为“loging”的FormBean的“username”、“password”分别进行“required”校验(即必填字段)。校验逻辑之间可以组合,如第6行的“required,codeinput”,定义了先进行required校验,通过了再进行codeinput校验。
与在validate方法内进行校验相比,使用Validator的优势就很明显了,可重用(从上述xml文件可以看出对很多字段都进行了required校验);可灵活配置、扩展,只需修改validation.xml文件就可以改变校验逻辑。
-- EOF --
除非注明(如“转载”、“[zz]”等),本博文章皆为原创内容,转载时请注明: 「转载自程序员的信仰©」
本文链接地址:在Struts中使用Validator实现可配置的信息校验(一)
Today on history:
【2008】iPhone破解:1.0.2 -> 1.1.3
【2007】女人节
发表回复