JSR303校驗
- 導入
依賴Hibernate-Validator
- 注意 :
及其以上的伺服器中,有最新的Tomcat7
。對于低版本中,需要替換el表達式
中的标準lib
最好使用較高版本el
Tomcat
- 加入依賴
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.4.1.Final</version> </dependency>
- 注意 :
- 使用
- 标明要檢驗的字段 - 注解
- 常用校驗規則。
限制長度 @Length、 自定義正規表達式 @Pattern(regexp="", message="")、 郵箱校驗@Email
- 注解标注在實體
Bean
- 注意 : 對于正則式表達式中的
使用\
轉義\\
- 常用校驗規則。
- 編寫處理校驗邏輯 - 注入
- 在
中編寫邏輯。Controller
- 接收的參數(一般是實體Bean),配置注解
@Valid
- 同時,傳入
用于擷取校驗資訊BindingResult bindResult
- 常用于編寫錯誤處理邏輯 :
bindResult.hasError、bindResult.getFieldErrors、bindResult.getField、bindResult.getDefaultMessage
- 常用于編寫錯誤處理邏輯 :
- 在
- 此外,可以完善對應的 資料庫中
關鍵字限制 unique
- 前台js校驗
- 背景校驗
- 資料庫校驗
- end
- 标明要檢驗的字段 - 注解
- end