Struts 2 基于 XML 的验证器

以下是 Struts2 中可用的各种类型的字段级别和非字段级别验证的列表

date 验证器

<field name = "birthday">
   <field-validator type = "date">
      <param name = "min">01/01/1990</param>
      <param name = "max">01/01/2000</param>
      <message>
         生日必须在 ${min} 和 ${max} 之间
      </message>
   </field-validator>
</field>

double 验证器

<field name = "percentage">
   <field-validator type = "double">
      <param name = "minExclusive">0.123</param>
      <param name = "maxExclusive">99.98</param>
      <message>
         百分比需要在 ${minExclusive} 和 ${maxExclusive}之间
      </message>
   </field-validator>
</field>

email 验证器

<field name = "myEmail">
   <field-validator type = "email">
      <message>
         必须提供有效的电子邮件
      </message>
   </field-validator>
</field>

expression 验证器

基于提供的正则表达式进行验证的非字段级别验证器。

<field-validator type = "expression">
   <param name = "expression">....</param>
   <message>
      未能满足 Ognl 表达式....
   </message>
</field-validator>

int 验证器

<field name = "age">
   <field-validator type = "int">
      <param name = "min">5</param>
      <param name = "max">50</param>
      <message>
         年龄需要在 ${min} 和 ${max} 之间
      </message>
   </field-validator>
</field>

regex 验证器

<field name = "myStrangePostcode">
   <field-validator type = "regex">
      <param name = "fieldName">myStrangePostcode</param>
         <param name = "expression">
         <![CDATA[([aAbBcCdD][123][eEfFgG][456])]]>
      </param>
   </field-validator>
</field>

required 验证器

<field name = "username">
   <field-validator type = "required">
      <message>
         用户名不能为空
      </message>
   </field-validator>
</field>

requiredstring 验证器

<field name = "username">
   <field-validator type = "requiredstring">
     <param name = "trim">true</param>
     <message>
         用户名不能为空
      </message>
   </field-validator>
</field>

stringlength 验证器

<field name = "username">
   <field-validator type = "stringlength">
      <param name = "minLength">10</param>
      <param name = "maxLength">10</param>
      <param name = "trim">true</param>
      <message>
         用户名长度必须为 10 个字符
      </message>
   </field-validator>
</field>

url 验证器

<field-validator type = "url">
   <param name = "fieldName">myHomePageURL</param>
   <message>
    无效的主页网址
   </message>
</field-validator>

查看笔记

扫码一下
查看教程更方便