struts 使用validate

來源:互聯網
上載者:User

 

validate使用了2個設定檔,一個是validation.xml,一個是validator-rules.xml.分別就2者的用途作一
描述:
1) validator-rules.xml:
 定義一些通用的javascript校正規則.注意--如果<validator>標籤含有一個名為:
msg屬性,則表示這個屬性的值在資源檔中被定義.例如:
    <validator name="requiredif"
                 classname="org.apache.struts.validator.FieldChecks"
                 method="validateRequiredIf"
                 methodParams="java.lang.Object,
                               org.apache.commons.validator.ValidatorAction,
                               org.apache.commons.validator.Field,
                               org.apache.struts.action.ActionErrors,
                               org.apache.commons.validator.Validator,
                               javax.servlet.http.HttpServletRequest"
                 msg="errors.required">
      </validator>
其中msg=errors.required,在屬性檔案中找到:
errors.required={0} is required.
這樣就實現了通用校正規則的動態資訊提示

我們也可以自己擴充這個檔案,定義我們需要的校正規則,只要參照裡面的格式就行.

 

2)  validation.xml:

 針對頁面form表單項指定校正規則.如
  <form    name="registrationForm">
         <field    property="firstName"
             depends="required,mask,minlength">
               <arg0 key="registrationForm.firstname.displayname"/>
               <arg1 name="minlength" key="${var:minlength}" resource="false"/>
                     <var>
                       <var-name>mask</var-name>
                       <var-value>^/w+$</var-value>
                     </var>
                     <var>
                       <var-name>minlength</var-name>
                       <var-value>5</var-value>
                     </var>
         </field>
         ...
        
    其中form 屬性中的name值要和struts-config.xml中的formbean的屬性值一樣.
    field屬性工作表示表單項.  
    depends表示該表單項需要進行的校正判斷.
    <arg0 key=...>表示校正firstName屬性時第一種可能顯示的錯誤資訊.
    <arg1 name="minlength" key="${var:minlength}" resource="false"/>中key的表示在validator-rules.xml
    中定義了名為minlength的校正規則. name="minlength"中的minlength變數名在下面使用:
                     <var>
                       <var-name>minlength</var-name>
                       <var-value>5</var-value>
                     </var>   

   表示傳入的值為5.

3)啟用檢驗規則:在struts-config.xml中加入下面2段定義.(注意:在<action-mappings> 定義結束後添加,
                                                      不同版本,參照不同的tld定義)
  
  <!-- ========== Message Resources Definitions =========================== -->

  <message-resources
    parameter="org.apache.struts.webapp.validator.ApplicationResources"/>

  <!-- ========== Plug Ins Configuration ================================== -->
 
  <!--
     Add multiple validator resource files by setting the pathnames property
     with a comma delimitted list of resource files to load.
  -->
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,
                                              /WEB-INF/validation.xml"/>
  </plug-in>  
 
  這樣校正規則即告生效.
 
4)在jsp顯示錯誤資訊:

 在jsp頁面加上:
 <logic:messagesPresent>
    <ul>
    <html:messages id="error">
       <li><bean:write name="error"/></li>
    </html:messages>
    </ul><hr>
 </logic:messagesPresent>

5)對應的form應該繼承ValidatorForm類
 public final class RegistrationForm extends ValidatorForm implements Serializable
 {
  ...
 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.