Struts2输入校验总结

本文介绍Struts2框架中的输入校验机制,包括手动输入校验、基于验证框架的输入校验流程及配置,同时讲解了校验器的执行顺序、短路原则等内容。

一. 手动输入完成校验
1.普通的处理方式:只需要在act<wbr style="line-height:25px">ion中重写validate()方法<br style="line-height:25px"> 2.一个act<wbr style="line-height:25px">ion对应多个逻辑处理方法:指定校验摸个特定方法的方式:<br style="line-height:25px"> 重写validateXxxx()方法。Eg:如果,只校验login方法,则只需重写validateLogin().</wbr></wbr>

二. 输入校验流程:
1. 类型转换器负责对字符串的请求参数执行类型转换。并将这些值设置成Act<wbr style="line-height:25px">ion的属性值。<br style="line-height:25px"> 2. 转换过程中若出现异常,则将异常信息保存到ActionContext中,conversionError拦截器将其封装到fieldError中。<br style="line-height:25px"> 3. 通过反射调用validateXxx()方法。<br style="line-height:25px"> 4. 调用validate().<br style="line-height:25px"> 5. 如果未出现异常,则转入用户请求的处理方法,如果出现异常,则转入inout视图所指定的视图资源(所以,对于验证的方法,必须要在配置文件中为其指定input)。<br style="line-height:25px"> 三. 基于验证框架的输入校验<br style="line-height:25px"> 1. 编写校验配置文件:命名规则:act<wbr style="line-height:25px">ion类名-validatin.xml.<br style="line-height:25px"> 2. 一个act<wbr style="line-height:25px">ion对应多个逻辑处理方法:指定校验摸个特定方法的方式:<br style="line-height:25px"> act<wbr style="line-height:25px">ion类名-name属性名-validatin.xml.(name属性名:在strtus配置文件中的)<br style="line-height:25px"> 3. 配置文件存放位置:放在与Act<wbr style="line-height:25px">ion相同的文件夹内。<br style="line-height:25px"> 4. 验证规则:先加载act<wbr style="line-height:25px">ion类名-validatin.xml,然后加载act<wbr style="line-height:25px">ion类名-name属性名-validatin.xml文件。<br style="line-height:25px"> 5. 校验器的配置风格:两种:字段校验器,非字段校验器。<br style="line-height:25px"> 字段校验器配置格式:<br style="line-height:25px"> &lt;field name="被校验的字段"&gt;<br style="line-height:25px"> &lt;field-validator type="校验器名"&gt;<br style="line-height:25px"> &lt;!--此处需要为不同校验器指定数量不等的校验规则--&gt;<br style="line-height:25px"> &lt;param name="参数名"&gt;参数值&lt;/param&gt;<br style="line-height:25px"> ....................<br style="line-height:25px"> &lt;!--校验失败后的提示信息,其中key指定国际化信息的key--&gt;<br style="line-height:25px"> &lt;message key="I18Nkey"&gt;校验失败后的提示信息&lt;/message&gt;<br style="line-height:25px"> &lt;!--校验失败后的提示信息:建议用getText("I18Nkey"),否则可能出现Freemarker template Error--&gt;<br style="line-height:25px"> &lt;/field-vallidator&gt;<br style="line-height:25px"> &lt;!-- 如果校验字段满足多个规则,下面可以配置多个校验器--&gt;<br style="line-height:25px"> &lt;/field&gt;<br style="line-height:25px"> 非字段校验器配置格式:<br style="line-height:25px"> &lt;validator type="校验器名"&gt;<br style="line-height:25px"> &lt;param name="fieldName"&gt;需要被校验的字段&lt;/param&gt;<br style="line-height:25px"> &lt;!--此处需要为不同校验器指定数量不等的校验规则--&gt;<br style="line-height:25px"> &lt;param name="参数名"&gt;参数值&lt;/param&gt;<br style="line-height:25px"> &lt;!--校验失败后的提示信息,其中key指定国际化信息的key--&gt;<br style="line-height:25px"> &lt;message key="I18Nkey"&gt;校验失败后的提示信息&lt;/message&gt;<br style="line-height:25px"> &lt;!--校验失败后的提示信息:建议用getText("I18Nkey"),否则可能出现Freemarker template Error--&gt;<br style="line-height:25px"> &lt;/validator&gt;<br style="line-height:25px"> 非字段校验:先指定校验器:由谁来校验,来校验谁!<br style="line-height:25px"> 字段校验器:先指定校验的属性:我来校验谁,由谁来校验!</wbr></wbr></wbr></wbr></wbr></wbr></wbr>

6. 校验器:struts2提供了大量的内置校验器:你可以在xwork-core-2.1.6.jar的com.opensymphony.xwork2.validator.validators下找到如下配置文件:default.xml.里面列出了所有的内置校验器。

----------------------------------------------------------

struts2表单验证里field-validator type值:

int 整数;
double 实数;
date 日期;
expr<wbr style="line-height:25px">ession 两数的关系比较;<br style="line-height:25px"> email Email地址;<br style="line-height:25px"> url<br style="line-height:25px"> visitor<br style="line-height:25px"> conversion<br style="line-height:25px"> regex 正则表达式验证;<br style="line-height:25px"> required 是否为空;<br style="line-height:25px"> requiredstring 必须字符;<br style="line-height:25px"> stringlength 输入字符长度限制;</wbr>

url web地址

visitor 访问

conversion 转换

-------------------------------------------------------------

<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="long" class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>
<validator name="short" class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/>
<validator name="double" class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/>
<!-- 日期校验器 -->
<validator name="date" class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>
<!-- 表达式校验器 -->
<validator name="expr<wbr style="line-height:25px">ession" class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/&gt;<br style="line-height:25px"> &lt;!-- 字段表达式校验器 --&gt;<br style="line-height:25px"> &lt;validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/&gt;<br style="line-height:25px"> &lt;!-- 邮件校验器 --&gt;<br style="line-height:25px"> &lt;validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/&gt;<br style="line-height:25px"> &lt;!-- 网址校验器 --&gt;<br style="line-height:25px"> &lt;validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/&gt;<br style="line-height:25px"> &lt;validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/&gt;<br style="line-height:25px"> &lt;!-- 转换器校验器 --&gt;<br style="line-height:25px"> &lt;validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/&gt;<br style="line-height:25px"> &lt;!-- 字符串长度校验器 --&gt;<br style="line-height:25px"> &lt;validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/&gt;<br style="line-height:25px"> &lt;!-- 正则表达式校验器 --&gt;<br style="line-height:25px"> &lt;validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/&gt;<br style="line-height:25px"> &lt;validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/&gt;<br style="line-height:25px"> &lt;/validators&gt;<br style="line-height:25px"> 这些校验器已经可以满足大多数的验证需求,如果还需要特殊的要求,建议直接采用java代码搞定,除非你有很多地方都要用到,此时你可以自定义一个校验器。</wbr>

四. 校验器的执行顺序
1. 所有非字段校验风格的校验器优先于字段校验风格的校验器;
2. 所有非字段校验风格的校验器中,排在前面的会先执行;
3. 所有字段校验风格的校验器,排在前面的会先执行;

五. 校验器的短路原则
1. 所有非字段检验器时最优先执行的,如果某个非字段校验器校验失败了,则该字段上的所有字段校验器都不会获得校验机会;
2. 非字段校验校验失败,不会阻止其他非字段校验执行;
3. 如果一个字段校验器校验失败后,则该字段下且排在该校验失败后的检验器之后的其他字段校验器不会获得校验机会;
4. 字段校验器永远不会阻止非字段校验器的执行!

六. 短路校验器

短路校验器:只需在<validator>或<filed-validator>元素中添加short-circuit=”true”属性。
注:在struts2.1.6版本,已经支持客户端的短路校验。
七. 客户端的校验:
在<s:form>中添加validate=”true”.

Struts2提供了客户端校验与服务端校验。这里只是做个简易的总结。

1.)服务端校验:
当程序流程进入到Act<wbr style="line-height:25px">ion类,并经历了struts2的类型转换阶段后。接着进入struts2的输入校验阶段。<br style="line-height:25px"> struts2的输入校验可自己编码进行校验,也可通过有规则命名的配置文件进行校验。</wbr>

1.1)编码校验:

在Act<wbr style="line-height:25px">ion类里重写父类的validate()方法即可。</wbr>

public void validate(){

if(null == username || "".equals(username)){

this.addFieldError("username","username should not be empty !");

}else if(username.length() < 6 || username.length() > 12){

this.addFieldError("username","username should be between 6 and 12 !");

}

if(null == password || "".equals(password )){

this.addFieldError("password ","password should not be empty !");

}else if(password .length() < 6 || password .length() > 12){

this.addFieldError("password ","password should be between 6 and 12 !");

}

}

在上面的validate方法中,一旦发现校验失败,就把失败提示通过addFieldError方法添加到系统的fieldError中。校验完毕后,若系统的fieldError不为空,则自动转到input视图对应的JSP页面中输出错误提示,这与类型转换失败后的处理是完全一样的。

为了在input视图中对应的JSP页面输出错误提示,应该在该页面中增加如下代码:

<!-- 输入类型转换失败提示和校验失败提示 -->

<s:fielderror/>


这里不能忽略另外种情况:

Struts2的Act<wbr style="line-height:25px">ion类里可以包含多个处理逻辑,不同的处理逻辑对应不同的方法。即Struts2的Act<wbr style="line-height:25px">ion类里定义了几个类似于execute的方法,只是方法名不同。而重写validate方法无法知道需要校验的是哪个处理逻辑。实际上重写validate方法会校验所有的处理逻辑。</wbr></wbr>

为了校验指定处理逻辑,需提供一个validateXxx()方法,其中xxx为Act<wbr style="line-height:25px">ion对应的处理逻辑方法。</wbr>


例如Act<wbr style="line-height:25px">ion类里有个login()逻辑方法,那么对应的校验方法validateLogin()方法。</wbr>

但之后还是会调用validate()进行校验,因此此时validate()没必要写了,或者让validate()进行公共字段的校验。


1.2)配置文件校验

只需为Act<wbr style="line-height:25px">ion指定一个校验文件即可。</wbr>

在Act<wbr style="line-height:25px">ion类所在的com.test.act<wbr style="line-height:25px">ion包下建立LoginAction-validation.xml校验文件(格式:Act<wbr style="line-height:25px">ion类名-validation.xml).</wbr></wbr></wbr>

LoginAction-validation.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username should not be empty !</message>
</field-validator>

<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message>username should be between ${minLength} and ${maxLength}!</message>
</field-validator>
</field>

<field name="password">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>password should not be empty !</message>
</field-validator>

<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">12</param>
<message>password should be between ${minLength} and ${maxLength}!</message>
</field-validator>
</field>

</validators>

每个<field.../>元素指定一个Act<wbr style="line-height:25px">ion属性必须遵守的规则,该元素name属性指定了被校验的字段;如果该属性满足多个规则,则在该&lt;field.../&gt;下增多个&lt;field-validator.../&gt;。<br style="line-height:25px"> 每个&lt;field-validator .../&gt;指定一个校验规则,该元素的type属性指定校验器名称该元素可以包含多个&lt;param .../&gt;子元素,用户指定该校验器的参数;除外,每个&lt;field-validator .../&gt;元素都有一个必须的&lt;message .../&gt;元素,用户确定校验失败后的提示信息。</wbr>

例子中只采用了"必填字符串校验器"和"字符串长度校验器"2个校验规则。

struts2貌似共提供了13个校验器:
required:必填校验器
requiredstring:必填字符串校验器
int:整数校验器
double:双精度浮点数校验器
date:日期校验器
expr<wbr style="line-height:25px">ession:表达式校验器<br style="line-height:25px"> fieldexpression:字段表达式校验器<br style="line-height:25px"> email:电子邮件校验器<br style="line-height:25px"> url:网址校验器<br style="line-height:25px"> visitor:Visitor校验器<br style="line-height:25px"> conversion:转换校验器<br style="line-height:25px"> stringlength:字符串长度校验器<br style="line-height:25px"> regex:正则表达式校验器</wbr>

用法大部分雷同,用到的时候可自己google搜索。


配置文件校验肯定不输给编码校验的,自然也提供了对应于具体业务逻辑的校验规则文件的格式:

<Act<wbr style="line-height:25px">ion类名-Act<wbr style="line-height:25px">ion所包含的处理方法在struts.xml对应的name属性-validation.xml&gt;</wbr></wbr>

例:

<!-- 配置一个名为user的Act<wbr style="line-height:25px">ion,对应的逻辑方法为UserAction的login方法--&gt;</wbr>

<act<wbr style="line-height:25px">ion name="login" class="com.test.act<wbr style="line-height:25px">ion.UserAction" method="login"&gt;<br style="line-height:25px"> &lt;result name="input"&gt;/login.jsp&lt;/result&gt;<br style="line-height:25px"> &lt;result name="success"&gt;/index.jsp&lt;/result&gt;<br style="line-height:25px"> &lt;/act<wbr style="line-height:25px">ion&gt;</wbr></wbr></wbr>

则此逻辑方法对应的校验规则文件为:
UserAction-login-validation.xml

与编码校验一样,校验错误信息会添加到系统的fieldError中,校验完毕后若FieldError不为空。则进入到input视图对应的JSP。在<fieldError/>处输出错误提示。


提醒:没记错的话。与编码校验不同的是校验顺序,配置文件校验方式下,

先UserAction-validation.xml,再UserAction-login-validation.xml,

因此若是只针login()逻辑进行输入校验的话,那UserAction-validation.xml没必要写了,或让其进行公共字段的校验

2)客户端校验

增加客户端校验非常简单,将输入页面的表单元素改为使用struts2标签来生成表单,并且为该表单增加validate="true"属性即可。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值