Спасибо @Ritesh за ответ.Вы даете мне идею справиться с этой проблемой.Я обошел это дальше:
public class InternationalizationFilter extends OncePerRequestFilter {
private Logger log=Logger.getLogger(InternationalizationFilter.class);
private String localeParam="lang";
private LocaleResolver localeResolver;
public InternationalizationFilter(String localeParam, LocaleResolver localeResolver) {
this.localeParam = localeParam;
this.localeResolver = localeResolver;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
protected void doFilterInternal(
final HttpServletRequest request,
final HttpServletResponse response,
final FilterChain filterChain)
throws ServletException, IOException {
if (localeResolver == null) {
throw new IllegalStateException("No LocaleResolver found: not in a DispatcherServlet request?");
}
else{
final String newLocale = request.getParameter(localeParam);
if (newLocale != null) {
final Locale locale = StringUtils.parseLocaleString(newLocale.toLowerCase());
LocaleContextHolder.setLocale(locale);
LocaleEditor localeEditor = new LocaleEditor();
localeEditor.setAsText(newLocale);
localeResolver.setLocale(request, response, (Locale) localeEditor.getValue());
log.debug("change locale to "+
locale.getLanguage()+"_"+locale.getCountry()+
" at Thread"+Thread.currentThread().getId());
}
else{
final Locale locale=localeResolver.resolveLocale(request);
LocaleContextHolder.setLocale(locale);
log.debug("restore locale to "+
locale.getLanguage()+"_"+locale.getCountry()+
" at Thread"+Thread.currentThread().getId());
}
try {
filterChain.doFilter(request, response);
} finally {
LocaleContextHolder.resetLocaleContext();
}
}
}
}
С объявленными localChangeInterceptor и localeResolver в конфигурационном файле mvc.Таким образом, мы можем изменить локаль с параметром запроса «lang»:
<mvc:interceptors>
<bean id="localChangeInterceptor"
class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"/>
</bean>
</mvc:interceptors>
<!--localeResolver-->
<bean id="localeResolver"
class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<!--<property name="cookieDomain" value=""/>-->
<property name="defaultLocale" value="zh"/>
</bean>
В файле конфигурации безопасности я создал i18nFilter и добавил его в цепочку фильтров:
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy">
<security:filter-chain-map path-type="ant">
<security:filter-chain pattern="/**/*.css" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/**/*.jpg" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/**/*.png" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/**/*.gif" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/**/*.js" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/**/*.cur" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/**/*.swf" filters="
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/login" filters="
i18nFilter,
securityContextPersistenceFilter,
exceptionTranslationFilter"/>
<security:filter-chain pattern="/checkin" filters="
i18nFilter,
securityContextPersistenceFilter,
authenticationFilter"/>
<security:filter-chain pattern="/**" filters="
i18nFilter,
securityContextPersistenceFilter,
authenticationFilter,
logoutFilter,
anonymousAuthenticationFilter,
exceptionTranslationFilter,
filterSecurityInterceptor" />
</security:filter-chain-map>
</bean>
<bean id="i18nFilter" class="com.bjinfotech.filter.InternationalizationFilter">
<constructor-arg name="localeParam" value="lang"/>
<constructor-arg name="localeResolver" ref="localeResolver"/>
</bean>
с помощью localChangeInterceptor& localeResolver, я могу изменить локаль и сохранить локаль в сеанс или файл cookie.
при помощи localeResolver, InternationalizationFilter может сохранять / восстанавливать локаль и вносить изменения в локаль LocaleContextHolder.