Struts1.x系列教程(12):Validator验证框架的内置标准验证

xiaoxiao2022-08-13  187

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C04%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><!--[if !mso]> <style> st1":*{behavior:url(#ieooui) } </style> <![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} table.MsoTableGrid { border:solid windowtext 1.0pt; text-align:justify; text-justify:inter-ideograph; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]--> 本文为原创,如需转载,请注明作者和出处,谢谢! 上一篇: Struts1.x系列教程(11):Validator验证框架高级应用

Struts2教程4:使用validate方法验证数据

Struts2教程5:使用Validation框架验证数据

下面列出了Validator框架的预定义验证(Struts的版本号为1.2.9)。

Validator

变量

引发条件

required

字段只有空格

validwhen

test

test条件失败(详见下一节)

minlength

minlength

字段的字符数小于minlength

maxlength

maxlength

字段的字符数大于maxlength

mask

mask

字段值不匹配mask所指的个正则表达式

byte、short、integer、long、float、double

字段值无法转换为这些数据类型

date

datePattern 或

datePatternStrict

字值值不能按着指定的格式转换为日期类型

intRangefloatRangedoubleRange

min、max

字段值不在指定的范围内

creditCard

字段值不是一个信誉卡号

email

字段值不是一个合法的email

url

allowallschemes、

allow2slashes、

nofragments、

schemes

字段不是一个URL

关于Validator框架标准验证的详细信息,读者可以访问如下的URL:

http://struts.apache.org/1.2.9/userGuide/dev_validator.html

下面来举一个validwhen验证的例子。 validwhen可以验证当前属性和其他属性的关系,如在验证密码时,要保证两次输入的密码一致,验证代码如下: <!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < field property ="password1" depends ="validwhen" > < msg name ="validwhen" key ="error.password1.confirmation" /> < var > < var-name > test </ var-name > < var-value > (password1!=null)and(*this*==password) </ var-value > </ var > </ field > 使用validwhen也可以验证嵌套和索引属性,如下面的代码所示: <!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < field property ="field1" depends ="validwhen" > < msg name ="validwhen" key ="error.field.message" /> < var > < var-name > test </ var-name > < var-value > (*this*==field2.name)and(*this*!=field3[1]) </ var-value > </ var > </ field > <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <meta name="ProgId" content="Word.Document"> <meta name="Generator" content="Microsoft Word 11"> <meta name="Originator" content="Microsoft Word 11"> <link rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C04%5Cclip_filelist.xml"> <!--[if gte mso 9]><xml> Normal 0 7.8 磅 0 2 false false false MicrosoftInternetExplorer4 </xml><![endif]--><!--[if gte mso 9]><![endif]--><style> <!-- /* Font Definitions */ &#64;font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} &#64;font-face {font-family:""&#64;宋体"; panose-1:2 1 6 0 3 1 1 1 1 1;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; font-size:10.5pt; font-family:"Times New Roman";} /* Page Definitions */ &#64;page {} &#64;page Section1 {size:612.0pt 792.0pt; margin:72.0pt 90.0pt 72.0pt 90.0pt;} div.Section1 {page:Section1;} --> </style> <!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable { mso-style-parent:""; font-size:10.0pt; font-family:"Times New Roman";} </style> <![endif]-->

其中*this*表示当前属性的值。 下一篇:Struts1.x系列教程(13):国际化

国内最棒的Google Android技术社区(eoeandroid),欢迎访问! 《银河系列原创教程》发布 《Java Web开发速学宝典》出版,欢迎定购 相关资源:敏捷开发V1.0.pptx
转载请注明原文地址: https://www.6miu.com/read-4974617.html

最新回复(0)