« | September 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
本博客在此声明所有文章均为转摘,只做资料收集使用。并无其他商业用途。 |
Blog信息 |
blog名称: 日志总数:210 评论数量:205 留言数量:-19 访问次数:920141 建立时间:2007年5月10日 |

| |
[struts2]在Struts 2.0中实现表单数据校验(Validation) 文章收藏, 网上资源, 软件技术, 电脑与网络
李小白 发表于 2007/11/9 22:32:42 |
All Input Is Evil! -Writing secure code
在写前几篇文章的时候,有些朋友建议我的写一篇关于表单数据校验的文章。 正如文章的开头所引用的《Writing Secure Code》的名言:“所有的输入都是罪恶的”,所以我们应该对所有的外部输入进行校验。而表单是应用程序最简单的入口,对其传进来的数据,我们必须进行校验。
转换与校验(Conversion & Validation)
其实上篇文章,我本来是打算写表单数据校验的内容,但是经过再三思考后,还是决定先写Struts 2.0转换器的内容。原因是我认为转换是校验的基础,只有在数据被正确地转换成其对应的类型后,我们才可以对其取值范围进行校验。看个例子相信大家可以更清楚。现在我们就来改造一下《转换器(Converter)——Struts 2.0中的魔术师》的第一个例子。
首先,从Action开始,修改后的代码如下:
500)this.width=500'> package tutorial;500)this.width=500'>500)this.width=500'> import java.util.Locale;500)this.width=500'>500)this.width=500'> import com.opensymphony.xwork2.ActionSupport;500)this.width=500'> import com.opensymphony.xwork2.util.LocalizedTextUtil;500)this.width=500'>500)this.width=500'>500)this.width=500'> public class HelloWorld extends ActionSupport 500)this.width=500'> {500)this.width=500'> private String msg;500)this.width=500'> private Locale loc = Locale.US;500)this.width=500'> 500)this.width=500'>500)this.width=500'> public String getMsg() 500)this.width=500'> {500)this.width=500'> return msg; 500)this.width=500'> } 500)this.width=500'> 500)this.width=500'>500)this.width=500'> public Locale getLoc() 500)this.width=500'> {500)this.width=500'> return loc;500)this.width=500'> } 500)this.width=500'> 500)this.width=500'>500)this.width=500'> public void setLoc(Locale loc) 500)this.width=500'> {500)this.width=500'> this .loc = loc;500)this.width=500'> } 500)this.width=500'> 500)this.width=500'> @Override500)this.width=500'>500)this.width=500'> public void validate() 500)this.width=500'> {500)this.width=500'> System.out.println( " Calling validate()500)this.width=500'> " );500)this.width=500'>500)this.width=500'> if ( ! (loc.equals(Locale.US) || loc.equals(Locale.CHINA))) 500)this.width=500'> {500)this.width=500'> addFieldError( " loc " , getText( " validation.loc " ));500)this.width=500'> } 500)this.width=500'> } 500)this.width=500'> 500)this.width=500'>500)this.width=500'> public void validateExecute() 500)this.width=500'> {500)this.width=500'> System.out.println( " Calling validateExecute() by reflection500)this.width=500'> " );500)this.width=500'> } 500)this.width=500'> 500)this.width=500'> @Override500)this.width=500'>500)this.width=500'> public String execute() 500)this.width=500'> {500)this.width=500'> System.out.println( " Calling execute()500)this.width=500'> " );500)this.width=500'> // LocalizedTextUtil是Struts 2.0中国际化的工具类,<s:text>标志就是通过调用它实现国际化的 500)this.width=500'> msg = LocalizedTextUtil.findDefaultText( " HelloWorld " , loc);500)this.width=500'> return SUCCESS;500)this.width=500'> } 500)this.width=500'>}
然后,修改Struts.xml中Action的定义指明输入地址:
< action name ="HelloWorld" class ="tutorial.HelloWorld" > < result > /HelloWorld.jsp </ result > < result name ="input" > /HelloWorld.jsp </ result > </ action >
接着,在HelloWorld.jsp中加入错误提示:
<% @ page contentType = " text/html; charset=UTF-8 " %> <% @taglib prefix = " s " uri = " /struts-tags " %> < html > < head > < title > Hello World </ title > </ head > < body > < div style ="color:red;" > < s:fielderror /> </ div > < s:form action ="HelloWorld" theme ="simple" > Locale: < s:textfield name ="loc" /> < s:submit /> </ s:form > < h2 >< s:property value ="msg" /></ h2 > </ body > </ html >
再修改LocaleConverter.java文件,将内容改为:
500)this.width=500'> package tutorial;500)this.width=500'>500)this.width=500'> import java.util.Locale;500)this.width=500'> import java.util.Map;500)this.width=500'> import java.util.regex.Pattern;500)this.width=500'>500)this.width=500'>500)this.width=500'> public class LocaleConverter extends ognl.DefaultTypeConverter 500)this.width=500'> {500)this.width=500'> @Override500)this.width=500'>500)this.width=500'> public Object convertValue(Map context, Object value, Class toType) 500)this.width=500'> {500)this.width=500'>500)this.width=500'> if (toType == Locale. class ) 500)this.width=500'> { 500)this.width=500'> System.out.println( " Converting String to Locale500)this.width=500'> " );500)this.width=500'> String locale = ((String[]) value)[ 0 ];500)this.width=500'> return new Locale(locale.substring( 0 , 2 ), locale.substring( 3 ));500)this.width=500'>500)this.width=500'> } else if (toType == String. class ) 500)this.width=500'> {500)this.width=500'> System.out.println( " Converting Locale to String500)this.width=500'> " );500)this.width=500'> Locale locale = (Locale) value;500)this.width=500'> return locale.toString();500)this.width=500'> } 500)this.width=500'> return null ;500)this.width=500'> } 500)this.width=500'>}
之后,修改国际化资源文件,内容为:
HelloWorld = 你好,世界!invalid.fieldvalue.loc = Locale必须为\ " xx_XX\ " 的格式validation.loc = 区域必须为中国或美国 globalMessages_zh_CN.properties
HelloWorld = Hello World!invalid.fieldvalue.loc = Locale must like \ " xx_XX\ " validation.loc = Locale must be China or USA globalMessages_en_US.properties
发布运行应用程序,在浏览器中键入http://localhost:8080/Struts2_Validation/HelloWorld.action,在Locale中输入zh_CN,按“Submit”提交,效果如上篇文章所示。而在服务器控制台有如下输出:
Converting String to Locale... Calling validateExecute() by reflection... Calling validate()... Calling execute()... Converting Locale to String...
上述的输出说明了Struts 2.0的数据校验工作方式,它需要经过下面几个步骤:
通过转换器将请求参数转换成相应的Bean属性;
判断转换过程是否出现异常。如果有,则将其保存到ActionContext中,conversionError拦截器再封装为fieldError;如果没有,进行下一步;
通过反射(Reflection)来调用validateXxx()方法(其中,Xxx表示Action的方法名);
调用validate()方法;
如果经过上述步骤没有出现fieldError,则调用Action方法;如果有,则会跳过Action方法,通过国际化将fieldError输出到页面。
不喜欢看文字的朋友,可以参考下面的图1。
500)this.width=500'> 图1 校验顺序图
看到这里可能大家会疑问:“这么多地方可以校验表单数据,到底我应该在那里做呢?”有选择是好事,但抉择的过程往往是痛苦的,往往让人不知所措。如果大家参照以下几点建议,相信会比较容易地做出正确的抉择。
如果需要转换的数据,通常做法在转换的时候做格式的校验,在Action中的校验方法中校验取值。假如用户填错了格式,我们可以通过在资源文件配置invalid.fieldvalue.xxx(xxx为属性名)来提示用户正确的格式,不同的阶段出错显示不同的信息。具体做法请参考上面的例子;
至于用validate()还是validateXxx(),我推荐使用validate()。原因是validateXxx()使用了反射,相对来说性能稍差,而validate()则是通过接口com.opensymphony.xwork2.Validateable调用。当然如果你的表单数据取值是取决于特定Action方法,则应该使用validateXxx()。
在运行上面的例子时,在Locale中输入zh并提交时出现图2所示页面。
500)this.width=500'> 图2 错误格式
在Locale中输入de_DE时,出现如图3所示页面。
500)this.width=500'> 图3 取值错误
使用Struts 2.0的校验框架
上一节的内容都是关于如何编程实现校验,这部分工作大都是单调的重复。更多情况下,我们使用Struts 2.0的校验框架,通过配置实现一些常见的校验。
我学习编程有个习惯——喜欢先看输出结果,再看代码实现。这样学的好处是先看结果可以刺激学习的激情,也可以在看代码前自已思考一下如何实现,然后带着问题去看代码,那就清晰多了。因此下面我们先来做演示。
首先,在tutorial包下新建ValidationAction.java,代码如下:
500)this.width=500'> package tutorial;500)this.width=500'>500)this.width=500'> import com.opensymphony.xwork2.ActionSupport;500)this.width=500'>500)this.width=500'>500)this.width=500'> public class ValidationAction extends ActionSupport 500)this.width=500'> {500)this.width=500'> private String reqiuredString;500)this.width=500'>500)this.width=500'>500)this.width=500'> public String getReqiuredString() 500)this.width=500'> {500)this.width=500'> return reqiuredString;500)this.width=500'> } 500)this.width=500'> 500)this.width=500'> 500)this.width=500'> public void setReqiuredString(String reqiuredString) 500)this.width=500'> {500)this.width=500'> this .reqiuredString = reqiuredString;500)this.width=500'> } 500)this.width=500'> 500)this.width=500'> @Override500)this.width=500'>500)this.width=500'> public String execute() 500)this.width=500'> {500)this.width=500'> return SUCCESS;500)this.width=500'> } 500)this.width=500'>}
然后,配置上述所建的Ation,代码片段如下:
< action name ="ValidationAction" class ="tutorial.ValidationAction" > < result > /Output.jsp </ result > < result name ="input" > /Input.jsp </ result > </ action >
接着,创建Input.jsp和Output.jsp,内容分别如下:
<% @ page contentType = " text/html; charset=UTF-8 " %> <% @taglib prefix = " s " uri = " /struts-tags " %> < |
|
|