簡體   English   中英

bean驗證默認參數的命令?

[英]The orders of bean validation default parameters?

我目前正在嘗試使用 bean 驗證提供自定義驗證消息。

目前使用 spring mvc 3.1.1 + apache bean 驗證。

在我的 bean 中,我指定:

@Size(min=1, max=50)
private String title;

在我的 messages.properties 中:

Size.addForm.title=The title must not be empty and must not exceed {1} characters.

從實驗中,我發現:

  • {0} 指的是“標題”
  • {1} 表示最大值,即 50
  • {2} 指的是 min,即 1

並且它會顯示為The title must not be empty and must not exceed 50 characters. 哪個是正確的。

但所有這些都來自實驗。 我想知道是否有文檔說明默認約束的參數順序

我希望使用Size.addForm.title=The title must not be empty and must not exceed {max} characters. 基於默認的 ValidationMessages.properties 但以{max}上的 NumberFormatException 結束。 我認為這與插值有關?


更新

因此,這些中的每一個都因{max}上的 NumberFormatException 而獨立失敗:

  • messages.properties : Size.addForm.title=標題不得為空且不得超過{max} 個字符。
  • messages.properties : Size=標題不得為空且不得超過{max} 個字符。
  • messages.properties : javax.validation.constraints.Size.message=標題不得為空且不得超過{max} 個字符。
  • ValidationMessages.properties : Size.addForm.title=標題不得為空且不得超過{max} 個字符。
  • ValidationMessages.properties : Size=標題不得為空且不得超過{max} 個字符。

這是堆棧跟蹤:

java.lang.NumberFormatException: For input string: "max"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.text.MessageFormat.makeFormat(Unknown Source)
    at java.text.MessageFormat.applyPattern(Unknown Source)
    at java.text.MessageFormat.<init>(Unknown Source)
    at org.springframework.context.support.MessageSourceSupport.createMessageFormat(MessageSourceSupport.java:151)
    at org.springframework.context.support.ResourceBundleMessageSource.getMessageFormat(ResourceBundleMessageSource.java:281)
    at org.springframework.context.support.ResourceBundleMessageSource.resolveCode(ResourceBundleMessageSource.java:188)
    at org.springframework.context.support.AbstractMessageSource.getMessageInternal(AbstractMessageSource.java:205)
    at org.springframework.context.support.AbstractMessageSource.getMessage(AbstractMessageSource.java:146)
    at org.springframework.context.support.AbstractApplicationContext.getMessage(AbstractApplicationContext.java:1214)
    at org.springframework.web.servlet.support.RequestContext.getMessage(RequestContext.java:571)
    at org.springframework.web.servlet.support.BindStatus.initErrorMessages(BindStatus.java:177)
    at org.springframework.web.servlet.support.BindStatus.getErrorMessages(BindStatus.java:273)
    at org.springframework.web.servlet.tags.form.ErrorsTag.exposeAttributes(ErrorsTag.java:173)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementBodyTag.writeTagContent(AbstractHtmlElementBodyTag.java:48)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)

這是唯一一個與命名參數一起使用的參數,它必須是 ValidationMessages.properties,並且它必須是 jsr 303 實現的默認資源包中存在的鍵

  • ValidationMessages.properties : javax.validation.constraints.Size.message=你知道,大小必須在{min}{max} 之間

基本上,當前的結論是,默認情況下,我不能在我的特定消息中使用命名參數。 當我重寫的默認jsr303資源包&&當我使用相同的默認jsr303資源包文件名,這是ValidationMessages.properties的精確關鍵字命名的參數

我現在更喜歡避免使用插值,因此最初的問題是如何找出 {0} 或 {1} 或 {2} 指的是文檔中的內容。

JSR 303 規范,4.3.1.1。 “默認消息插值算法”

  • 4 - 從消息字符串中提取消息參數。 那些與約束屬性名稱匹配的內容將替換為約束聲明中該屬性的值。

我是這樣讀的:您應該為消息參數使用注釋屬性的名稱,而不是數字。

規范的附錄 B“標准 ResourceBundle 消息”顯示了一些示例:

javax.validation.constraints.Min.message=must be greater than or equal to {value}
javax.validation.constraints.Max.message=must be less than or equal to {value}
javax.validation.constraints.Size.message=size must be between {min} and {max}
javax.validation.constraints.Digits.message=numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected)

因此,命名參數似乎是您應該使用的方式。 (那個{0}{1}{2}也可以工作,似乎是一個實現“功能”) -但最終,這只是默認消息插值器的行為,標准定義了一種如何替換的方法他們由你自己。


更新

Hibernate Validation 實現接縫具有附加功能來格式化值${validatedValue:<format>} -- 也許這對你的java.lang.NumberFormatException有幫助

@參見Hibernate Validator Reference,第 5.3 章。 消息插值器

更好的使用:

    @Size(max=99)
    @NotBlank
    private String title;

因為@Size允許空白字符,比如 space( )。

  • 對於帶有名稱的Size.foo.bar插值不起作用
  • 但是對於size.foo.barbaz.foo.bar確實如此(當未使用驗證注釋名稱時;檢查區分大小寫)

檢查message.properties文件,該文件添加在WebAppConfig擴展WebMvcConfigurerAdapter例如:

  @Bean
  public MessageSource messageSource() {
    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    return messageSource;
  }

  @Override
  public Validator getValidator() {
    LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
    bean.setValidationMessageSource(messageSource());
    return bean;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM