Spring框架:在locale代码下找不到任何消息

时间:2013-02-25 11:17:26

标签: spring spring-mvc message

这是我的messageResource声明

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <!-- Auto-detect controllers in this package -->
    <context:component-scan base-package="levelup.world.web" />

    <!-- Prepend /WEB-INF/jsp/ and append .jsp to the view name -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <!-- Access resource bundles with the specified basename -->
    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
        p:basename="/WEB-INF/messages/" />

</beans>

当我运行我的应用程序时,会出现此错误

No message found under code 'country.plural' for locale 'fil_PH'

现在在web-inf内的我的消息文件夹中,我有以下消息属性

messages_en.properties
messages_fr.properties
messages.properties

我在这里缺少什么?

7 个答案:

答案 0 :(得分:23)

通常,此类问题不是因为不存在的区域设置,而是因为MessageBundle配置不正确。在您的情况下,您似乎需要删除基名中的“/”。

<bean id="messageSource"
     class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
     p:basename="/WEB-INF/messages" />

为什么会如此:

如果您有messages.propertiesmessages_en.properties捆绑包,则捆绑包名称为messages。如果您将它们放在WEB-INF文件夹中,则基本名称为/WEB-INF/messages,即根据/ path/to/bundle/bundlename。如果messages.properties文件夹中有/WEB-INF/messages,则相应的基本名称为/WEB-INF/messages/messages

答案 1 :(得分:10)

对于春季靴子你需要这样的东西:

@Bean
public MessageSource messageSource() {
     ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
     messageSource.setBasename("/WEB-INF/classes/messages");
     return messageSource;
}

答案 2 :(得分:2)

您可以在application.properties中添加一些代码:

spring.messages.always-use-message-format=false
spring.messages.basename=messages
spring.messages.cache-seconds=-1
spring.messages.encoding=UTF-8
spring.messages.fallback-to-system-locale=true

答案 3 :(得分:1)

您也可以在Spring boot application.properties中指定

# INTERNATIONALIZATION 
spring.messages.basename=i18n/messages
spring.messages.encoding=UTF-8

答案 4 :(得分:1)

对于spring boot文件夹资源,您需要添加Bean名称:

Class SecondClass: UIViewController {

      let myfirstClass = FirstClass()
      myFirstClass.abc()     // changes the tag in your instance of FirstClass

      var a = myfirstClass.getTag()

      // will print 5 because now you ask the instance, 
      // where the abc-function was called       
}

答案 5 :(得分:0)

也许它会帮助某人。

errors.rejectValue("fieldName", "errorCode", "textException");

如果你不像我那样使用 messageResource,那么你应该写 "" 而不是 "errorCode"。

示例:

errors.rejectValue("name", "", "This field should not be empty");

这种方式帮助我避免了异常。

答案 6 :(得分:-1)

默认情况下,maven假定在

下找到此类资源包消息源。

src / main / resources

。通过将所有必要的文件夹移到该位置下,并确保上下文中包含以下代码

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename"><value>messages</value></property>
</bean>