在项目中,我使用SpringMVC和Ajax。
因此,为了验证来自提交表单的输入,我不使用SpringMVC的错误标记来显示错误消息。相反,我将所有错误扭曲到一个对象中,然后返回到JSP,以便使用jQuery显示它们。
如我所知,如果使用SpringMVC的错误标记,Spring将根据您在bean上设置的键从messages.properties(e.g)获取错误消息。
问题
我想在不使用messages.properites(e.g)的错误标记的情况下从它中准确地得到错误消息。
现在,我在实现Validator接口的项目中的验证器中硬编码默认错误消息,但这不是我想要的。
我想根据我设置的错误从xxx.properties中得到消息s。
我能做什么?
发布于 2014-03-14 05:28:36
你需要ResourceBundleMessageSource豆。
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
<property name="defaultEncoding" value="UTF-8" />
</bean>在你的控制器里
@Autowired
ResourceBundleMessageSource messageSource;
@RequestMapping( "/your/url" )
public @ResponseBody String doSomething( @RequestParam Long myVal ) {
String errorMessage = messageSource.getMessage("error.code", Locale.getDefault());
...
return ...;
}https://stackoverflow.com/questions/22395605
复制相似问题