Spring MVC驗證的配置步驟

來源:互聯網
上載者:User

  這是我在練習MVC驗證時的大致步聚,其中不少檔案來源於appFuse架構.先記下來,供自己以後參考. ^_^

看這個步驟前,最好先看一下” jamyy2000的專欄”(我已經在我的另一篇文章中做了轉寄).

1.從appFuse中拷貝 ApplicationResources_zh_CN.properties 和 ApplicationResouces.properties到項目的src根目錄下

2.拷貝 validation.xml 和 validator-rules.xml 到WebRoot/WEB-INF下

3.在web.xml中加:
    <!--載入資源檔-->
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>ApplicationResources</param-value>
    </context-param>

4.在application中加:
    <bean id="validatorFactory" class="org.springmodules.validation.commons.DefaultValidatorFactory">
        <property name="validationConfigLocations">
            <list>
                <value>/WEB-INF/validation.xml</value>
                <value>/WEB-INF/validator-rules.xml</value>
            </list>
        </property>
    </bean>

    <bean id="beanValidator" class="org.springmodules.validation.commons.DefaultBeanValidator">
        <property name="validatorFactory" ref="validatorFactory"/>
    </bean>
   
   
   <!-- 載入資源檔 -->
   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="ApplicationResources"/>
    </bean>

5.controller的配置為:
 <bean id="controller.TestCtrl" class="controller.TestCtrl">
  <property name="formView">     -->剛開始進去時要執行的JSP檔案
    <value>/jsp/test</value>
  </property>
  <property name="successView">   -->在controller中判斷如果執行submit後,要去的JSP
    <value>/8jsearch/jsp/welcome.jsp</value>
  </property>
  <property name="commandName" value="user"/>   -->domain類名,注意一定要小寫,
         並且和validation.xml中form的名字一樣
  <property name="commandClass" value="domain.User"/>
  <property name="validator" ref="beanValidator"/>  
 </bean>

6.(1)下載springmodules.jar時,一個要下那個帶依賴類的, 要驗證email時,需要匯入包 oro-2.0.8.jar
  (2)antlr/錯誤,少一個antlr.jar包,是用來掃描碼用的,是代碼掃描器.hibernate3.0包中,就用它來翻譯hql語句. 可以到www.antlr.org下載
  (3)我用到的jar包:
     antlr-2.7.5.jar
     commons-beanutils-1.7.0.jar
     commons-digester-1.7.jar
     commons-validator-1.3.0.jar
     jstl.jar
     oro-2.0.0.jar
     spring-modules-0.5-all.jar
     spring.jar
     standard.jar

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.