溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

詳解SpringMVC驗證框架Validation特殊用法

發(fā)布時間:2020-09-25 20:40:40 來源:腳本之家 閱讀:101 作者:wangpeng047 欄目:編程語言

基本用法不說了,網(wǎng)上例子很多,這里主要介紹下比較特殊情況下使用的方法。

1. 分組

有的時候,我們對一個實體類需要有多中驗證方式,在不同的情況下使用不同驗證方式,比如說對于一個實體類來的id來說,保存的時候是不需要的,對于更新時是必須的,可以如下配置:

public class UserModel { 
 
  @NotNull(message = "{id.empty}", groups = { First.class }) 
  private int id; 
 
  @NotNull(message = "{username.empty}", groups = { First.class, Second.class }) 
  private String username; 
 
  @NotNull(message = "{content.empty}", groups = { First.class, Second.class }) 
  private String content; 
 
  public int getId() { 
    return id; 
  } 
 
  public void setId(int id) { 
    this.id = id; 
  } 
 
  public String getUsername() { 
    return username; 
  } 
 
  public void setUsername(String username) { 
    this.username = username; 
  } 
 
  public String getContent() { 
    return content; 
  } 
 
  public void setContent(String content) { 
    this.content = content; 
  } 
} 
public interface First { 
} 
 
public interface Second { 
} 

通過 groups 對驗證進行分組

在controler中的代碼如下:

@RequestMapping(value = "/save.action", method = RequestMethod.POST) 
public String save(@Validated( { Second.class }) UserModel userModel, BindingResult result) { 
  if (result.hasErrors()) { 
    return "validate/error"; 
  } 
  return "redirect:/success"; 
} 
 
@RequestMapping(value = "/update.action", method = RequestMethod.POST) 
public String update(@Validated( { First.class, Second.class }) UserModel user, BindingResult result) { 
  if (result.hasErrors()) { 
    return "validate/error"; 
  } 
  return "redirect:/success"; 
} 

2. 組序列

默認情況下,不同組別的約束驗證是無序的,然而在某些情況下,約束驗證的順序卻很重要,如下面兩個例子:(1)第二個組中的約束驗證依賴于一個穩(wěn)定狀態(tài)來運行,而這個穩(wěn)定狀態(tài)是由第一個組來進行驗證的。(2)某個組的驗證比較耗時,CPU 和內(nèi)存的使用率相對比較大,最優(yōu)的選擇是將其放在最后進行驗證。因此,在進行組驗證的時候尚需提供一種有序的驗證方式,這就提出了組序列的概念。

一個組可以定義為其他組的序列,使用它進行驗證的時候必須符合該序列規(guī)定的順序。在使用組序列驗證的時候,如果序列前邊的組驗證失敗,則后面的組將不再給予驗證。

下例中聲明了組 GroupA.class,GroupB.class 和 Group.class,其中 default,GroupA,GroupB 均為 Group 的序列。

public interface GroupA { 
} 
 
public interface GroupB { 
} 
 
@GroupSequence( { Default.class, GroupA.class, GroupB.class }) 
public interface Group { 
} 
 
public class User { 
  @NotEmpty(message = "firstname may be empty") 
  private String firstname; 
 
  @NotEmpty(message = "middlename may be empty", groups = Default.class) 
  private String middlename; 
 
  @NotEmpty(message = "lastname may be empty", groups = GroupA.class) 
  private String lastname; 
 
  @NotEmpty(message = "country may be empty", groups = GroupB.class) 
  private String country; 
} 
[java] view plain copy 在CODE上查看代碼片派生到我的代碼片
@RequestMapping(value = "/update.action", method = RequestMethod.POST) 
public String register(@Validated(Group.class) User user, BindingResult result) { 
  if (result.hasErrors()) { 
    return "validate/error"; 
  } 
  return "redirect:/success"; 
} 

3. 驗證多個對象

當我們在一個功能處理方法上需要驗證多個模型對象時,需要通過如下形式來獲取驗證結(jié)果:

@RequestMapping("/validate/multi") 
public String multi(@Valid @ModelAttribute("a") A a, BindingResult aErrors, @Valid @ModelAttribute("b") B b, BindingResult bErrors) { 
 
  if (aErrors.hasErrors()) { //如果a模型對象驗證失敗 
    return "validate/error"; 
  } 
  if (bErrors.hasErrors()) { //如果a模型對象驗證失敗 
    return "validate/error"; 
  } 
  return "redirect:/success"; 
} 

每一個模型對象后邊都需要跟一個Errors或BindingResult對象來保存驗證結(jié)果,其方法體內(nèi)部可以使用這兩個驗證結(jié)果對象來選擇出錯時跳轉(zhuǎn)的頁面或處理的邏輯。

4. Junit測試

當自定義拓展Validation時,可以使用如下方法進行測試:

@Test 
public void testValidate() { 
  AnnotationDescriptor<EqualsAny> descriptor = new AnnotationDescriptor<EqualsAny>(EqualsAny.class); 
  EqualsAny equalsAny = AnnotationFactory.create(descriptor); 
  EqualsAnyValidator equalsAnyValidator = new EqualsAnyValidator(); 
  equalsAnyValidator.initialize(equalsAny); 
  Assert.assertTrue(equalsAnyValidator.isValid("123", null)); 
} 

另外再講一點spring對自定義JSR-303限制類型支持的新特性,那就是Spring支持往ConstraintValidator里面注入bean對象。例如在EqualsAnyValidator中利用@Resource注解注入其他Bean對象。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI