放置在文件夹中时找不到MessageSource的ResourceBundle [英] ResourceBundle not found for MessageSource when placed inside a folder

查看:1894
本文介绍了放置在文件夹中时找不到MessageSource的ResourceBundle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将资源束与Spring的消息源一起使用.这是我的操作方式:

I am trying to use resource bundles with Spring's Message Source. Here is the way I am doing it:

@Component
public class MessageResolver implements MessageSourceAware {

    @Autowired
    private MessageSource messageSource;

    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    public String getMessage(){
        return messageSource.getMessage("user.welcome", new Object[]{"Rama"} , Locale.US);
    }

}

这是我的文件夹结构:

messages_zh_CN.properties仅包含一行:

messages_en_US.properties contains just one line:

user.welcome=Welcome {0}

这是使用的xml配置:

Here is the xml configuration used:

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

这是我遇到的错误:

WARNING: ResourceBundle [resourcebundles/messages] not found for MessageSource: Can't find bundle for base name resourcebundles/messages, locale en_US
Exception in thread "main" org.springframework.context.NoSuchMessageException: No message found under code 'user.welcome' for locale 'en_US'.

但是,如果我将资源束直接移动到resources文件夹下,则可以正常工作.在这种情况下,这是我正在使用的xml配置:

But if I move my resource bundle to directly under the resources folder, it is working fine. In this case, here is the xml configuration I am using:

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

是是否必须使用ResourceBundleMessageSource,我应该将资源束直接放在资源下方?如果我只需要将其保留在指定的文件夹中,还有其他方法可以使这一工作吗?

Is is that if I have to use ResourceBundleMessageSource, I should put my resource bundles directly under the resources? If i have to keep it in specified folder only, is there any other way to get this one work?

谢谢!

推荐答案

将xml文件中messageSource bean的配置更改为以下内容.

Change your configuration to the following for messageSource bean in your xml file.

<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename"> 
        <value>classpath*:resourcebundles/messages</value> 
    </property> 
</bean>

由于所有属性文件都位于java的classpath中,因此需要使用前缀classpath*:定义路径,否则它将进入您应用程序的Web目录.

Since all your properties files are in classpath of java you need to define the path with prefix classpath*: otherwise it will look into the web directory of your application.

希望这对您有所帮助.干杯.

Hope this helps you. Cheers.

这篇关于放置在文件夹中时找不到MessageSource的ResourceBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆