ber*_*tie 8 java spring spring-mvc internationalization bean-validation
我目前正在尝试使用bean验证提供自定义验证消息.
目前使用spring mvc 3.1.1 + apache bean验证.
在我的bean中,我指定:
@Size(min=1, max=50)
private String title;
Run Code Online (Sandbox Code Playgroud)
在我的messages.properties中:
Size.addForm.title=The title must not be empty and must not exceed {1} characters.
Run Code Online (Sandbox Code Playgroud)
从实验中我发现:
并且它将显示为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的方法,但最终会使用NumberFormatException {max}.我认为它与插值有关?
因此,每个都在NumberFormatException上独立失败{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)
Run Code Online (Sandbox Code Playgroud)
这是唯一一个使用named参数的参数,它必须是ValidationMessages.properties,它必须是jsr 303实现中默认资源包中存在的密钥:
基本上,目前的结论是,默认情况下,我不能在我的特定消息上使用命名参数.只有在我使用相同的默认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)
Run Code Online (Sandbox Code Playgroud)
所以看来命名参数是你应该使用的方式。(这{0}似乎是一个实现“功能”) -但最终,这只是默认消息插值器的行为,标准定义了一种如何用您自己的消息插值器替换它们的方法{1}。{2}
更新
Hibernate Validation 实现似乎有一个附加功能来格式化值${validatedValue:<format>}。——也许这对你有帮助java.lang.NumberFormatException
@参见Hibernate 验证器参考,第 5.3 章。消息插值器
| 归档时间: |
|
| 查看次数: |
4301 次 |
| 最近记录: |