关于国际化时报org.springframework.context.NoSuchMessageException错,具体到No message found under code '你的键名' for locale 'zh_CN'.的解决方案

使用IntelliJ IDEA开发工具解决方案:

总结原因,解决方案:

1,在使用messageSource.getMessage方法时,参数1的键名跟属性文件中键名不一致,比如Controller中是name ,而配置文件中却是names

2,因为使用springMvc提供了MessageSource类,所有也顺带给我们配置好了bean,我们只需注入(按名称注入)就行,但是要去总配置文件,也就是application,properties配置中添加

#为了spring找到资源文件
spring.messages.basename=message

使用Exlicps开发工具解决方案

总结原因:

1.如果你使用eclipse创建的工程是class和src分开的,那么资源属性文件一定要放在src目录以内。
2.属性文件名的写法:
messages_zh_CN.properties (中文)
messages_en_US.properties   (英文)
3.配置messageSource这个bean(注意:一定是messageSource不是messageResource ,这是Spring规定的)
 
一般报这个错,都是路径问题,所有改变bean的路径
默认配置是:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <!--默认到当前web应用下找 -->
                <value>classpath*:messages</value>
                <value>classpath:org/hibernate/validator/ValidationMessages</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="false"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
</bean>

改变路径位置:比如你的属性文件放在resources/message/messages.propeties,那么就改变路径为:

    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <!-- 默认到当前web应用下找  -->
                <value>classpath:/messages/messages</value>
                <value>classpath:/messages/ValidationMessages</value>
            </list>
        </property>
        <property name="useCodeAsDefaultMessage" value="false"/>
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="cacheSeconds" value="60"/>
    </bean>

问题解决方案纯粹个人方式,若有不足,请评论区提出,谢谢

猜你喜欢

转载自www.cnblogs.com/lwh-note/p/8967577.html
今日推荐