beanvalidation默认参数的顺序?

我目前正在尝试使用beanvalidation提供自定义validation消息。

目前使用spring mvc 3.1.1 + apache beanvalidation。

在我的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.(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) 

这是唯一一个使用named参数的参数,它必须是ValidationMessages.properties,它必须是jsr 303实现中默认资源包中存在的密钥

  • ValidationMessages.properties:javax.validation.constraints.Size.message =你知道wad,大小必须介于{min}{max}之间

基本上,目前的结论是,默认情况下,我不能在我的特定消息上使用命名参数。 只有在我使用相同的默认jsr303 resourcebundle文件名(即ValidationMessages.properties)时,我才会 覆盖默认jsr303 resourcebundle &&的确切键时,named参数才有效

我现在更喜欢避免使用插值,因此关于如何找出{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}起作用,似乎是一个实现“function”) – 但最后,这只是默认消息插补器的行为,标准定义了如何替换的方式他们是你自己的。


更新

Hibernate Validation实现接口具有格式化值${validatedValue:}的附加function。 – 也许这可以帮助你解决java.lang.NumberFormatException

@参见Hibernate Validator Reference,第5.3章。 MessageInterpolator

更好用:

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

因为@Size允许像Whitespace characters一样的Whitespace characters

  • 对于Size.foo.bar使用名称进行插值不起作用
  • 但是对于size.foo.barbaz.foo.bar它(当没有使用validation注释名称时;检查区分大小写)

检查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; }