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

    歡迎訪問 生活随笔!

    生活随笔

    當前位置: 首頁 > 前端技术 > javascript >内容正文

    javascript

    Spring Validation(使用Hibernate Validator)

    發布時間:2025/3/21 javascript 41 豆豆
    生活随笔 收集整理的這篇文章主要介紹了 Spring Validation(使用Hibernate Validator) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

    1、需要的jar包

    hibernate-validator.5.1.3.Final.jar

    validation-api.1.1.0.Final.jar

    2、springsevlet-config.xml配置

    在spring3之后,任何支持JSR303的validator(如Hibernate Validator)都可以通過簡單配置引入,只需要在配置xml中加入,這時validatemessage的屬性文件默認為classpath下的ValidationMessages.properties

    <!-- support JSR303 annotation if JSR 303 validation present on classpath --> <mvc:annotation-driven />

    如果不使用默認,可以使用下面配置:

    <mvc:annotation-driven validator="validator" /><bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"><property name="providerClass" value="org.hibernate.validator.HibernateValidator"/><!--不設置則默認為classpath下的ValidationMessages.properties --><property name="validationMessageSource" ref="validatemessageSource"/> </bean>
    <bean id="validatemessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basename" value="classpath:validatemessages"/> <property name="fileEncodings" value="utf-8"/> <property name="cacheSeconds" value="120"/> </bean>

    ?3、hibernate validator constraint 注解

    Bean Validation 中內置的 constraint @Null 被注釋的元素必須為 null @NotNull 被注釋的元素必須不為 null @AssertTrue 被注釋的元素必須為 true @AssertFalse 被注釋的元素必須為 false @Min(value) 被注釋的元素必須是一個數字,其值必須大于等于指定的最小值 @Max(value) 被注釋的元素必須是一個數字,其值必須小于等于指定的最大值 @DecimalMin(value) 被注釋的元素必須是一個數字,其值必須大于等于指定的最小值 @DecimalMax(value) 被注釋的元素必須是一個數字,其值必須小于等于指定的最大值 @Size(max=, min=) 被注釋的元素的大小必須在指定的范圍內 @Digits (integer, fraction) 被注釋的元素必須是一個數字,其值必須在可接受的范圍內 @Past 被注釋的元素必須是一個過去的日期 @Future 被注釋的元素必須是一個將來的日期 @Pattern(regex=,flag=) 被注釋的元素必須符合指定的正則表達式 Hibernate Validator 附加的 constraint @NotBlank(message =) 驗證字符串非null,且長度必須大于0 @Email 被注釋的元素必須是電子郵箱地址 @Length(min=,max=) 被注釋的字符串的大小必須在指定的范圍內 @NotEmpty 被注釋的字符串的必須非空 @Range(min=,max=,message=) 被注釋的元素必須在合適的范圍內

    ?4、使用validator

    在需要校驗的對象前增加@Valid 注解(該注解位于javax.validation包中)來觸發校驗。這樣就可以完成針對輸入數據User對象的校驗了,校驗結果任然保存在BindingResult對象中。

    1 package com.mkyong.common.model; 2 3 import org.hibernate.validator.constraints.NotEmpty; 4 import org.hibernate.validator.constraints.Range; 5 6 public class Customer { 7 8 @NotEmpty //make sure name is not empty 9 String name; 10 11 @Range(min = 1, max = 150) //age need between 1 and 150 12 int age; 13 14 //getter and setter methods 15 16 } 1 package com.mkyong.common.controller; 2 3 import javax.validation.Valid; 4 import org.springframework.stereotype.Controller; 5 import org.springframework.ui.ModelMap; 6 import org.springframework.validation.BindingResult; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import com.mkyong.common.model.Customer; 10 11 @Controller 12 @RequestMapping("/customer") 13 public class SignUpController { 14 15 @RequestMapping(value = "/signup", method = RequestMethod.POST) 16 public String addCustomer(@Valid Customer customer, BindingResult result) { 17 18 if (result.hasErrors()) { 19 return "SignUpForm"; 20 } else { 21 return "Done"; 22 } 23 24 } 25 26 @RequestMapping(method = RequestMethod.GET) 27 public String displayCustomerForm(ModelMap model) { 28 29 model.addAttribute("customer", new Customer()); 30 return "SignUpForm"; 31 32 } 33 34 } 1 <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 2 <html> 3 <head> 4 <style> 5 .error { 6 color: #ff0000; 7 } 8 9 .errorblock { 10 color: #000; 11 background-color: #ffEEEE; 12 border: 3px solid #ff0000; 13 padding: 8px; 14 margin: 16px; 15 } 16 </style> 17 </head> 18 19 <body> 20 <h2>Customer SignUp Form - JSR303 @Valid example</h2> 21 22 <form:form method="POST" commandName="customer" action="customer/signup"> 23 <form:errors path="*" cssClass="errorblock" element="div" /> 24 <table> 25 <tr> 26 <td>Customer Name :</td> 27 <td><form:input path="name" /></td> 28 <td><form:errors path="name" cssClass="error" /></td> 29 </tr> 30 <tr> 31 <td>Customer Age :</td> 32 <td><form:input path="age" /></td> 33 <td><form:errors path="age" cssClass="error" /></td> 34 </tr> 35 <tr> 36 <td colspan="3"><input type="submit" /></td> 37 </tr> 38 </table> 39 </form:form> 40 41 </body> 42 </html>

    可以通過定義“validatemessage.properties”文件,覆蓋定義在持久化對象上的錯誤提示,通常屬性文件中屬性key為“@Annotation Name.object.fieldname“的形式:

    NotEmpty.customer.name = Name is required! Range.customer.age = Age value must be between 1 and 150

    代碼運行Demo:

    SpringMVC-Bean-Validation-JSR303-Example-2.zip

    5、參考

    http://www.mkyong.com/spring-mvc/spring-3-mvc-and-jsr303-valid-example/

    http://www.cnblogs.com/myitmylife/p/3617084.html

    http://www.cnblogs.com/liukemng/p/3738055.html


    from:?http://www.cnblogs.com/afeng7882999/p/4300032.html

    總結

    以上是生活随笔為你收集整理的Spring Validation(使用Hibernate Validator)的全部內容,希望文章能夠幫你解決所遇到的問題。

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