2011-06-09 19 views
1

我正在嘗試設置ReloadableResourceBundleMessageSource以在我的JSP和Controller中使用。這是我的作品。消息鍵正在打印到我的jsp中的屏幕上。我錯過了什麼?我的Spring MessageSource安裝有什麼問題?

爲spring-servlet.xml

<bean id="messages" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basenames"> 
     <value>/WEB-INF/messages/login</value> 
    </property> 
</bean> 

login_en.properties

login.title=Welcome to the login page. 

回到Home.jsp

<body> 
    <spring:message code="login.title" text="login.title"></spring:message> 
</body> 

回答

4

呀,這我絆倒了很長一段時間了。 bean id必須被稱爲messageSource。

+0

這就是它......謝謝! – coder 2011-06-09 03:25:10

0

也不要忘記聲明messageSource應用程序範圍廣泛,而不是在servlet spring bean中。

1

我張貼我的回答基於java的配置,因爲我遇到了同樣的問題,當我從xml切換到基於java的配置應用程序,這是一個痛苦的a **找出原因它沒有工作。 (對我來說,@ jiggy指出的是同樣的問題......你不知道...)

@Bean(name = "validator") 
    public LocalValidatorFactoryBean getLocalValidatorFactoryBean() { 
     final LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); 
     validator.setValidationMessageSource(this.getMessageSource()); 
     validator.afterPropertiesSet(); 
     return validator; 
    } 

    @Bean(name = "messageSource") // --> !!! This is what is so important!!! 
    public MessageSource getMessageSource() { 
     final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource(); // easily swapped out with "ReloadableResourceBundleMessageSource", my app just doesn't have any necessary reloadable message requirements. 
     messageSource.setBasenames(ERROR_MESSAGE_DIRECTORY); // static String for a direct path to your "ValidationMessages.properties" file or whatever name you've given it. 
     return messageSource; 
    } 
相關問題