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

来源:互联网 发布:在淘宝买到假货怎么办 编辑:程序博客网 时间:2024/06/06 11:01

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代码进行校验!

 

原创粉丝点击