2013-08-30 33 views
0

我试图通过使用属性文件来使用自定义验证错误消息。我已经将messages.properties文件放在web内容/ web-inf /文件夹中。Spring mvc无法读取messages.properties文件

NonEmpty.batchDomain.batchName=Invalid message 2. 

我的applicationContext文件是:

 <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是:

 public class BatchDomain { 

@NotNull 
@NotEmpty 
@Size(min = 1, max = 100) 
private String batchName; 

@Min(1) 
@Max(1000) 
private int strength; 

} 

至于我在谷歌所看到的,我遵循正确的认识ACH。那么,这个问题背后的原因是什么?

+0

您可以尝试将属性文件放在类路径的根目录下,而不是在“WEB-INF”内部?在spring上下文中更改声明。 –

+0

我已经把文件放在src文件夹中,按照http://stackoverflow.com/questions/11302531/how-to-place-a-file-on-classpath-in-eclipse和我有编辑上下文文件为<属性名称=“basename”value =“messages”/>但没有任何事情发生。 –

+0

您的messages.properties文件是否可以在其他情况下使用,但在验证消息中会被忽略,或者在任何情况下都无法使用?可能你使用的是hibernate validator,它会查找ValidationMessages.properties(除非另有配置)。 –

回答

0

您可能会尝试将文件“messages.properties”放在/ src/main/resource目录中。