日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

springMVC学习(7)-springMVC校验

發布時間:2025/3/15 c/c++ 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springMVC学习(7)-springMVC校验 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、校驗理解:

對于安全要求較高點建議在服務端進行校驗。

控制層conroller:校驗頁面請求的參數的合法性。在服務端控制層conroller校驗,不區分客戶端類型(瀏覽器、手機客戶端、遠程調用)

業務層service(使用較多):主要校驗關鍵業務參數,僅限于service接口中使用的參數。

持久層dao:一般是不校驗

二、SpringMVC校驗需求:

springmvc使用hibernate的校驗框架validation(和hibernate沒有任何關系)。

思路:

頁面提交請求的參數,請求到controller方法中,使用validation進行校驗。如果校驗出錯,將錯誤信息展示到頁面。

具體需求:

商品修改,添加校驗(校驗商品名稱長度,生產日期的非空校驗)如果校驗出錯,在商品修改頁面顯示錯誤信息。

三、環境準備:

添加jar包:這里使用的是:

hibernate-validator-4.3.0-Final.jar;

jboss-logging-3.1.0.CR2.jar;

validation-api-1.0.0.GA.jar;

四、相關配置:

1)配置校驗器:

1 <mvc:annotation-driven conversion-service="conversionService" validator="validator"> 2 </mvc:annotation-driven> 3 <!-- 校驗器 --> 4 <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 5 <!-- hibernate校驗器--> 6 <property name="providerClass" value="org.hibernate.validator.HibernateValidator" /> 7 <!-- 指定校驗使用的資源文件,在文件中配置校驗錯誤信息,如果不指定則默認使用classpath下的ValidationMessages.properties --> 8 <property name="validationMessageSource" ref="messageSource" /> 9 </bean> 10 <!-- 校驗錯誤信息配置文件 --> 11 <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 12 <!-- 資源文件名--> 13 <property name="basenames"> 14 <list> 15 <value>classpath:CustomValidationMessages</value> 16 </list> 17 </property> 18 <!-- 資源文件編碼格式 --> 19 <property name="fileEncodings" value="utf-8" /> 20 <!-- 對資源文件內容緩存時間,單位秒 --> 21 <property name="cacheSeconds" value="120" /> 22 </bean>

2)由于這里是在Contoller中的形參(ItemsCustom)來接收參數,在pojo中添加校驗規則:

1 public class Items { 2 private Integer id; 3 4 //校驗名稱在1到30字符中間 5 //message是提示校驗出錯顯示的信息 6 @Size(min=1,max=30,message="{items.name.length.error}") 7 private String name; 8 private Float price; 9 private String pic; 10 11 //非空校驗 12 @NotNull(message="{items.createtime.isNUll}") 13 private Date createtime; 14 15 .... 16 } View Code

3)配置校驗錯誤提示信息文件 --?/springMVC/config/CustomValidationMessages.properties

#items modify error message items.name.length.error=the item name must be between 1-30 char items.createtime.isNUll=the createtime should be not null View Code

4)在controller中編寫校驗、捕獲校驗錯誤信息:

1 //商品信息修改提交 2 // 在需要校驗的pojo前邊添加@Validated,在需要校驗的pojo后邊添加BindingResult 3 // bindingResult接收校驗出錯信息 4 // 注意:@Validated和BindingResult bindingResult是配對出現,并且形參順序是固定的(一前一后)。 5 @RequestMapping("/editItemsSubmit") 6 public String editItemsSubmit(Model model, 7 HttpServletRequest request, 8 Integer id, 9 @Validated ItemsCustom itemsCustom,BindingResult bindingResult) 10 throws Exception { 11 12 if(bindingResult.hasErrors()){ 13 List<ObjectError> allErrors = bindingResult.getAllErrors(); 14 for(ObjectError objectError : allErrors){ 15 System.out.println(objectError.getDefaultMessage()); 16 } 17 18 // 將錯誤信息傳到頁面 19 model.addAttribute("allErrors", allErrors); 20 21 return "items/editItems"; 22 } 23 24 itemsService.updateItems(id, itemsCustom); 25 return "success"; 26 } View Code

5)jsp頁面展示錯誤信息:

1 <!-- 顯示錯誤信息 --> 2 <c:if test="${allErrors!=null}"> 3 錯誤信息:<br/> 4 <c:forEach items="${allErrors}" var="error"> 5 ${error.defaultMessage}<br/> 6 </c:forEach> 7 </c:if> View Code

效果:后臺打印:

前臺展示:

?

?

---------------------------------------------------------------------------------------------------------------------------------------

?分組校驗:

需求:

在pojo中定義校驗規則,而pojo是被多個 controller所共用,當不同的controller方法對同一個pojo進行校驗,但是每個controller方法需要不同的校驗。

解決辦法:

定義多個校驗分組(其實是一個java接口),分組中定義有哪些規則

每個controller方法使用不同的校驗分組

1)定義兩個校驗分組:

package com.cy.controller.validation;/*** 校驗分組* @author chengyu**/ public interface ValidGroup1 {//接口中不需要定義任何方法,僅是對不同的校驗規則進行分組//此分組只校驗商品名稱長度 }....public interface ValidGroup2 {} View Code

2)在校驗規則中添加分組,Items中:

1 public class Items { 2 private Integer id; 3 4 //校驗名稱在1到30字符中間 5 //message是提示校驗出錯顯示的信息 6 //groups:此校驗屬于哪個分組,groups可以定義多個分組 7 @Size(min=1,max=30,message="{items.name.length.error}",groups={ValidGroup1.class}) 8 private String name; 9 private Float price; 10 private String pic; 11 12 //非空校驗 13 @NotNull(message="{items.createtime.isNUll}") 14 private Date createtime; 15 16 ..... 17 18 } View Code

3)在Controller方法中指定分組的校驗:

1 //商品信息修改提交 2 // 在需要校驗的pojo前邊添加@Validated,在需要校驗的pojo后邊添加BindingResult 3 // bindingResult接收校驗出錯信息 4 // 注意:@Validated和BindingResult bindingResult是配對出現,并且形參順序是固定的(一前一后)。 5 // value={ValidGroup1.class}指定使用ValidGroup1分組的 校驗 6 @RequestMapping("/editItemsSubmit") 7 public String editItemsSubmit(Model model, 8 HttpServletRequest request, 9 Integer id, 10 @Validated(value={ValidGroup1.class}) ItemsCustom itemsCustom,BindingResult bindingResult) 11 throws Exception { 12 13 if(bindingResult.hasErrors()){ 14 List<ObjectError> allErrors = bindingResult.getAllErrors(); 15 for(ObjectError objectError : allErrors){ 16 System.out.println(objectError.getDefaultMessage()); 17 } 18 19 // 將錯誤信息傳到頁面 20 model.addAttribute("allErrors", allErrors); 21 22 return "items/editItems"; 23 } 24 25 itemsService.updateItems(id, itemsCustom); 26 return "success"; 27 } View Code

后臺打印:

前臺頁面:

轉載于:https://www.cnblogs.com/tenWood/p/6312535.html

總結

以上是生活随笔為你收集整理的springMVC学习(7)-springMVC校验的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。