首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring无法加载资源包

Spring无法加载资源包
EN

Stack Overflow用户
提问于 2015-05-26 21:50:54
回答 2查看 1.4K关注 0票数 0

我声明了一个组件

代码语言:javascript
复制
@Component
public class I18nManagerImpl implements I18nManager{

@Autowired
MessageSource messageSource;

public ResourceBundle getResourceBundle() {
    Locale fromConfig = Locale.UK;

    //Resolve Locale from a configuration service

    return new MessageSourceResourceBundle(messageSource, fromConfig); //replace code in my question
}
@Override
public String message(String locale) {
    ResourceBundle beanResourceBundle = getResourceBundle();
    String invalidEmail = beanResourceBundle.getString("invalidEmail");
    return invalidEmail;
}

}

applicationContext.xml中的messageSource bean定义是

代码语言:javascript
复制
<bean id="messageSource"         
class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="messages"/>
</bean>

但是找不到消息,如果使用getResourceBundle键返回的调试器ResourceBundle为空并且没有内容。

提供的代码有什么问题?以下是项目结构图:

EN

回答 2

Stack Overflow用户

发布于 2015-05-26 21:59:00

代码语言:javascript
复制
<bean id="messageSource"         
class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames" value="classpath:messages"/>
</bean>

添加类路径

还可以添加-

代码语言:javascript
复制
<resources mapping="/resources/**" location="/resources/" />

由于您使用的是注解- Add

代码语言:javascript
复制
<annotation-driven />
<context:component-scan base-package="your.base.package" />

资源的命名空间-

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/mvc"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
票数 0
EN

Stack Overflow用户

发布于 2015-06-01 19:03:04

问题是propoerties文件不在源根目录中。我将文件插入到src config文件夹中,并将该文件夹标记为源根目录。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30460527

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档