如何使REST Spring-MVC应用程序国际化? [英] How to internationalize REST Spring-MVC application?

查看:105
本文介绍了如何使REST Spring-MVC应用程序国际化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是spring的新手,我仍然不太了解spring的层次结构和术语.我正在用Spring实现一个 RESTful 应用程序.搜索并阅读了有关如何使spring国际化的信息后,我尝试在我的应用程序中进行此操作.但是在我看来,它的配置不正确.因为我得到了例外.我想向您展示我的项目结构的屏幕截图.我想问你为什么我的applicationContext.xml显示问题.

I am new with spring and I still don't know well about hierarchy and terminology of spring. I am implementing a RESTful app with spring. After searching and reading about how to internationalize spring, I tried to do it in my app. But it seems to me it is not configured properly. Because I get exception. I would like to show you the screenshot of my project structure. And I would like to ask you why my applicationContext.xml show a problem exist.

spring-servlet.xml

<!-- SPRING INTERNALIZATION CONFIGURATION -->
    <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" /> 
        <property name="defaultEncoding" value="UTF-8"/>
    </bean>
    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en"/>
    </bean>
    <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="language" />
    </bean>
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>

推荐答案

设置一个基本名称数组,每个基本名称均遵循基本ResourceBundle约定,该约定不指定文件扩展名或语言代码,但与ResourceBundleMessageSource引用的Spring资源位置相反: "WEB-INF/messages.properties"的"WEB-INF/messages","WEB-INF/messages_en.properties"等.

Set an array of basenames, each following the basic ResourceBundle convention of not specifying file extension or language codes, but in contrast to ResourceBundleMessageSource referring to a Spring resource location: e.g. "WEB-INF/messages" for "WEB-INF/messages.properties", "WEB-INF/messages_en.properties", etc.

但是您已经将消息属性文件放置在Maven项目结构的src/main/resources下,因此它们将最终位于类路径的根目录(这意味着,即使Spring在类路径中寻找它们,您在配置中使用的/resources前缀将阻止Spring查找它们.)

But you've placed your message properties files under src/main/resources in the Maven project structure, so they will end up at the root of the classpath instead (which means that even if Spring looked for them in the classpath, the /resources prefix you've used in the configuration would prevent Spring to find them).

因此将属性文件放在WEB-INF下,并使用WEB-INF/messages作为basenames属性.

So place the properties file under WEB-INF, and use WEB-INF/messages as the basenames property.

这篇关于如何使REST Spring-MVC应用程序国际化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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