Spring MVC无法读取messages.properties文件 [英] Spring mvc not able to read messages.properties file
本文介绍了Spring MVC无法读取messages.properties文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过使用属性文件来使用自定义验证错误消息.我已将messages.properties文件放置在Web内容/web-inf/文件夹中.
I am trying to use custom validation error messages by using properties file. I have placed messages.properties file in web content/web-inf/ folder.
NonEmpty.batchDomain.batchName=Invalid message 2.
我的applicationContext文件是:
My applicationContext file is :
<context:component-scan base-package="com.coaching.controller" />
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />
<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views
directory -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:default-servlet-handler />
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages" />
</bean>
我的控制器是:
@RequestMapping(method = RequestMethod.POST)
public ModelAndView addBatch(
@Valid @ModelAttribute("batchDomain") BatchDomain batchDomain,
BindingResult result, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
if (result.hasErrors()) {
ModelAndView modelAndView = new ModelAndView("newBatch");
return modelAndView;
}
}
BatchDomain是:
BatchDomain is :
public class BatchDomain {
@NotNull
@NotEmpty
@Size(min = 1, max = 100)
private String batchName;
@Min(1)
@Max(1000)
private int strength;
}
据我在google上看到的,我正在遵循正确的方法.那么,这个问题背后的原因是什么?
As far as I have seen in google, I am following the correct approach. So, what can be the reason behind this issue?
推荐答案
您可以尝试将文件"messages.properties"放在/src/main/resource目录中.
You may try to put file "messages.properties" in /src/main/resource directory.
这篇关于Spring MVC无法读取messages.properties文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文