且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

国际化REST Spring-MVC应用程序?

更新时间:2023-01-09 22:49:39

请确保在 handleMapping bean的拦截器属性中添加了 localeChangeInterceptor bean,如下所示:

Rather make sure that you added the localeChangeInterceptor bean within the interceptors properties of your handleMapping bean as follows:

<bean id="handlerMapping"vclass="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="interceptors">
    <list>
      <ref bean="localeChangeInterceptor" />
    </list>
  </property>
</bean>

或尝试将LocaleChangeInterceptor注册为 mvc:interceptor :

<mvc:interceptors>    
  <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"
    p:paramName="language" />
</mvc:interceptors>

您需要在spring上下文文件的根声明中添加mvc名称空间架构(xmlns:mvc ="http://www.springframework.org/schema/mvc"),以便其外观如下:

You need to add the mvc namespace schema (xmlns:mvc="http://www.springframework.org/schema/mvc") in the root declaration of the spring context file so it may look as:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="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
                       http://www.springframework.org/schema/mvc
                       http://www.springframework.org/schema/mvc/spring-mvc.xsd">
...

</beans>