📄 在struts 2_0中实现表单数据校验(validation) - max on java - blogjava.htm
字号:
style="COLOR: #000000">{<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> @Override<BR><IMG id=Codehighlighter1_244_625_Open_Image
onclick="this.style.display='none'; Codehighlighter1_244_625_Open_Text.style.display='none'; Codehighlighter1_244_625_Closed_Image.style.display='inline'; Codehighlighter1_244_625_Closed_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_244_625_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_244_625_Closed_Text.style.display='none'; Codehighlighter1_244_625_Open_Image.style.display='inline'; Codehighlighter1_244_625_Open_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000">Object convertValue(Map context, Object value, Class
toType) </SPAN><SPAN id=Codehighlighter1_244_625_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_244_625_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_275_448_Open_Image
onclick="this.style.display='none'; Codehighlighter1_275_448_Open_Text.style.display='none'; Codehighlighter1_275_448_Closed_Image.style.display='inline'; Codehighlighter1_275_448_Closed_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_275_448_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_275_448_Closed_Text.style.display='none'; Codehighlighter1_275_448_Open_Image.style.display='inline'; Codehighlighter1_275_448_Open_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">if</SPAN> <SPAN style="COLOR: #000000">(toType
</SPAN><SPAN style="COLOR: #000000">==</SPAN> <SPAN
style="COLOR: #000000">Locale.</SPAN> <SPAN style="COLOR: #0000ff">class</SPAN>
<SPAN style="COLOR: #000000">) </SPAN><SPAN
id=Codehighlighter1_275_448_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_275_448_Open_Text><SPAN
style="COLOR: #000000">{ <BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> System.out.println(</SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">Converting
String to Locale<IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/dot.gif"></SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">);<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> String locale </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">((String[])
value)[</SPAN> <SPAN style="COLOR: #000000">0</SPAN> <SPAN
style="COLOR: #000000">];<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #0000ff">new</SPAN> <SPAN
style="COLOR: #000000">Locale(locale.substring(</SPAN> <SPAN
style="COLOR: #000000">0</SPAN> <SPAN style="COLOR: #000000">, </SPAN><SPAN
style="COLOR: #000000">2</SPAN> <SPAN style="COLOR: #000000">),
locale.substring(</SPAN> <SPAN style="COLOR: #000000">3</SPAN> <SPAN
style="COLOR: #000000">));<BR><IMG id=Codehighlighter1_482_607_Open_Image
onclick="this.style.display='none'; Codehighlighter1_482_607_Open_Text.style.display='none'; Codehighlighter1_482_607_Closed_Image.style.display='inline'; Codehighlighter1_482_607_Closed_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedSubBlockStart.gif"
align=top><IMG id=Codehighlighter1_482_607_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_482_607_Closed_Text.style.display='none'; Codehighlighter1_482_607_Open_Image.style.display='inline'; Codehighlighter1_482_607_Open_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ContractedSubBlock.gif"
align=top> }</SPAN> </SPAN><SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">else</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">if</SPAN> <SPAN
style="COLOR: #000000">(toType </SPAN><SPAN style="COLOR: #000000">==</SPAN>
<SPAN style="COLOR: #000000">String.</SPAN> <SPAN
style="COLOR: #0000ff">class</SPAN> <SPAN style="COLOR: #000000">) </SPAN><SPAN
id=Codehighlighter1_482_607_Closed_Text
style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/dot.gif">
</SPAN><SPAN id=Codehighlighter1_482_607_Open_Text><SPAN
style="COLOR: #000000">{<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> System.out.println(</SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">Converting
Locale to String<IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/dot.gif"></SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">);<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> Locale locale </SPAN><SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">(Locale)
value;<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">return</SPAN> <SPAN
style="COLOR: #000000">locale.toString();<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN
style="COLOR: #000000"><BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN> <SPAN
style="COLOR: #0000ff">return</SPAN> <SPAN style="COLOR: #000000"></SPAN><SPAN
style="COLOR: #0000ff">null</SPAN> <SPAN style="COLOR: #000000">;<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedSubBlockEnd.gif"
align=top> }</SPAN> </SPAN><SPAN style="COLOR: #000000"><BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedBlockEnd.gif"
align=top>}</SPAN> </SPAN></DIV>
<P>之后,修改国际化资源文件,内容为:</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #000000">HelloWorld</SPAN> <SPAN style="COLOR: #000000">=</SPAN>
<SPAN style="COLOR: #000000">你好,世界!<BR>invalid.fieldvalue.loc</SPAN> <SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">Locale必须为\</SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">xx_XX\</SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN
style="COLOR: #000000">的格式<BR>validation.loc</SPAN> <SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">区域必须为中国或美国</SPAN>
</DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">globalMessages_zh_CN.properties</SPAN>
<BR><BR>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #000000">HelloWorld</SPAN> <SPAN style="COLOR: #000000">=</SPAN>
<SPAN style="COLOR: #000000">Hello World!<BR>invalid.fieldvalue.loc</SPAN> <SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">Locale must like
\</SPAN> <SPAN style="COLOR: #000000">"</SPAN> <SPAN
style="COLOR: #000000">xx_XX\</SPAN> <SPAN style="COLOR: #000000">"</SPAN> <SPAN
style="COLOR: #000000"><BR>validation.loc</SPAN> <SPAN
style="COLOR: #000000">=</SPAN> <SPAN style="COLOR: #000000">Locale must be
China or USA</SPAN> </DIV><SPAN
style="FONT-SIZE: smaller; FONT-STYLE: italic">globalMessages_en_US.properties</SPAN>
<P>发布运行应用程序,在浏览器中键入<A
href="http://localhost:8080/Struts2_Validation/HelloWorld.action">http://localhost:8080/Struts2_Validation/HelloWorld.action</A>,在Locale中输入zh<SPAN
style="FONT-SIZE: 13px; BACKGROUND-COLOR: #eeeeee">_CN</SPAN>,按“Submit”提交,效果如上篇文章所示。而在服务器控制台有如下输出:</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><SPAN
style="COLOR: #000000">Converting String to Locale... <BR>Calling
validateExecute() by reflection... <BR>Calling validate()... <BR>Calling
execute()... <BR>Converting Locale to String...</SPAN> <SPAN
style="COLOR: #000000"></SPAN></DIV>
<P>上述的输出说明了Struts 2.0的数据校验工作方式,它需要经过下面几个步骤:</P>
<OL>
<LI>通过转换器将请求参数转换成相应的Bean属性;
<LI>判断转换过程是否出现异常。如果有,则将其保存到ActionContext中,conversionError拦截器再封装为fieldError;如果没有,进行下一步;
<LI>通过反射(Reflection)来调用validateXxx()方法(其中,Xxx表示Action的方法名);
<LI>调用validate()方法;
<LI>如果经过上述步骤没有出现fieldError,则调用Action方法;如果有,则会跳过Action方法,通过国际化将fieldError输出到页面。
</LI></OL>
<P>不喜欢看文字的朋友,可以参考下面的图1。</P>
<P><IMG alt="图1 校验顺序图"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/r_img1.jpg">
<BR><SPAN style="FONT-SIZE: smaller; FONT-STYLE: italic">图1 校验顺序图</SPAN> </P>
<P>看到这里可能大家会疑问:“这么多地方可以校验表单数据,到底我应该在那里做呢?”有选择是好事,但抉择的过程往往是痛苦的,往往让人不知所措。如果大家参照以下几点建议,相信会比较容易地做出正确的抉择。</P>
<OL>
<LI>如果需要转换的数据,通常做法在转换的时候做格式的校验,在Action中的校验方法中校验取值。假如用户填错了格式,我们可以通过在资源文件配置invalid.fieldvalue.xxx(xxx为属性名)来提示用户正确的格式,不同的阶段出错显示不同的信息。具体做法请参考上面的例子;
<LI>至于用validate()还是validateXxx(),我推荐使用validate()。原因是validateXxx()使用了反射,相对来说性能稍差,而validate()则是通过接口<A
href="http://www.opensymphony.com/webwork/api/com/opensymphony/xwork/Validateable.html">com.opensymphony.xwork2.Validateable</A>调用。当然如果你的表单数据取值是取决于特定Action方法,则应该使用validateXxx()。
</LI></OL>
<P>在运行上面的例子时,在Locale中输入zh并提交时出现图2所示页面。</P>
<P><IMG alt="图2 错误格式"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/r_img2.gif">
<BR><SPAN style="FONT-SIZE: smaller; FONT-STYLE: italic">图2 错误格式</SPAN> </P>
<P>在Locale中输入de_DE时,出现如图3所示页面。</P>
<P><IMG alt="图3 取值错误"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/r_img3.gif">
<BR><SPAN style="FONT-SIZE: smaller; FONT-STYLE: italic">图3 取值错误</SPAN> </P>
<H2>使用Struts 2.0的校验框架</H2>
<P>上一节的内容都是关于如何编程实现校验,这部分工作大都是单调的重复。更多情况下,我们使用Struts 2.0的校验框架,通过配置实现一些常见的校验。</P>
<P>我学习编程有个习惯——喜欢先看输出结果,再看代码实现。这样学的好处是先看结果可以刺激学习的激情,也可以在看代码前自已思考一下如何实现,然后带着问题去看代码,那就清晰多了。因此下面我们先来做演示。</P>
<P>首先,在tutorial包下新建ValidationAction.java,代码如下:</P>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/None.gif"
align=top> <SPAN style="COLOR: #0000ff">package</SPAN> <SPAN
style="COLOR: #000000">tutorial;<BR><IMG
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -