How to internationalize REST Spring-MVC applicatio

2019-09-05 22:13发布

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>

1条回答
\"骚年 ilove
2楼-- · 2019-09-05 22:25

The javadoc of ReloadableResourceBundleMessageSource.setBaseNames() says:

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.

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).

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

查看更多
登录 后发表回答