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">
<field name="被校验的字段"><br style="line-height:25px">
<field-validator type="校验器名"><br style="line-height:25px">
<!--此处需要为不同校验器指定数量不等的校验规则--><br style="line-height:25px">
<param name="参数名">参数值</param><br style="line-height:25px">
....................<br style="line-height:25px">
<!--校验失败后的提示信息,其中key指定国际化信息的key--><br style="line-height:25px">
<message key="I18Nkey">校验失败后的提示信息</message><br style="line-height:25px">
<!--校验失败后的提示信息:建议用getText("I18Nkey"),否则可能出现Freemarker template Error--><br style="line-height:25px">
</field-vallidator><br style="line-height:25px">
<!-- 如果校验字段满足多个规则,下面可以配置多个校验器--><br style="line-height:25px">
</field><br style="line-height:25px">
非字段校验器配置格式:<br style="line-height:25px">
<validator type="校验器名"><br style="line-height:25px">
<param name="fieldName">需要被校验的字段</param><br style="line-height:25px">
<!--此处需要为不同校验器指定数量不等的校验规则--><br style="line-height:25px">
<param name="参数名">参数值</param><br style="line-height:25px">
<!--校验失败后的提示信息,其中key指定国际化信息的key--><br style="line-height:25px">
<message key="I18Nkey">校验失败后的提示信息</message><br style="line-height:25px">
<!--校验失败后的提示信息:建议用getText("I18Nkey"),否则可能出现Freemarker template Error--><br style="line-height:25px">
</validator><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"/><br style="line-height:25px">
<!-- 字段表达式校验器 --><br style="line-height:25px">
<validator name="fieldexpression" class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/><br style="line-height:25px">
<!-- 邮件校验器 --><br style="line-height:25px">
<validator name="email" class="com.opensymphony.xwork2.validator.validators.EmailValidator"/><br style="line-height:25px">
<!-- 网址校验器 --><br style="line-height:25px">
<validator name="url" class="com.opensymphony.xwork2.validator.validators.URLValidator"/><br style="line-height:25px">
<validator name="visitor" class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/><br style="line-height:25px">
<!-- 转换器校验器 --><br style="line-height:25px">
<validator name="conversion" class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator"/><br style="line-height:25px">
<!-- 字符串长度校验器 --><br style="line-height:25px">
<validator name="stringlength" class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/><br style="line-height:25px">
<!-- 正则表达式校验器 --><br style="line-height:25px">
<validator name="regex" class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/><br style="line-height:25px">
<validator name="conditionalvisitor" class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator"/><br style="line-height:25px">
</validators><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属性指定了被校验的字段;如果该属性满足多个规则,则在该<field.../>下增多个<field-validator.../>。<br style="line-height:25px"> 每个<field-validator .../>指定一个校验规则,该元素的type属性指定校验器名称该元素可以包含多个<param .../>子元素,用户指定该校验器的参数;除外,每个<field-validator .../>元素都有一个必须的<message .../>元素,用户确定校验失败后的提示信息。</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></wbr></wbr>
例:
<!-- 配置一个名为user的Act<wbr style="line-height:25px">ion,对应的逻辑方法为UserAction的login方法--></wbr>
<act<wbr style="line-height:25px">ion name="login" class="com.test.act<wbr style="line-height:25px">ion.UserAction" method="login"><br style="line-height:25px"> <result name="input">/login.jsp</result><br style="line-height:25px"> <result name="success">/index.jsp</result><br style="line-height:25px"> </act<wbr style="line-height:25px">ion></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"属性即可。
相关推荐
struts2输入校验总结,struts2自带的校验器,以及校验器不能做的check的处理的总结
NULL 博文链接:https://fangguanhong.iteye.com/blog/1779291
java,struts2的输入校验总结大全
struts2 最新的入门实例 我自己总结的 呵呵,欢迎提出宝贵的意见 1.Struts2_01_login 对应登录。。 login.jsp---------------------------------------html标签的登陆页面 login_struts2.jsp--------------...
输入校验 ————复习前面的 10.Struts2_04_validate_method ????????????? validateXXX方法校验得到的错误信息先显示出来,然后才是validate方法校验的字段错误信息 validate方法在...
输入校验 ————复习前面的 10.Struts2_04_validate_method ????????????? validateXXX方法校验得到的错误信息先显示出来,然后才是validate方法校验的字段错误信息 validate方法在...
输入校验 ————复习前面的 10.Struts2_04_validate_method ????????????? validateXXX方法校验得到的错误信息先显示出来,然后才是validate方法校验的字段错误信息 validate方法在...
struts2框架的输入校验与校验框架,这都是上课的总结笔记
struts2.doc struts 1 和struts 2比较 Action 类 线程模式 Servlet 依赖 可测性 捕获输入 表达式语言 绑定值到页面(view) 类型转换 校验 Action执行的控制 等 自己总结的一些东西
输入校验 国际化支持 Webwork2 in Spring Struts in Spring 数据持久层 事务管理 持久层封装 JDBC Hibernate in Spring ibatis in Spring Aspect Oriented Programming AOP 概念 AOP in Spring ...
1 此系统采用了目前最流行的ssm框架,其中的spingMVC框架相对于struts2框架更灵活,更安全。 2 本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3 同时使用了了...
2.28.2 Struts2校验框架应用 71 2.28.2.1 校验配置文件 71 2.28.2.2 角色Insert页面示例 71 2.28.2.3 用户Query页面示例 73 2.28.2.4 多行输入域校验 75 2.29 多语言管理 77 2.29.1 定义输入域的name属性和资源文件 ...
2.继承: 继承是一种联结类的层次模型,并且允许和鼓励类的重用,它提供了一种明确表述共性的方法。对象的一个新类可以从现有的类中派生,这个过程称为类继承。新类继承了原始类的特性,新类称为原始类的派生类...
4、请写出用于校验HTML文本框中输入的内容全部为数字的javascript代码 84 5、说说你用过那些ajax技术和框架,说说它们的区别 85 四. Java web部分 85 1、Tomcat的优化经验 85 2、HTTP请求的GET与POST方式的区别 85 3...
15.4. Struts 15.4.1. ContextLoaderPlugin 15.4.1.1. DelegatingRequestProcessor 15.4.1.2. DelegatingActionProxy 15.4.2. ActionSupport 类 15.5. Tapestry 15.5.1. 注入 Spring 托管的 beans 15.5.1.1. 将 ...