java 基于 Spring MVC LocaleChangeInterceptor 注释不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11752710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC LocaleChangeInterceptor annotation based doesn't work
提问by Poni
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
@Configuration
public class Config {
@Bean
public LocaleResolver localeResolver() {
final CookieLocaleResolver ret = new CookieLocaleResolver();
ret.setDefaultLocale(new Locale("en_US"));
return ret;
}
@Bean
public MessageSource messageSource() {
final ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
ret.setBasename("classpath:lang");
ret.setDefaultEncoding("UTF-8");
return ret;
}
@Bean
public HandlerMapping handlerMapping() {
final LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("language");
final DefaultAnnotationHandlerMapping ret = new DefaultAnnotationHandlerMapping();
ret.setInterceptors(new Object[] { interceptor });
return ret;
}
}
The above is my annotation configuration. I've basically translated thistutorial's XML.
以上是我的注解配置。我基本上已经翻译了本教程的 XML。
Strangely it doesn't work when I go to ...?language=fr
.
奇怪的是,当我去时它不起作用...?language=fr
。
However, the following does work (in app-servlet.xml
) (notice here it's using locale
):
但是,以下确实有效(在app-servlet.xml
)(请注意这里使用的是locale
):
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="locale" />
</bean>
</mvc:interceptors>
Another important thing to note is that when I put breakpoints on the above methods, all of the three of them, every breakpoint does break, which implies that "someone" is reading the values.
另一个需要注意的重要事项是,当我在上述方法上设置断点时,所有三个方法,每个断点都会中断,这意味着“有人”正在读取值。
So, why wouldn't my annotation based interceptor doesn't work?
那么,为什么我的基于注释的拦截器不起作用?
回答by swapy
Extending config class by WebMvcConfigurerAdapter
may help.
to add interceptor entry override
通过扩展配置类WebMvcConfigurerAdapter
可能会有所帮助。添加拦截器条目覆盖
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
method.
also add bean entry for LocaleChangeInterceptor
方法。还添加 bean 条目LocaleChangeInterceptor
@Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
回答by Wouter
In addition to what swap says, you need to add:
除了swap所说的,您还需要添加:
@Bean(name = "localeResolver")
public LocaleResolver getLocaleResolver(){
return new CookieLocaleResolver();
}
The bean name is important. That's the way spring will resolve the correct locale resolver.
bean 名称很重要。这就是 spring 将解析正确的语言环境解析器的方式。
Alternatively you can return the SessionLocaleResolver.
或者,您可以返回 SessionLocaleResolver。
If you do not add this you will get the following error:
如果不添加此项,则会出现以下错误:
Cannot change HTTP accept header - use a different locale resolution strategy
回答by Lord Nighton
The full example of Spring MVC 4.1.4.RELEASE localization is posted. Also you can use MKYong's example(but unfortunately its config is based on XML) to solve the problems with project structure.
发布了 Spring MVC 4.1.4.RELEASE 本地化的完整示例。您也可以使用 MKYong 的示例(但不幸的是它的配置是基于 XML 的)来解决项目结构的问题。
package com.pizza.config;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(value = "com.pizza")
public class WebConfig extends WebMvcConfigurerAdapter {
/* Resolvers and other MVC needs */
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}
/* Localization section is started */
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(localeChangeInterceptor());
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor(){
LocaleChangeInterceptor localeChangeInterceptor=new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("language");
return localeChangeInterceptor;
}
@Bean(name = "localeResolver")
public LocaleResolver getLocaleResolver(){
return new CookieLocaleResolver();
}
@Bean
public MessageSource messageSource() {
final ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
ret.setBasename("classpath:languages");
ret.setDefaultEncoding("UTF-8");
return ret;
}
}