2014-03-14 62 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

感謝您的幫助。 –