這是我的配置文件作出迴應:彈簧4 MVC地點更改,但JSP不適用於本
初始化程序:
public class Initializer implements WebApplicationInitializer {
private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(ApplicationConfig.class);
servletContext.addListener(new ContextLoaderListener(ctx));
servletContext.setInitParameter("defaultHtmlEscape", "true");
servletContext.addListener(new HttpSessionEventPublisher());
servletContext.addListener(new RequestContextListener());
FilterRegistration.Dynamic fr = servletContext.addFilter("encodingFilter",
new CharacterEncodingFilter());
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, true, "/*");
servletContext.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");
ServletRegistration.Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME,
new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
servlet.setAsyncSupported(true);
ctx.setServletContext(servletContext);
}
public Filter[] getServletFilters() {
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceEncoding(true);
return new Filter[]{characterEncodingFilter, new DelegatingFilterProxy()};
}
}
ApplicationConfig:
@Configuration
@EnableWebMvc
@ComponentScan("com.example.*")
@PropertySource("classpath:app.properties")
public class ApplicationConfig extends WebMvcConfigurerAdapter {
private static final Charset UTF8 = Charset.forName("UTF-8");
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
OpenSessionInViewInterceptor sessionInViewInterceptor = new OpenSessionInViewInterceptor();
sessionInViewInterceptor.setSessionFactory(sessionFactory);
registry.addWebRequestInterceptor(sessionInViewInterceptor);
registry.addInterceptor(localeChangeInterceptor());
}
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
ret.setBasenames("classpath:messages", "classpath:labels", "classpath:exceptions");
ret.setDefaultEncoding(UTF8.name());
return ret;
}
// @Bean
// public SessionLocaleResolver localeResolver() {
// // TODO locale
// Locale ruLocale = new Locale.Builder().setLanguage("ru").build();
// SessionLocaleResolver localeResolver = new SessionLocaleResolver();
// localeResolver.setDefaultLocale(ruLocale);
// return localeResolver;
// }
@Bean
public CookieLocaleResolver localeResolver() {
CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setCookieName("localceCookie");
cookieLocaleResolver.setCookieMaxAge(3600);
return cookieLocaleResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("locale");
return localeChangeInterceptor;
}
}
所以我有LocaleChangeInterceptor,CookieLocaleResolver的, MessageSource的。我有labels.properties
和labels_en.properties
。
JSP:
Language : <a href="?locale=en">English</a>|<a href="?locale=ru">Russian</a>
<h2>
lable.testLocale : <spring:message code="lable.testLocale" text="default text" />
</h2>
Current Locale : ${pageContext.response.locale}
當我點擊鏈接打開英語,俄語,JSP重載和當前locale值在JSP顯示俄語和英語(取決於鏈接)。所以語言環境更改成功。出現名稱爲localceCookie
的Cookie,它包含ru或en值取決於所選語言環境,但JSP值label.testLocale
僅顯示en
語言環境的值,並且不會更改。我查了很多問題,其中大部分都是關於localeInterceptor的。
我嘗試了Tomcat,Jetty,JBoss(現在是野蠻)應用服務器。我嘗試了不同的瀏覽器。另外,我發現this tutorial,項目不起作用,我爲控制器做了一些類似@RequestMapping
的更改,但區域設置在此示例中不起作用。
UPDATE#1
因此到@TechBreak的答案i執行以下操作: 1.改變束的基本名稱,並設置默認語言環境中CookieLocaleResolver的
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
ret.setBasenames("/WEB-INF/messages", "/WEB-INF/labels", "/WEB-INF/exceptions");
ret.setDefaultEncoding(UTF8.name());
return ret;
}
@Bean
public CookieLocaleResolver localeResolver() {
Locale ruLocale = new Locale.Builder().setLanguage("ru").build();
CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(ruLocale);
cookieLocaleResolver.setCookieName("localceCookie");
cookieLocaleResolver.setCookieMaxAge(3600);
return cookieLocaleResolver;
}
- 移動到/ WEB-INF/
問題依然存在。
更新#2
現在我的配置是這樣的:
@Configuration
@EnableWebMvc
@ComponentScan("com.example.*")
@PropertySource("classpath:app.properties")
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Autowired
private SessionFactory sessionFactory;
private static final Charset UTF8 = Charset.forName("UTF-8");
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesBean() {
return new PropertySourcesPlaceholderConfigurer();
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/assets/**").addResourceLocations("/assets/").setCachePeriod(31556926);
registry.addResourceHandler("/css/**").addResourceLocations("/css/").setCachePeriod(31556926);
registry.addResourceHandler("/img/**").addResourceLocations("/img/").setCachePeriod(31556926);
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(31556926);
}
@Bean
public RequestMappingHandlerMapping handlerMapping() {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
handlerMapping.setInterceptors(localeChangeInterceptor());
return handlerMapping;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
OpenSessionInViewInterceptor sessionInViewInterceptor = new OpenSessionInViewInterceptor();
sessionInViewInterceptor.setSessionFactory(sessionFactory);
// registry.addWebRequestInterceptor(sessionInViewInterceptor);
registry.addInterceptor(localeChangeInterceptor());
}
// @Bean
// public SimpleControllerHandlerAdapter simpleControllerHandlerAdapter() {
// return new SimpleControllerHandlerAdapter();
// }
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/pages/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource ret = new ReloadableResourceBundleMessageSource();
ret.setBasenames("/WEB-INF/labels");
ret.setDefaultEncoding(UTF8.name());
return ret;
}
// @Bean
// public SessionLocaleResolver localeResolver() {
// // TODO locale
// Locale ruLocale = new Locale.Builder().setLanguage("ru").build();
// SessionLocaleResolver localeResolver = new SessionLocaleResolver();
// localeResolver.setDefaultLocale(ruLocale);
// return localeResolver;
// }
@Bean
public CookieLocaleResolver localeResolver() {
Locale ruLocale = new Locale.Builder().setLanguage("ru").build();
CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
cookieLocaleResolver.setDefaultLocale(ruLocale);
cookieLocaleResolver.setCookieName("localceCookie");
cookieLocaleResolver.setCookieMaxAge(3600);
return cookieLocaleResolver;
}
@Bean
public LocaleChangeInterceptor localeChangeInterceptor() {
// TODO locale
LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
localeChangeInterceptor.setParamName("locale");
localeChangeInterceptor.setHttpMethods(HttpMethod.GET.name(), HttpMethod.POST.name());
return localeChangeInterceptor;
}
}
我嘗試DefaultAnnotationHandlerMapping
,但它已被棄用,Spring says to useRequestMappingHandlerMapping
,在這兩種情況下,問題依然存在。在label.properties我有俄語,它是默認的語言環境,在label_en.properites英語語言。俄語區域設置爲默認語言環境。
最終配置
這是在配置
@Bean
public RequestMappingHandlerMapping handlerMapping() {
RequestMappingHandlerMapping handlerMapping = new RequestMappingHandlerMapping();
handlerMapping.setInterceptors(localeChangeInterceptor());
return handlerMapping;
}
的重要組成部分,爲默認的locale我做labels_ru.properties和英語語言環境中我做了labels.properties。
其中是您的labels_ru.properties?在它缺席的情況下,它將用於默認屬性文件。 – ScanQR
src/main/Java/resources – dikkini
它不應該在java下。它應該位於java資源目錄中。 – ScanQR