当前位置: 代码迷 >> Java Web开发 >> struts2 短路校验问题?
  详细解决方案

struts2 短路校验问题?

热度:190   发布时间:2010-03-01 10:11:33.0
struts2 短路校验问题?
struts2短路校验都说是把 short-circuit="true",但是发现还是校验的时候会全部出来,没有发生短路情况。在struts.properies中有设置struts.ui.theme=simple,不知道各位是怎么弄短路验证的?难道不要用struts2的校验框架?

部分代码:
regist.jsp
<s:form action="registAction" name="regist" namespace="/ebuy" validate="true" >
<table id="tableID" align="center" cellspacing="0" border="0"
cellpadding="0" width="100%">
<tr>
<td align="right" width="40%">用户名:</td>
<td align="left"><s:textfield id="username" name="username"
label="用户名" cssStyle="width:150px;" /> <s:fielderror cssClass="errorText">
<s:param>username</s:param>
</s:fielderror></td>
</tr>

<tr>
<td align="right">年龄:</td>
<td align="left"><s:textfield name="age" label="年龄" cssStyle="width:150px;"></s:textfield>
<s:fielderror cssClass="errorText">
<s:param>age</s:param>
</s:fielderror></td>
</tr>

<tr>
<td colspan="2" align="center"><s:submit value="注册" /></td>
</tr>
</table>
</s:form>
校验文件:RegistAction-validation.xml
<validators>
<!-- 指定username表单域的校验规则 -->
<field name="username">
<!-- username必填 -->
<field-validator type="requiredstring" short-circuit="true">
    <param name="trim">true</param>
<message>用户名必填</message>
</field-validator>
<!-- 必须匹配一个正则表达式 -->
<field-validator type="regex">
<param name="expression"><![CDATA[(\w{6,20})]]></param>
<message>您输入的用户名只能是字母和数字,且长度必须在6到20之间</message>
</field-validator>
</field>
<!-- 年龄验证 -->
<field name="age">
<field-validator type="int" short-circuit="true">
   <param name="min">1</param>
   <param name="max">300</param>
<message>年龄必须在1到300之间!</message>
</field-validator>
</field>
</validators>
搜索更多相关主题的帖子: 短路  校验  

----------------解决方案--------------------------------------------------------
大侠们帮帮忙吧!
----------------解决方案--------------------------------------------------------
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的官网的指南上获得的说明,其实意思就是说:
短路适用的情况是其他要检验的字段引用了在此之前被检验的字段的时候,如果前一个检验失败,后面的相关检验就
不被执行。而不是同一个字段的检验是互不相干,并不会因为在配置文件中靠前字段的检验失败,从而就不去检验其他
后面的字段了。
下面的官网的例子描述了短路的情况:

<!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>
上面第一个或者第二个检验失败的话,那么第三个就不会执行了。
第一个检验失败的话,最后一个就不会执行了。
不知道现在你明白了没有。




[ 本帖最后由 baifenghan 于 2010-4-2 20:50 编辑 ]
----------------解决方案--------------------------------------------------------
  相关解决方案