2014-03-14 66 views
0

在项目中,我使用SpringMVC和Ajax。ajax SpringMVC验证

因此,为验证提交表单的输入,我不使用SpringMVC的错误标签来显示错误消息。相反,我将所有错误都转换为对象并返回到JSP以使用jQuery显示它们。

据我所知,如果使用SpringMVC的错误标记,Spring将根据您在bean上设置的键从messages.properties(e.g)获取错误消息。

问题

我想确切来自messages.properites(e.g)错误信息,而不使用其错误标签。

现在,我在实现Validator接口的项目中的验证器中硬编码了默认错误消息,但那不是我想要的。

我想根据我设置的错误从xxx.properties确切消息s。

我该怎么办?

+1

你的问题太难以理解,你需要向我们提供的代码示例,预期输入/输出等 – gerrytan

回答

0

您需要ResourceBundleMessageSource bean。

<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 ...; 
} 
+0

感谢您的帮助。 –