随笔-61  评论-159  文章-0  trackbacks-0
        在struts2中使用validation校验框架,如果想对某个类似于execute方法进行校验,xml的校验文件的命名规则为action名-方法名-validation.xml,对应action中继承另外一个action两者都有校验xml,那么校验顺序如下:
RegisterAction extends BaseAction,如果两个action里面要对方法进行校验,那校验文件的搜索路径如下:

BaseAction-validation.xml,BaseAction-Xxx-validation.xml,RegisterAction-validation.xml,Register-Xxx-validation.xml

         短路校验,在字段校验或者非字段校验中加入:short-circuit="true"如果某个被标记为短路的校验器失败了,将会阻止其他后续的校验器的进行,然后一个错误(action错误或者字段错误,取决于校验器的类型)将会被添加到被校验的对象的ValidationContext中
                     
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<!-- Field Validators for email field -->
<field name="email">
<field-validator type="required" short-circuit="true">
<message>You must enter a value for email.</message>
</field-validator>
<field-validator type="email" short-circuit="true">
<message>Not a valid e-mail.</message>
</field-validator>
</field>
<!-- Field Validators for email2 field -->
<field name="email2">
<field-validator type="required">
<message>You must enter a value for email2.</message>
</field-validator>
<field-validator type="email">
<message>Not a valid e-mail2.</message>
</field-validator>
</field>
<!-- Plain Validator 1 -->
<validator type="expression">
<param name="expression">email.equals(email2)</param>
<message>Email not the same as email2</message>
</validator>
<!-- Plain Validator 2 -->
<validator type="expression" short-circuit="true">
<param name="expression">email.startsWith('mark')</param>
<message>Email does not start with mark</message>
</validator>
</validators>

在上面的例子中,实际的校验器执行是这样的:

  1. Plain Validator 1
  2. Plain Validator 2
  3. email字段的字段校验器
  4. email2字段的字段校验器

因为普通校验器2是短路的,如果它的校验失败,它会导致email字段的校验器和email2字段的校验器不会被执行.




-------------------------------------------------------------------------------------------------
PS:本博客文章,如果没有注明是有“转”字样,属于本人原创。如果需要转载,务必注明作者文章的详细出处地址,否则不允许转载,多谢合作!
posted on 2008-11-29 21:35 apple0668 阅读(1139) 评论(0)  编辑  收藏 所属分类: struts2

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


网站导航: