【问题标题】:Spring framework: No message found under code for localeSpring框架:在语言环境的代码下找不到消息
【发布时间】:2013-02-10 12:34:11
【问题描述】:

这是我的 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

我在这里错过了什么?

【问题讨论】:

  • 首先您的所有信息都在这里? fil_PH 可能是您的邮件文件夹的另一个属性文件。

标签: spring spring-mvc message


【解决方案1】:

也许它会对某人有所帮助。

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

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

例子:

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

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

【讨论】:

    【解决方案2】:

    默认情况下,maven 假设在

    下找到这样的资源包消息源

    src/main/资源

    。通过移动该位置下的所有必要文件夹并确保我们在上下文中有以下代码

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

    【讨论】:

      【解决方案3】:

      你可以在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
      

      【讨论】:

      • 为什么 spring.messages.always-use-message-format=false
      【解决方案4】:

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

      @Bean(name="messageSource")
      public ResourceBundleMessageSource bundleMessageSource() {
      ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
      messageSource.setBasename("messages");
      return messageSource;
      }
      

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          对于 spring boot,你需要这样的东西:

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

          【讨论】:

            【解决方案7】:

            一般来说,出现此类问题不是因为不存在语言环境,而是因为 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。如果您在/WEB-INF/messages 文件夹中有messages.properties,则对应的基本名称为/WEB-INF/messages/messages

            【讨论】:

            • 为什么我需要删除'/'?
            • 您需要删除“/”,因为当您指定基本名称时,您应该使用这样的结构:/path/to/bundle/bundlename。你能告诉我它是否有帮助吗?
            • 我还以为是我指的文件夹或者包含消息资源的文件夹,我错了吗?
            • 让我们看看:如果你有messages.propertiesmessages_en.properties 捆绑包,那么捆绑包名称是messages。如果您将它们放在WEB-INF 文件夹中,则基本名称为/WEB-INF/messages,即根据/path/to/bundle/bundlename。如果您在/WEB-INF/messages 文件夹中有messages.properties,则对应的基本名称为/WEB-INF/messages/messages
            • 什么是 MessageBundle,我在哪里配置它?
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-05-16
            • 1970-01-01
            • 1970-01-01
            • 2021-01-19
            • 1970-01-01
            相关资源
            最近更新 更多