Spring MVC 資料驗證——validate註解方式,mvcvalidate

來源:互聯網
上載者:User

Spring MVC 資料驗證——validate註解方式,mvcvalidate
1、說明

學習註解方式之前,應該先學習一下編碼方式的spring注入。這樣便於理解驗證架構的工作原理。在出錯的時候,也能更好的解決問題。所以本次部落格教程也是基於編碼方式,只是在原來的基礎加上註解方式。
2、配置資訊
  • web.xml不需要改變的
  • hello-servlet.xml將原來的載入方式,改為自動加入有hibernate和Spring提供的validate的預設類,配置如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:p="http://www.springframework.org/schema/p"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/beans    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/mvc    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd    http://www.springframework.org/schema/aop     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd    http://www.springframework.org/schema/context    http://www.springframework.org/schema/context/spring-context.xsd">    <context:component-scan     base-package="controller">    </context:component-scan>      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"    p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">        <!-- <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property> -->        </bean><!--基於validate的載入配置-->   <mvc:annotation-driven validator="validator"/> <!--    <bean id="accountValidator" class="dao.AccountValidator"></bean> -->    <bean id= "validator"    class= "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">            <property name= "providerClass"  value= "org.hibernate.validator.HibernateValidator"/>            <!-- 如果不加預設到 使用classpath下的 ValidationMessages.properties -->            <property name= "validationMessageSource" ref= "messageSource"/>    </bean>       <bean id= "messageSource"    class= "org.springframework.context.support.ReloadableResourceBundleMessageSource">            <property name= "basename" value= "classpath:message"/>            <property name= "fileEncodings" value= "utf-8"/>            <property name= "cacheSeconds" value= "120"/>    </bean></beans>
  • 在src目錄下面建立設定檔message.properties 給檔案的載入是由hello-servlet.xml中的欄位messageSource中的屬性basename的value值決定的。可以不用配置這個檔案,但是一般為了支援國際化,但是吧資訊寫到設定檔中的,如下:
account.username.size=\u7528\u6237\u540D\u7684\u957F\u5EA6\u57283-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4account.username.space=\u7528\u6237\u540D\u5B57\u7B26\u4E32\u4E4B\u95F4\u4E0D\u80FD\u6709\u7A7A\u683Caccount.password.size=\u5BC6\u7801\u7684\u957F\u5EA6\u8303\u56F4\u57286-20\u4E2A\u5B57\u7B26\u4E32\u4E4B\u95F4

後面的亂碼都是轉義後產生的,對應的中文如下:

3、原始碼
- 修改Account類,如下代碼:

package dao;import javax.validation.constraints.Pattern;import javax.validation.constraints.Size;public class Account {    @Size(min=3,max=20,message="{account.username.size}")    @Pattern(regexp="^[a-zA-Z0-9]+$",message="{account.username.space}")    private String username;    @Size(min=6,max=20,message="{account.password.size}")    private String password;    public Account() {        // TODO Auto-generated constructor stub    }    public Account(String username, String password) {        super();        this.username = username;        this.password = password;    }    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }}

其中的@Size @Pattern都是註解方式的資料驗證方式,後面的message資訊都是定義在message.properties中

  • 不需要AccountValidate類了
3、效果示範

啟動tomcat服務,在瀏覽器中輸入:http://localhost:8080/annotation_springmvc/register

我們是用一個大有空白字元串測試一下:

註冊效果:

4、代碼位置

如果有需要的,可以去這個位置下載下來看看:註解方式的資料驗證,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.