Struts2框架验证--短路较验(short-circuit)

Short cuircuiting and validator flavours

A FieldValidator that gets shortcircuited will only prevent other FieldValidators for the same field
from being evaluated. Note that this "same field" behavior applies regardless of whether the or syntax
was used to declare the validation rule. By way of example, given this validation.xml file:

<validator type="required" short-circuit="true">
  <param name="fieldName">bar</param>
  <message>You must enter a value for bar.</message>
</validator>
<p/>
<validator type="expression">
  <param name="expression">foo gt bar</param>
  <message>foo must be great than bar.</message>
</validator>

both validators will be run, even if the "required" validator shortcircuits. "required" validators are
FieldValidator's and will not shortcircuit the plain ExpressionValidator because FieldValidators only
shortcircuit other checks on that same field. Since the plain Validator is not field specific, it is notshort-circuited.

上面是apache的官网的指南上获得的说明,其实意思就是说:短路适用的情况是其他要检验的字段引用了在此之前被检验的字段的时候,如果前一个检验失败,后面的相关检验就不被执行。而不是同一个字段的检验是互不相干,并不会因为在配置文件中靠前字段的检验失败,从而就不去检验其他后面的字段了。

校验器的执行顺序:

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

校验器短路的原则(有待确定):

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

对于复杂的校验最好写java代码进行校验!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值