放置在文件夹中时找不到MessageSource的ResourceBundle

编程入门 行业动态 更新时间:2024-10-28 06:34:49
本文介绍了放置在文件夹中时找不到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

本文发布于:2023-11-13 10:08:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584094.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   中时   文件夹   ResourceBundle   MessageSource

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!