`
playfish
  • 浏览: 285258 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

在Struts 2.0中实现表单数据校验(Validation)

 
阅读更多
All Input Is Evil!
-Writing secure code

在写前几篇文章的时候,有些朋友建议我的写一篇关于表单数据校验的文章。 正如文章的开头所引用的《Writing Secure Code》的名言:“所有的输入都是罪恶的”,所以我们应该对所有的外部输入进行校验。而表单是应用程序最简单的入口,对其传进来的数据,我们必须进行校 验。

转换与校验(Conversion & Validation)

其实上篇文章,我本来是打算写表单数据校验的内容,但是经过再三思考后,还是决定先写Struts 2.0转换器的内容。原因是我认为转换是校验的基础,只有在数据被正确地转换成其对应的类型后,我们才可以对其取值范围进行校验。看个例子相信大家可以更 清楚。现在我们就来改造一下《转换器(Converter)——Struts 2.0中的魔术师》的第一个例子。

首先,从Action开始,修改后的代码如下:

package tutorial;

import java.util.Locale;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.util.LocalizedTextUtil;

public class HelloWorld extends ActionSupport {
   
private String msg;
   
private Locale loc = Locale.US;
   
   
public String getMsg() {
       
return msg;        
   }

   
   
public Locale getLoc() {
       
return loc;
   }

   
   
public void setLoc(Locale loc) {
       
this .loc = loc;
   }

   
   @Override
   
public void validate() {
       System.out.println(
" Calling validate() " );
       
if ( ! (loc.equals(Locale.US) || loc.equals(Locale.CHINA))) {
                   addFieldError(
" loc " , getText( " validation.loc " ));
       }

   }

       
   
public void validateExecute() {
       System.out.println(
" Calling validateExecute() by reflection " );
   }

   
   @Override
   
public String execute() {
       System.out.println(
" Calling execute() " );
       
// LocalizedTextUtil是Struts 2.0中国际化的工具类,<s:text>标志就是通过调用它实现国际化的
           msg = LocalizedTextUtil.findDefaultText( " HelloWorld " , loc);
       
return SUCCESS;
   }

}

然后,修改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" /> &nbsp; < s:submit />
   
</ s:form >    
   
< h2 >< s:property value ="msg" /></ h2 >
</ body >
</ html >

再修改LocaleConverter.java文件,将内容改为:

package tutorial;

import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

public class LocaleConverter extends ognl.DefaultTypeConverter {
   @Override
   
public Object convertValue(Map context, Object value, Class toType) {
       
if (toType == Locale. class ) {            
           System.out.println(
" Converting String to Locale " );
           String locale
= ((String[]) value)[ 0 ];
           
return new Locale(locale.substring( 0 , 2 ), locale.substring( 3 ));
       }
else if (toType == String. class ) {
           System.out.println(
" Converting Locale to String " );
           Locale locale
= (Locale) value;
           
return locale.toString();
       }

       
return null ;
   }

}

之后,修改国际化资源文件,内容为:

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的数据校验工作方式,它需要经过下面几个步骤:

  1. 通过转换器将请求参数转换成相应的Bean属性;
  2. 判断转换过程是否出现异常。如果有,则将其保存到ActionContext中,conversionError拦截器再封装为fieldError;如果没有,进行下一步;
  3. 通过反射(Reflection)来调用validateXxx()方法(其中,Xxx表示Action的方法名);
  4. 调用validate()方法;
  5. 如果经过上述步骤没有出现fieldError,则调用Action方法;如果有,则会跳过Action方法,通过国际化将fieldError输出到页面。

不喜欢看文字的朋友,可以参考下面的图1。

图1 校验顺序图
图1 校验顺序图

看到这里可能大家会疑问:“这么多地方可以校验表单数据,到底我应该在那里做呢?”有选择是好事,但抉择的过程往往是痛苦的,往往让人不知所措。如果大家参照以下几点建议,相信会比较容易地做出正确的抉择。

  1. 如 果需要转换的数据,通常做法在转换的时候做格式的校验,在Action中的校验方法中校验取值。假如用户填错了格式,我们可以通过在资源文件配置 invalid.fieldvalue.xxx(xxx为属性名)来提示用户正确的格式,不同的阶段出错显示不同的信息。具体做法请参考上面的例子;
  2. 至于用validate()还是validateXxx(),我推荐使用validate()。原因是validateXxx()使用了反射,相对来说性能稍差,而validate()则是通过接口com.opensymphony.xwork2.Validateable调用。当然如果你的表单数据取值是取决于特定Action方法,则应该使用validateXxx()。

在运行上面的例子时,在Locale中输入zh并提交时出现图2所示页面。

图2 错误格式
图2 错误格式

在Locale中输入de_DE时,出现如图3所示页面。

图3 取值错误
图3 取值错误

使用Struts 2.0的校验框架

上一节的内容都是关于如何编程实现校验,这部分工作大都是单调的重复。更多情况下,我们使用Struts 2.0的校验框架,通过配置实现一些常见的校验。

我学习编程有个习惯——喜欢先看输出结果,再看代码实现。这样学的好处是先看结果可以刺激学习的激情,也可以在看代码前自已思考一下如何实现,然后带着问题去看代码,那就清晰多了。因此下面我们先来做演示。

首先,在tutorial包下新建ValidationAction.java,代码如下:

package tutorial;

import com.opensymphony.xwork2.ActionSupport;

public class ValidationAction extends ActionSupport {
   
private String reqiuredString;

   
public String getReqiuredString() {
       
return reqiuredString;
   }


   
public void setReqiuredString(String reqiuredString) {
       
this .reqiuredString = reqiuredString;
   }

   
   @Override
   
public String execute() {
       
return SUCCESS;
   }
   
}

然后,配置上述所建的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 " %>
< html
分享到:
评论

相关推荐

    struts2.0中文教程

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新...

    Struts 2.0系列(MAX)

    在Struts 2.0中实现表单数据校验(Validation) Struts 2的基石——拦截器(Interceptor) 在Struts 2中实现IoC 在Struts 2中实现文件上传 在Struts 2中实现CRUD Struts 2中的OGNL Strus 2的新表单标志的使用 ...

    Struts2.0中文教程权威版

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 trus 2的新...

    个人认为目前最完备的Struts2教程

    06 在Struts 2.0中实现表单数据校验(Validation) 07 Struts 2的基石——拦截器(Interceptor) 08 在Struts 2中实现IoC 09 在Struts 2中实现文件上传 10 在Struts 2中实现CRUD 11 Struts 2中的OGNL 12 Struts 2的...

    struts2.0很全面的中文教程

    1.为Struts 2.0做好准备 2.常用的Struts 2.0的标志(Tag)介绍 3.Struts 2.0的Action讲解 ...5.转换器(Converter)——Struts 2.0中的魔术师 在6.Struts 2.0中实现表单数据校验(Validation) 7....... 8.......

    struts2入门教程

    ·Struts 2.0中实现表单数据校验(Validation) ·拦截器(Interceptor) ·Struts 2中实现IoC ·Struts 2中实现文件上传 ·Struts 2中的OGNL ·Strus 2的新表单标签的使用 ·Struts 2与AJAX ·Struts2分页 ...

    struts2[1].0标签详解

    Struts 2的基石——拦截器(Interceptor);在Struts 2_0中国际化(i18n)您的应用程序;在Struts 2_0中实现表单数据校验(Validation);Struts 2与AJAX;在Struts 2中实现IoC......

    基于struts2 的网络U盘

    数据库包括用户表和文件表,项目主体由Struts2框架搭建,前台jsp页面表单使用OGNL标签,XX_zh_CN(en_US).properties文件实现中英文国际化,XXAction-validation.xml实现表单校验,MD5加密文件绝对路径,保护文件安全...

    struts2验证框架

    使用struts2验证框架实现表单验证,也可以直接使用validation()

    struts自我学习过程程序以及说明

    表单校验: avlidation包: LocaleConverter.java Locales.java ValidationHWorld.java globalMessage_en_US.properties globalMessage_zh_CN.properties struts.properties xwork-conversion.propertie ...

    java面试宝典

    130、如何防止在JSP或SERVLET中的输出不被BROWSER保存在CACHE中? 32 131、在JSP中如何设置COOKIE? 32 132、在JSP中如何删除一个COOKIE? 32 133、在一个JSP的请求处理中如何停止JSP的执行 33 134、在JSP中如何定义...

    千方百计笔试题大全

    130、如何防止在JSP或SERVLET中的输出不被BROWSER保存在CACHE中? 32 131、在JSP中如何设置COOKIE? 32 132、在JSP中如何删除一个COOKIE? 32 133、在一个JSP的请求处理中如何停止JSP的执行 33 134、在JSP中如何定义...

    Struts in Action中文版

    2.4.4. Struts如何实现 Model 2, MVC, 和分层............................................................49 2.5. Struts 控制流...............................................................................

    struts in Action

    2.4.4. Struts 如何实现Model 2, MVC, 和分层............................................................49 2.5. Struts 控制流................................................................................

Global site tag (gtag.js) - Google Analytics