欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Struts1.x系列教程(11):Validator驗證框架高

系統 1896 0
<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]-->

本文為原創,如需轉載,請注明作者和出處,謝謝!

上一篇 Struts1.x系列教程(10):Validator驗證框架入門

Struts2教程4:使用validate方法驗證數據

Struts2教程5:使用Validation框架驗證數據

《Struts1.x系列教程(10):Validator驗證框架入門》 一文中 給出的例子中的 name age email 都是簡單的屬性,如果是嵌套的屬性(也就是一個屬性的數據類型是另外一個帶有屬性的類),可以使用圓點 (.) 符號來訪問。如我們可以通過設置 <field> 元素的 property 屬性值為“ employee.salary ”來驗證 employee 屬性中的 salary 屬性。
如果要驗證的索引屬性,我們可以使用<field>元素的indexedListProperty屬性來指定這個索引屬性,如下面代碼如示:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < field indexedListProperty ="items" depends = > </ 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]-->在使用上述配置代碼后,Validator框架將會按著depends屬性中的約束來驗證items屬性的每一個值。我們還可以將property和indexedListProperty配合使用,如下面代碼所示:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < field indexedListProperty ="items" property ="employee.age" depends = > </ 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]-->

Validator 框架將根據上述的配置代碼來驗證 items 屬性中的每一項的 employee.age 屬性。

二、使用常量和變量

1. 常量

《Struts1.x系列教程(10):Validator驗證框架入門》 一文 中的 name 屬性使用了 mask 進行驗證。這個 mask 實際上代表了一個正則表達式。但如果在 validator.xml 文件中有很多個地方需要用到這個正則表達式,那就它就會在很多地方重復。為此,我們可以使用常量來避免這種事情發生。

常量分為全局常量和局部常量。

1 )全局常量

全局常量可以被用在 validator.xml 中定義的所有 form 中。我們可以使用 <global> 元素來配置全局常量。為了配置全局常量,將如下的內容放到第一個 <formset> 元素的前面。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < global >
< constant >
< constant-name > mask </ constant-name >
< constant-value > ^[a-zA-Z0-9]*$ </ constant-value >
</ constant >
</ global >
< formset >

<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]-->(2)局部常量
局部常量需要放到<formset>元素的開始位置(根據DTD的定義,局部常量只能放到這里,而全局常量可以放在第一個<formset>元素的前面,也可以放到<formset>的后面)。如下面的代碼所示:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < formset >
< constant >
< constant-name > mask </ constant-name >
< constant-value > ^[a-zA-Z0-9]*$ </ constant-value >
</ constant >
< form name ="firstValidatorForm" >

</ formset >

我們可以編寫如下的代碼來使用mask常量:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < var >
< var-name > mask </ var-name >
< var-value > ${mask} </ var-value >
</ var >

<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]-->
2. 變量

Validator框架通過<var>元素來定義變量。變量將作為<field>的子元素被使用。主要向depends屬性值(如mask)以及<msg>的錯誤信息傳遞數據。如下面的代碼通過變量required將“姓名”傳遞給了錯誤信息的第一個參數{0}。

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < field property ="name" depends ="required" >
< msg name ="required" key ="error.name.blank" />
< arg name ="required" key ="${var:required}" position ="0" resource ="false" />
< var >
< var-name > required </ var-name >
< var-value > 姓名 </ 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]-->

三、客戶端驗證

使用Validator框架的好處之一就是可以很容易地將服務端驗證變為客戶端驗證(JavaScript驗證)。為了完成這一轉換,我們只需要修改兩個地方?,F在就拿 《Struts1.x系列教程(10):Validator驗證框架入門》 中的firstValidator.jsp為例來說明要修改什么。
首先,在firstValidator.jsp中的任何地方(當然,任何地方指的是客戶端代碼區,并不包括<%...%>和<%--... --%>所包含的內容)添加如下的<html:javascript>標簽:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> < html:javascript formName ="firstValidatorForm" />

<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]-->然后在<html:form>標簽中加入如下的屬性:

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --> onsubmit = " returnvalidateFirstValidatorForm(this); "

<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"><!--[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]--> 現在再運行 firstValidator.jsp ,如果輸入錯誤, IE 就會直接彈出錯誤信息對話框。我們查看客戶端源代碼就會發現,在源代碼中多了很多 JavaScript 代碼。實際上,當加入 <html:javascript> 標簽后,在運行 JSP 頁面時, Servlet 引擎就會將 Validator 框架中 JavaScript 版本的預定義驗證代碼連同 HTML 代碼都發送到客戶端。這些 JavaScript 代碼中有一個入口函數,這個函數的名稱前綴為 validate ,后面是首字母大寫的 Form 名。在 <html:form> 標簽中的 onsubmit 事件中調用這個入口函數就可以通過 JavaScript 代碼來驗證客戶端錄入的數據了。

下一篇:
Struts1.x系列教程(12):Validator驗證框架的內置標準驗證



國內最棒的Google Android技術社區(eoeandroid),歡迎訪問!

《銀河系列原創教程》 發布

《Java Web開發速學寶典》 出版,歡迎定購

Struts1.x系列教程(11):Validator驗證框架高級應用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!??!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 日韩亚洲欧美在线爱色 | 福利片在线观看 | 丝袜 亚洲 另类 欧美 综合 | 精品视频一区二区三区 | 成人在线视频免费 | 欧美性视频网 | 久久久久国产精品免费免费搜索 | 欧美一级二级三级 | 日本国产欧美 | 美日韩一区二区 | 亚洲一二三 | 国产一区二区三区久久久久久久久 | 国产精品成人国产乱一区 | 色综合久久中文字幕综合网 | 日本高清视频免费在线观看 | 噜噜狠狠 | 欧美日韩一区二区视频在线观看 | 亚洲 欧美 另类 综合 偷拍 | 久久一本久综合久久爱 | 青草在线观看 | 色老头xxxwww作爱视频 | 总攻调教各种受肉 | 亚洲免费久久 | 国产美女自卫慰视频福利 | 午夜影网 | 午夜视频免费 成人 | 午夜影视免费片在线观看 | 亚洲精品久久久久一区二区三区 | 色鬼久久 | 日本视频在线免费 | 一个色综合亚洲伊人久久 | 91在线看 | 亚洲免费视频在线观看 | 999国产视频 | 久久国产精品亚洲 | 精彩视频一区 | 亚洲精品国精品久久99热 | 日韩欧美精品在线 | 亚洲精品一区二区网址 | 成人毛片18岁女人毛片免费看 | 色橹橹欧美在线观看视频高清免费 |