[标题]:[原]Struts2校验框架
[时间]:2009-7-27
[摘要]:校验框架配置、客户端校验
[关键字]:浪曦视频,Struts2应用开发系列,WebWork,Apache,validation
[环境]:struts-2.1.6、JDK6、MyEclipse7、Tomcat6
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys

[正文]:
    Struts校验框架提供了一种不用编写代码,只需修改配置文件即可对输入进行校验的功能。代码方式校验,即重写validate()方法。实际应用中,validate()与校验框架一般只选其一

1、校验框架配置
    在RegisterAction.java相同目录新建校验框架配置文件RegisterAction-validation.xml。RegisterAction-validation.xml的DTD可以复制struts自带的示例struts2-mailreader-2.1.6中的DTD:
<!DOCTYPE validators PUBLIC
          "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

xwork-validator-1.0.2.dtd:
<?xml version="1.0" encoding="UTF-8"?>

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


<!ELEMENT validators (field|validator)+>

<!ELEMENT field (field-validator+)>
<!ATTLIST field
    name CDATA #REQUIRED
>

<!ELEMENT field-validator (param*, message)>
<!ATTLIST field-validator
    type CDATA #REQUIRED
    short-circuit (true|false) "false"
>

<!ELEMENT validator (param*, message)>
<!ATTLIST validator
    type CDATA #REQUIRED
    short-circuit (true|false) "false"
>

<!ELEMENT param (#PCDATA)>
<!ATTLIST param
    name CDATA #REQUIRED
>

<!ELEMENT message (#PCDATA)>
<!ATTLIST message
    key CDATA #IMPLIED
>


RegisterAction-validation.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
          "-//OpenSymphony Group//XWork Validator 1.0.2//EN"
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
 
 <validators>
 <!--  
     与field等价的validator
     <validator type="requiredstring">
         <param name="fieldName">name</param>
         <param name="trim">true</param>
         <message>name required</message>
     </validator>
     <validator type="stringlength">
         <param name="fieldName">name</param>
             <param name="trim">true</param>
             <param name="minLength">6</param>
             <param name="maxLength">10</param>
             <message>name length range : ${minLength} ~ ${maxLength}</message>
     </validator>
 -->    
     <field name="name">
         <field-validator type="requiredstring" >
             <param name="trim">true</param>
             <message>name required</message>
         </field-validator>
         <field-validator type="stringlength">
             <param name="trim">true</param>
             <param name="minLength">6</param>
             <param name="maxLength">10</param>
             <message>name length range : ${minLength} ~ ${maxLength}</message>
         </field-validator>
     </field>
     
     <field name="age">
         <field-validator type="conversion" short-circuit="true">
             <message>the field age conversion error</message>
         </field-validator>
         <field-validator type="int">
             <param name="min">1</param>
             <param name="max">150</param>
             <message>age range: ${min} ~ ${max}</message>
         </field-validator>
     </field>
     
     <field name="birthday">
         <field-validator type="required">
             <message>birthday required</message>
         </field-validator>
         <field-validator type="date">
             <param name="min">2009-01-01</param>
             <param name="max">2009-12-31</param>
             <message>birthday range: ${min} ~ ${max}</message>
         </field-validator>
     </field>
     
     <field name="email">
         <field-validator type="email">
             <message>invalid email address</message>
         </field-validator>
     </field>
 </validators>

说明:
a.校验框架有两种校验方式:field和validator。
field校验:先指定需要校验的属性,再指定校验方式。validator校验:先指定校验方式,再指定需要校验的属性。两种方式是等价的。

field校验:
<field name="name">
    <field-validator type="requiredstring" >
        <param name="trim">true</param>
        <message>name required</message>
    </field-validator>
    <field-validator type="stringlength">
        <param name="trim">true</param>
        <param name="minLength">6</param>
        <param name="maxLength">10</param>
        <message>name length range : ${minLength} ~ ${maxLength}</message>
    </field-validator>
</field>

validator校验:
<validator type="requiredstring">
    <param name="fieldName">name</param>
    <param name="trim">true</param>
    <message>name required</message>
</validator>
<validator type="stringlength">
    <param name="fieldName">name</param>
    <param name="trim">true</param>
    <param name="minLength">6</param>
    <param name="maxLength">10</param>
    <message>name length range : ${minLength} ~ ${maxLength}</message>
</validator>

b.<field-validator type="requiredstring" >中的type及其对应的xwork类可以在xwork-2.1.2.jar/com.opensymphony.xwork2.validator.validators/default.xml中查找到。
default.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//OpenSymphony Group//XWork Validator Config 1.0//EN"
        "http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">

<!-- START SNIPPET: validators-default -->
<validators>
    <validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>
    <validator name="requiredstring" class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>
    <validator name="int" class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>
    <validator name="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
    <validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
    <validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
    <validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
    <validator name="expression" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>
    <validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/>
    <validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>
    <validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/>
    <validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
    <validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/>
    <validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/>
    <validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>
    <validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/>
</validators>
<!--  END SNIPPET: validators-default -->

c. 对于校验<field-validator type="requiredstring" >:String用requiredstring,其它类型用required。
d. 验证错误将默认产生FieldError。
e. short-circuit="true" : 一个校验失败,是否是继续执行后续校验。
f. 对于相同属性的FieldError,validate()方法中的验证和RegisterAction-validation.xml中的验证都会执行,并且FieldError都会显示。
执行顺序: RegisterAction-validation.xml => validate()。


2、特定于业务逻辑的局部校验:
对于struts.xml中<action>的method="register"指定的业务方法register(),使用校验RegisterAction-register-validation.xml(与RegisterAction.java在同一目录下)。同时,RegisterAction-validation.xml会被执行。执行顺序:  RegisterAction-validation.xml => RegisterAction-register-validation.xml

3、Struts2客户端校验
    设置struts标签<s:form>中validate="true"。这样就会根据RegisterAction-validation.xml生成页面Javascript,在客户端执行校验。
 
 注意:
 a.struts标签不能使用simple主题
 b.Struts2客户端校验功能较弱,一般不用。

4、FieldError和ActionError的实现
a.真正存放field级别错误信息的对象是LinkedHashMap。
b.该LinkedHashMap的key是String类型的,value是ArrayList类型,所以同一个key可以对应多个错误消息。
c.对于Action级别的错误信息,实际上是放置在ArrayList中的。
    
[参考资料]:
    《浪曦视频之Struts2应用开发系列》