由于控制器是基于注解的
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.xx.pp.controller"></context:component-scan> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="defaultEncoding" value="utf-8" /> <property name="basenames"> <list> <value>WEB-INF/config/errorCode</value> <value>WEB-INF/messages/messages</value> </list> </property> </bean> <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> <property name="paramName" value="locale" /> </bean> <bean id="localeResolver" class="org.springframework.web.servlet.i18n.CookieLocaleResolver" /> <bean id="annotationMapper" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"> <property name="interceptors"> <list> <ref bean="localeChangeInterceptor" /> </list> </property> </bean> </beans>
以上配置是最小配置
配置完成以后就可以通过url?locale=zh_CN这种方式进行语言切换了
localeResolver要用CookieLocaleResolver而不是AcceptHeaderLocaleResolver,若是后者则会抛出异常:
throw new UnsupportedOperationException(”Cannot change HTTP accept header - use a different locale resolution strategy”);