dtr*_*unk 7 spring hibernate hibernate-validator spring-3
我尝试了这里的答案:Hibernate Validator,自定义ResourceBundleLocator和Spring
但仍然只是{location.title.notEmpty}作为输出而不是消息.
调度员servlet.xml中
<bean name="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="validationMessageSource">
<ref bean="resourceBundleLocator"/>
</property>
</bean>
<bean name="resourceBundleLocator" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>/WEB-INF/validationMessages</value>
</list>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
/WEB-INF/validationMessages.properties:
location.title.notEmpty=My custom message
Run Code Online (Sandbox Code Playgroud)
表格(课程地点)
@NotEmpty( message = "{location.title.notEmpty}" )
private String title;
Run Code Online (Sandbox Code Playgroud)
这里出了什么问题?
dtr*_*unk 13
得到它了!:-)
我添加了以下bean而不是上面提到的两个dispatcher-servlet.xml:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/validationMessages" />
</bean>
Run Code Online (Sandbox Code Playgroud)
然后,在我的validationMessages.properties中,我使用了以下语法:
NotEmpty.location.title=My custom Message
Run Code Online (Sandbox Code Playgroud)
我猜这就是原因:我使用了Hibernate验证注释(不是javax验证)
一般来说,消息文件的语法应如下所示
[ConstraintName].[ClassName].[FieldName]=[Message]
Run Code Online (Sandbox Code Playgroud)
希望这有助于其他人;-)
要从spring控制器访问消息,只需添加@Autowired private MessageSource messageSource;为类字段并使用messageSource.getMessage方法.因为我只使用我使用的一个语言环境messageSource.getMessage( "NotEmpty.location.title", null, null )
| 归档时间: |
|
| 查看次数: |
19100 次 |
| 最近记录: |