📄 在struts 2_0中实现表单数据校验(validation) - max on java - blogjava.htm
字号:
align=top> System.out.println(</SPAN> <SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">Calling
execute()<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> </SPAN> <SPAN
style="COLOR: #008000">//</SPAN> <SPAN
style="COLOR: #008000">LocalizedTextUtil是Struts
2.0中国际化的工具类,<s:text>标志就是通过调用它实现国际化的</SPAN> <SPAN
style="COLOR: #008000"><BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/InBlock.gif"
align=top> </SPAN><SPAN style="COLOR: #000000">
msg </SPAN><SPAN style="COLOR: #000000">=</SPAN> <SPAN
style="COLOR: #000000">LocalizedTextUtil.findDefaultText(</SPAN> <SPAN
style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">HelloWorld</SPAN>
<SPAN style="COLOR: #000000">"</SPAN> <SPAN style="COLOR: #000000">,
loc);<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">SUCCESS;<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>然后,修改Struts.xml中Action的定义指明输入地址:</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: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">action
</SPAN><SPAN style="COLOR: #ff0000">name</SPAN> <SPAN
style="COLOR: #0000ff">="HelloWorld"</SPAN> <SPAN
style="COLOR: #ff0000">class</SPAN> <SPAN
style="COLOR: #0000ff">="tutorial.HelloWorld"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">result</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000">/HelloWorld.jsp</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">result </SPAN><SPAN style="COLOR: #ff0000">name</SPAN>
<SPAN style="COLOR: #0000ff">="input"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000">/HelloWorld.jsp</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">result</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">action</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> </DIV>
<P>接着,在HelloWorld.jsp中加入错误提示:</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; BACKGROUND-COLOR: #ffff00"><%</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">@ page
contentType</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">text/html;
charset=UTF-8</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00"><%</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">@taglib prefix</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">s</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">uri</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">=</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">/struts-tags</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5">"</SPAN> <SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #f5f5f5"></SPAN><SPAN
style="COLOR: #000000; BACKGROUND-COLOR: #ffff00">%></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">html</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"><</SPAN>
<SPAN style="COLOR: #800000">head</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">title</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000">Hello World</SPAN> <SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">title</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">head</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">body</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">div </SPAN><SPAN style="COLOR: #ff0000">style</SPAN>
<SPAN style="COLOR: #0000ff">="color:red;"</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">s:fielderror </SPAN><SPAN
style="COLOR: #0000ff">/></SPAN> <SPAN style="COLOR: #000000"><BR>
</SPAN><SPAN style="COLOR: #0000ff"></</SPAN> <SPAN
style="COLOR: #800000">div</SPAN> <SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">s:form
</SPAN><SPAN style="COLOR: #ff0000">action</SPAN> <SPAN
style="COLOR: #0000ff">="HelloWorld"</SPAN> <SPAN
style="COLOR: #ff0000">theme</SPAN> <SPAN
style="COLOR: #0000ff">="simple"</SPAN> <SPAN style="COLOR: #0000ff">></SPAN>
<SPAN style="COLOR: #000000"> <BR>
Locale: </SPAN><SPAN style="COLOR: #0000ff"><</SPAN>
<SPAN style="COLOR: #800000">s:textfield </SPAN><SPAN
style="COLOR: #ff0000">name</SPAN> <SPAN style="COLOR: #0000ff">="loc"</SPAN>
<SPAN style="COLOR: #ff0000"></SPAN><SPAN style="COLOR: #0000ff">/></SPAN>
<SPAN style="COLOR: #ff0000">&nbsp;</SPAN> <SPAN
style="COLOR: #0000ff"><</SPAN> <SPAN style="COLOR: #800000">s:submit
</SPAN><SPAN style="COLOR: #0000ff">/></SPAN> <SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">s:form</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000">
<BR> </SPAN><SPAN style="COLOR: #0000ff"><</SPAN> <SPAN
style="COLOR: #800000">h2</SPAN> <SPAN style="COLOR: #0000ff">><</SPAN>
<SPAN style="COLOR: #800000">s:property </SPAN><SPAN
style="COLOR: #ff0000">value</SPAN> <SPAN style="COLOR: #0000ff">="msg"</SPAN>
<SPAN style="COLOR: #ff0000"></SPAN><SPAN
style="COLOR: #0000ff">/></</SPAN> <SPAN style="COLOR: #800000">h2</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> <SPAN
style="COLOR: #000000"><BR></SPAN><SPAN style="COLOR: #0000ff"></</SPAN>
<SPAN style="COLOR: #800000">body</SPAN> <SPAN
style="COLOR: #0000ff">></SPAN> <SPAN style="COLOR: #000000"><BR></SPAN><SPAN
style="COLOR: #0000ff"></</SPAN> <SPAN style="COLOR: #800000">html</SPAN>
<SPAN style="COLOR: #0000ff">></SPAN> </DIV>
<P>再修改LocaleConverter.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
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/None.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.Locale;<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/None.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.Map;<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/None.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">import</SPAN> <SPAN
style="COLOR: #000000">java.util.regex.Pattern;<BR><IMG
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/None.gif"
align=top><BR><IMG id=Codehighlighter1_162_627_Open_Image
onclick="this.style.display='none'; Codehighlighter1_162_627_Open_Text.style.display='none'; Codehighlighter1_162_627_Closed_Image.style.display='inline'; Codehighlighter1_162_627_Closed_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ExpandedBlockStart.gif"
align=top><IMG id=Codehighlighter1_162_627_Closed_Image style="DISPLAY: none"
onclick="this.style.display='none'; Codehighlighter1_162_627_Closed_Text.style.display='none'; Codehighlighter1_162_627_Open_Image.style.display='inline'; Codehighlighter1_162_627_Open_Text.style.display='inline';"
src="在Struts 2_0中实现表单数据校验(Validation) - Max On Java - BlogJava.files/ContractedBlock.gif"
align=top></SPAN> <SPAN style="COLOR: #0000ff">public</SPAN> <SPAN
style="COLOR: #000000"></SPAN><SPAN style="COLOR: #0000ff">class</SPAN> <SPAN
style="COLOR: #000000">LocaleConverter </SPAN><SPAN
style="COLOR: #0000ff">extends</SPAN> <SPAN
style="COLOR: #000000">ognl.DefaultTypeConverter </SPAN><SPAN
id=Codehighlighter1_162_627_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_162_627_Open_Text><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -