成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

SpringMVC中怎么實現(xiàn)Validation校驗

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)SpringMVC中怎么實現(xiàn)Validation校驗,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)公司專注于江門企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。江門網(wǎng)站建設(shè)公司,為江門等地區(qū)提供建站服務(wù)。全流程按需求定制網(wǎng)站,專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)

一、概述

對前端的校驗大多數(shù)通過js在頁面校驗,這種方法比較簡單,如果對安全性考慮,還要在后臺校驗。

springmvc使用JSR-303(javaEE6規(guī)范的一部分)校驗規(guī)范,springmvc使用的是Hibernate Validator(和Hibernate的ORM)

二、步驟

2.1 引入 Hibernate Validator

<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-validator --><dependency>  <groupId>org.hibernate</groupId>  <artifactId>hibernate-validator</artifactId>  <version>5.4.1.Final</version></dependency>

2.2 配置

<!-- 校驗器 -->  <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="basenames">      <list>        <value>classpath:CustomValidationMessages</value>      </list>    </property>    <!-- 資源文件編碼格式 -->    <property name="fileEncodings" value="utf-8" />    <!-- 對資源文件內(nèi)容緩存時間,單位秒 -->    <property name="cacheSeconds" value="120" />  </bean><!-- 自定義webBinder -->  <bean id="customBinder"    class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">    <!-- 使用converter進行參數(shù)轉(zhuǎn) -->    <property name="conversionService" ref="conversionService" />    <!-- 配置validator -->    <property name="validator" ref="validator" />    <!-- propertyEditorRegistrars用于屬性編輯器 -->    <!-- <property name="propertyEditorRegistrars"> <list> <ref bean="customPropertyEditor"       /> </list> </property> -->  </bean><!-- 注解適配器 -->  <bean    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">    <!-- 在webBindingInitializer中注入自定義屬性編輯器、自定義轉(zhuǎn)換器 -->    <property name="webBindingInitializer" ref="customBinder"></property>    <!-- 加入 json數(shù)據(jù)的消息轉(zhuǎn)換器 MappingJacksonHttpMessageConverter依賴Jackson的包 -->    <property name="messageConverters">      <list>        <bean          class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>      </list>    </property>  </bean>

2.3 創(chuàng)建CustomValidationMessages

在classpath下創(chuàng)建CustomValidationMessages.properties

# 校驗提示信息:還需要在java中配置items.name.length.error=商品長度請限制在1-30之間items.createtime.is.notnull=請輸入商品生產(chǎn)日期

2.4 校驗規(guī)則

商品信息提交時校驗 ,商品生產(chǎn)日期不能為空,商品名稱長度在1到30字符之間

public class Items {  private Integer id;    //商品名稱的長度請限制在1到30個字符  @Size(min=1,max=30,message="{items.name.length.error}")  private String name;  private Float price;  private String pic;    //請輸入商品生產(chǎn)日期  @NotNull(message="{items.createtime.is.notnull}")  private Date createtime;  private String detail;}

2.5 捕獲錯誤

需要修改controller方法,在要校驗的pojo前邊加上@Validated,

public String editItemSubmit(Model model,Integer id,        @Validated @ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,        BindingResult bindingResult,      //上傳圖片      MultipartFile pictureFile      )throws Exception{        //輸出校驗錯誤信息    //如果參數(shù)綁定時有錯    //輸出校驗錯誤信息    //如果參數(shù)綁定時有錯    if(bindingResult.hasErrors()){            //獲取錯誤       List<ObjectError> errors = bindingResult.getAllErrors();      //準(zhǔn)備在頁面輸出errors,頁面使用jstl遍歷      model.addAttribute("errors", errors);      for(ObjectError error:errors){        //輸出錯誤信息        System.out.println(error.getDefaultMessage());      }      //如果校驗錯誤,回到商品修改頁面      return "editItem";    }}

2.6 在頁面上展示錯誤

<!-- 錯誤信息 --><c:forEach items="${errors }" var="error"> ${error.defaultMessage }<br/></c:forEach>

2.7 分組校驗

需求

針對不同的controller方法通過分組校驗達到個性化校驗的目的,修改商品修改功能,只校驗生產(chǎn)日期不能為空。

第一步:創(chuàng)建分組接口

public interface ValidGroup1 {  //接口不定義方法,就是只標(biāo)識 哪些校驗 規(guī)則屬于該 ValidGroup1分組}

第二步:定義校驗規(guī)則屬于哪個分組

//請輸入商品生產(chǎn)日期//通過groups指定此校驗屬于哪個分組,可以指定多個分組 @NotNull(message="{items.createtime.is.notnull}",groups={ValidGroup1.class})  private Date createtime;

第三步:在controller方法定義使用校驗的分組

public String editItemSubmit(Model model,Integer id,        @Validated(value={ValidGroup1.class}) @ModelAttribute(value="itemsCustom") ItemsCustom itemsCustom,        BindingResult bindingResult,      //上傳圖片      MultipartFile pictureFile      )throws Exception{        //...其他代碼省略...      }

上述就是小編為大家分享的SpringMVC中怎么實現(xiàn)Validation校驗了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文標(biāo)題:SpringMVC中怎么實現(xiàn)Validation校驗
路徑分享:http://jinyejixie.com/article34/gpesse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、Google軟件開發(fā)、App開發(fā)、企業(yè)建站微信公眾號

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
平阳县| 临沭县| 盖州市| 内江市| 应用必备| 武穴市| 丰原市| 临城县| 五峰| 遵义市| 新宁县| 丹东市| 余庆县| 盖州市| 蒲江县| 柳林县| 开江县| 宜兴市| 金堂县| 临夏市| 怀来县| 开封县| 邵阳县| 文山县| 永昌县| 军事| 翼城县| 漳平市| 云霄县| 田东县| 洪江市| 湄潭县| 上杭县| 永吉县| 台中市| 天津市| 宁陕县| 广丰县| 鄂伦春自治旗| 蒙山县| 怀宁县|