随笔-34  评论-1965  文章-0  trackbacks-0
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 >
< head >
   
< title > Hello World </ title >
   
<!-- 此标志的作用是引入Struts 2.0的常用的Javascript和CSS -->
   
< s:head />
</ head >
< body >
   
< s:form action ="ValidationAction" >            
       
< s:textfield name ="reqiuredString" label ="Required String" />
       
< s:submit />
   
</ s:form >    
</ body >
</ html >
Input.jsp

<% @ page  contentType = " text/html; charset=UTF-8 " %>
<% @taglib prefix = " s " uri = " /struts-tags " %>
< html >
< head >
   
< title > Hello World </ title >
</ head >
< body >
    Required String:
< s:property value ="reqiuredString" />    
</ body >
</ html >
Output.jsp

再接下来,在tutorial包下创建ValidationAction的校验配置文件Xxx-validation.xml(Xxx为Action的类名),在本例中该文件名ValidationAction-validation.xml,内容如下:

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE validators PUBLIC 
          "-//OpenSymphony Group//XWork Validator 1.0//EN" 
          "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd"
>          
< validators >
   
< field name ="reqiuredString" >
       
< field-validator type ="requiredstring" >
           
< message > This string is required </ message >
       
</ field-validator >
   
</ field >
</ validators >

发布运行应用程序,在地址栏中键入http://localhost:8080/Struts2_Validation/Input.jsp,出现如图4所示页面。

图4 Input.jsp
图4 Input.jsp

直接点击“Submit”提交表单,出现图5所示的页面。

图5 错误提示
图5 错误提示

在Required String中随便填点东西,转到Output.jsp页面,如图6所示。

图6 Output.jsp
图6 Output.jsp

通过上面的例子,大家可以看到使用该校验框架十分简单方便。不过,上例还有两点不足:

  1. 还没有国际化错误消息;
  2. 没有实现客户端的校验。

当然,要完善以上不足,对于Struts 2.0来说,只是小菜一碟。

  1. 在Xxx-validation.xml文件中的<message>元素中加入key属性;
  2. 在Input.jsp中的<s:form>标志中加入validate="true"属性,就可以在用Javascript在客户端校验数据。

下面是具体的实现,首先在国际化资源文件中加入错误消息,然后按照上面说明实现。因为要使用Javascript在客户端显示出错信息,所以在加载Input.jsp页面时,Struts 2.0需要获得国际化的字符串,故我们需要使用Action来访问Input.jsp页面,具体实现请参考《在Struts 2.0中国际化(i18n)您的应用程序》的最后部分。最后发布运行应用程序,直接在页面中点击“Submit”,表单没有被提交并出现错误提示,如图7所示:

图7 客户端校验
图7 客户端校验

校验框架是通过validation拦截器实现,该拦载被注册到默认的拦截器链中。它在conversionError拦截器之后,在validateXxx()之前被调用。这里又出现了一个选择的问题:到底是应该在action中通过validateXxx()或validate()实现校验,还是使用validation拦截器?绝大多数情况,我建议大家使用校验框架,只有当框架满足不了您的要求才自已编写代码实现。

配置文件查找顺序

在上面的例子中,我们通过创建ValidationAction-validation.xml来配置表单校验。Struts 2.0的校验框架自动会读取该文件,但这样就会引出一个问题——如果我的Action继承其它的Action类,而这两个Action类都需要对表单数据进行校验,那我是否会在子类的配置文件(Xxx-validation.xml)中复制父类的配置吗?

答案是不,因为Struts 2.0的校验框架跟《在Struts 2.0中国际化(i18n)您的应用程序》提到的“资源文件查找顺序”相似,有特定的配置文件查找顺序。不同的是校验框架按照自上而下的顺序在类层次查找配置文件。假设以下条件成立:

  1. 接口 Animal;
  2. 接口 Quadraped 扩展了 Animal;
  3. 类 AnimalImpl 实现了 Animal;
  4. 类 QuadrapedImpl 扩展了 AnimalImpl 实现了 Quadraped;
  5. 类 Dog 扩展了 QuadrapedImpl;

如果Dog要被校验,框架方法会查找下面的配置文件(其中别名是Action在struts.xml中定义的别名):

  1. Animal-validation.xml
  2. Animal-别名-validation.xml
  3. AnimalImpl-validation.xml
  4. AnimalImpl-别名-validation.xml
  5. Quadraped-validation.xml
  6. Quadraped-别名-validation.xml
  7. QuadrapedImpl-validation.xml
  8. QuadrapedImpl-别名-validation.xml
  9. Dog-validation.xml
  10. Dog-别名-validation.xml

已有的校验器

Struts 2.0已经为您实现很多常用的校验了,以下在jar的default.xml中的注册的校验器。

< validators >
   
< validator name ="required" class ="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
   
< validator name ="requiredstring" class ="com.opensymphony.xwork2.validator.validators.RequiredStringValidator" />
   
< validator name ="int" class ="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator" />
   
< validator name ="double" class ="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator" />
   
< validator name ="date" class ="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator" />
   
< validator name ="expression" class ="com.opensymphony.xwork2.validator.validators.ExpressionValidator" />
   
< validator name ="fieldexpression" class ="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator" />
   
< validator name ="email" class ="com.opensymphony.xwork2.validator.validators.EmailValidator" />
   
< validator name ="url" class ="com.opensymphony.xwork2.validator.validators.URLValidator" />
   
< validator name ="visitor" class ="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator" />
   
< validator name ="conversion" class ="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator" />
   
< validator name ="stringlength" class ="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator" />
   
< validator name ="regex" class ="com.opensymphony.xwork2.validator.validators.RegexFieldValidator" />
</ validators >
关于每个校验器的具体用法,请参考以下链接:
http://wiki.javascud.org/display/ww2cndoc/Validation
该链接中还有一些很有的信息,请大家仔细阅读。

总结

使用校验框架既可以方便地实现表单数据校验,又能够将校验与Action分离,故我们应该尽可能使用校验框架。在使用校验框架时,请不要忘记通过在资源文件加入invalid.fieldvalue.xxx字符串,显示适合的类型转换出错信息;或者使用conversion校验器。

posted on 2006-11-14 13:38 Max 阅读(51335) 评论(118)  编辑  收藏 所属分类: Struts 2.0系列
评论共2页: 上一页 1 2 

评论:
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-11-15 15:51 | ronghai
你好怎么样才能联系到你,好好了解一些struts2。发现1.X和2有太大的差别了,刚把1.x弄得差不多,看2觉得很不是理解。  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-11-16 09:01 | Max
你可以通过email联系我, max.m.yuan@gmail.com  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-11-22 13:53 | 太行山人
正在学2.0,您写的太好了,收藏下来仔细研读  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-11-27 11:43 | breezedancer
写的很好,转到我的论坛www.51forum.uni.cc,如果有什么不当,请联系管理员,谢谢~~~  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-12-04 22:28 | zjlovezj
谢谢楼主,hello world是通往实际应用的必由之路~

冒昧提多个要求:
请写写spring2 和 struts2结合起来使用的HELLO WORLD,最好加上个hibernate3.2  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-12-06 01:30 | lhj
嗯,,同楼上,希望老大弄个spring2和struts2甚至hibernate结合的例子,重点讲下配置要点和原理,呵呵,不情之请,见谅!  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-12-06 09:35 | Max
谢谢大家的建议,我计划在以后文章中介绍Struts 2与一些流行的框架的搭配。  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2006-12-06 10:32 | sxf
写的好,加油!  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-02-01 15:59 | Sophia
Max,最近在尝试Struts2的Validation Annotation。发现@Annotation是需要引用一个com.opensymphony.xwork2.validator.annotations包下的@interface Validation。这个package被打到了xwork-2.0-beta-1.jar里面,但是struts官网上提供的jar里面却并未含有这个包。
我在网上找了一下,在http://ivyrep.opensymphony.com/opensymphony/xwork/ 下面找了个最新的xwork-2.0.0.jar 来用。但是tomcat启动的时候,报错struts - cleanup之类的。换回原来的xwork-2.0-beta-1.jar就没问题。
想请教一下,用Annotation方法如何实现Validation?
谢谢。  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-02-02 12:51 | Sophia
补充一下,所谓的struts - cleanup报错是因为在web.xml中配置了这个filter。
同样web.xml中配置的struts2的filter也会出错。
<code>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</code>
那个xwork-2.0.0.jar是webwork提供的。
我将需要用到的几个包单独拉出来打包就可以了。
com.opensymphony.xwork2.validator
com.opensymphony.xwork2.validator.annotations
com.opensymphony.xwork2.validator.metadata
com.opensymphony.xwork2.validator.validators
看来是beta版的原因啊!  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-02-02 12:52 | Sophia
sorry, 不会贴代码,嗬嗬,那个<code></code>请省略不看  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-02-13 01:21 | mos
如何在struts2中使用js呢?  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-03-02 16:13 | z
test
  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-03-06 16:02 | mz
请问报这个错是哪没有配好?

Method public java.lang.String com.opensymphony.xwork2.validator.validators.ValidatorSupport.getMessage(java.lang.Object) threw an exception when invoked on com.opensymphony.xwork2.validator.validators.RequiredStringValidator@156f920
The problematic instruction:
----------
==> ${validator.getMessage(action)?js_string} [on line 28, column 26 in template/xhtml/form-close-validate.ftl]
in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 3, column 1 in template/xhtml/form-close.ftl]
----------  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-04-18 17:40 | huhuyeah
看了这篇文章实验了一下客户端验证
login.jsp:
<s:fielderror></s:fielderror>
<p>&nbsp;</p>
<s:form action="login" validate="true">
用户名:<s:textfield name="username"/>
密 码:<s:password name="password" />
<s:submit/>
</s:form>

struts.xml:
<package name="loginout" extends="hello-default" namespace="/">
<action name="login" class="com.jetch.action.LoginAction">
<result name="success">/WEB-INF/jsp/main.jsp</result>
<result name="input">/WEB-INF/jsp/login.jsp</result>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>


校验的xml配置就是必须要username跟password
配置之后,服务器端的验证已经成功了
不过奇怪的是,看了帮助文件,在form标签里面加入validate="true" 之后,客户端的校验却不起作用
生产的源文件没有js也没有onsubmit,请帮忙看看原因。谢谢   回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-04-18 22:54 | Max
@huhuyeah
你可以忘记在<head></head>中加入<s:head />  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation) 2007-04-19 09:16 | huhuyeah
谢博主,加了这后生产的页面源码有JS了,如下
<script language="JavaScript" type="text/javascript">
// Dojo configuration
djConfig = {
baseRelativePath: "/hello/struts/dojo",
isDebug: false,
bindEncoding: "UTF-8",
debugAtAllCosts: true // not needed, but allows the Venkman debugger to work with the includes
};
</script>
<script language="JavaScript" type="text/javascript"
src="/hello/struts/dojo/dojo.js"></script>
<script language="JavaScript" type="text/javascript"
src="/hello/struts/simple/dojoRequire.js"></script>
但是客户端验证还是没有成功
生成的form没有onsubmit,如果把 validate="true" 去掉。
在form里面有onsubmit="return true"的js生成
如何调用生成的JS?
  回复  更多评论
  
# re: 在Struts 2.0中实现表单数据校验(Validation)[未登录] 2007-04-19 17:42 | test
把validate="true"这个加上之后会出现
FreeMarker template error!

Method public java.lang.String com.opensymphony.xwork2.validator.validators.ValidatorSupport.getMessage(java.lang.Object) threw an exception when invoked on com.opensymphony.xwork2.validator.validators.RequiredStringValidator@10699ea
The problematic instruction:
----------
==> ${validator.getMessage(action)?js_string} [on line 28, column 26 in template/xhtml/form-close-validate.ftl]
in include "/${parameters.templateDir}/xhtml/form-close-validate.ftl" [on line 3, column 1 in template/xhtml/form-close.ftl]
----------

Java backtrace for programmers:
----------
freemarker.template.TemplateModelException: Method public java.lang.String com.opensymphony.xwork2.validator.validators.ValidatorSupport.getMessage(java.lang.Object) threw an exception when invoked on com.opensymphony.xwork2.validator.validators.RequiredStringValidator@10699ea.......  回复  更多评论
  
评论共2页: 上一页 1 2 

只有注册用户登录后才能发表评论。


网站导航: