- 浏览: 620428 次
- 性别:
- 来自: 广州
文章分类
最新评论
Struts1.x系列教程(11):Validator验证框架高级应用
本文为原创,如需转载,请注明作者和出处,谢谢!
上一篇:Struts1.x系列教程(10):Validator验证框架入门
Struts2教程4:使用validate方法验证数据
在《Struts1.x系列教程(10):Validator验证框架入门》一文中给出的例子中的name、age和email都是简单的属性,如果是嵌套的属性(也就是一个属性的数据类型是另外一个带有属性的类),可以使用圆点(.)符号来访问。如我们可以通过设置<field>元素的property属性值为“employee.salary”来验证employee属性中的salary属性。
如果要验证的索引属性,我们可以使用<field>元素的indexedListProperty属性来指定这个索引属性,如下面代码如示:
<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 */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
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 */
@page
{}
@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配合使用,如下面代码所示:
<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 */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; 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 */ @page {} @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>元素的前面。
<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 */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
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 */
@page
{}
@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>的后面)。如下面的代码所示:
<constant>
<constant-name>mask</constant-name>
<constant-value>^[a-zA-Z0-9]*$</constant-value>
</constant>
<formname="firstValidatorForm">
</formset>
我们可以编写如下的代码来使用mask常量:
<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 */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
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 */
@page
{}
@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}。
<msgname="required"key="error.name.blank"/>
<argname="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 */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1;} @font-face {font-family:""@宋体"; 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 */ @page {} @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验证)。为了完成这一转换,我们只需要修改两个地方。现在就拿《Struts1.x系列教程(10):Validator验证框架入门》中的firstValidator.jsp为例来说明要修改什么。
首先,在firstValidator.jsp中的任何地方(当然,任何地方指的是客户端代码区,并不包括<%...%>和<%--... --%>所包含的内容)添加如下的<html:javascript>标签:
<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 */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
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 */
@page
{}
@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>标签中加入如下的属性:
<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 */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
{font-family:""@宋体";
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 */
@page
{}
@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版本中使用validator验证框架实例。
validator框架验证入门例子 struts1.x validator框架验证入门例子
解决Struts1.x中使用Validator框架对DispatchAction不同方法使用不同的字段验证 你可以先参看我的blog文章:http://blog.csdn.net/qjyong/archive/2008/11/16/3311688.aspx 再决定是否下载。
Struts1的validator验证框架
jar包大小:135KB commons-validator-1.3.1.jar jar包大小:93KB commons-transaction-1.2.jar jar包大小:141KB commons-scxml-0.6.jar jar包大小:254KB commons-primitives-1.0.jar jar包大小:60KB commons-pool-1.3....
hibernate-validator 5.3.5.Final jar包 ;desc:if you want validator your project
Struts Validator验证框架详细讲解.txt
NULL 博文链接:https://nassir.iteye.com/blog/1112713
struts validator验证框架项目,都是很常用的验证,比如用户名满足6位以上,密码与确认密码必须相同,日期格式必须为yyyy-MM-dd等等
struts中使用validator框架由简单到复杂的三个实例,附加说明。
struts 2.3.4.1 最新英文版API =========================================================== 概览 程序包 类 使用 树 已过时 索引 帮助 上一个 下一个 框架 无框架 所有类 ------------------------------------...
struts 的validator框架验证,入门好例!!!!
struts应用中使用了validator验证框架,它为struts提供了灵活的验证规则,而无需编程
Validator验证框架 回顾ActionForm的表单验证,掌握Validator验证框架.zip
Validator验证框架,STRUTS动态验证的规则与规范!
Validator验证框架.pdf
Struts Validator验证器使用指南 <br>验证器: 从0.5版,验证器在一些form中就已经实现了,他最初包含在开发人员包中,后来核心代码挪到Jakarta Commons包中和Struts特别扩展中作为 Struts 1.1的一部分。...
掌握Validator验证框架.pptx
传智博客Validator框架与struts应用ppt
validator of struts, 验证框架,主要包括4种方法,具体可以参考博客!