java View的Spring MVC配置类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java View的Spring MVC配置类相关的知识,希望对你有一定的参考价值。
package com.mycompany.config;
import java.util.Locale;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ViewResolver;
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.resource.PathResourceResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.mycompany")
@PropertySource(value={"classpath:config.properties"})
public class HelloWorldConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pdfs/**").addResourceLocations("/pdfs/").setCachePeriod(3600).resourceChain(true)
.addResolver(new PathResourceResolver());
registry.addResourceHandler("/js/**").addResourceLocations("/js/").setCachePeriod(3600).resourceChain(true)
.addResolver(new PathResourceResolver());
}
// @Bean
// public static PropertySourcesPlaceholderConfigurer properties() {
// return new PropertySourcesPlaceholderConfigurer();
// }
@Bean(name = "messageSource")
public ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:com/mycompany/resources/messages");
messageSource.setDefaultEncoding("UTF-8");
// System.out.println("etc, resource call");
return messageSource;
}
// @Bean(name = "localeResolver")
@Bean
public LocaleResolver localeResolver() {
CookieLocaleResolver resolver = new CookieLocaleResolver();
//Locale locale = LocaleContextHolder.getLocale();
// System.out.println("The language is = " + locale.getLanguage());
resolver.setDefaultLocale(new Locale("en"));
resolver.setCookieName("myLocaleCookie");
resolver.setCookieMaxAge(4800);
return resolver;
}
/*
* @Bean public LocaleResolver localeResolver() { SessionLocaleResolver
* resolver = new SessionLocaleResolver(); resolver.setDefaultLocale(new
* Locale("en")); return resolver; }
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
interceptor.setParamName("mylocale");
registry.addInterceptor(interceptor);
}
/*
* @Override public void
* configureContentNegotiation(ContentNegotiationConfigurer configurer) {
* configurer.defaultContentType(MediaType.APPLICATION_XML); }
*/
/*@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
// Simple strategy: only path extension is taken into account
configurer.favorPathExtension(true).ignoreAcceptHeader(true).useJaf(false)
.defaultContentType(MediaType.TEXT_HTML).mediaType("html", MediaType.TEXT_HTML)
.mediaType("xml", MediaType.APPLICATION_XML).mediaType("json", MediaType.APPLICATION_JSON);
}*/
}
以上是关于java View的Spring MVC配置类的主要内容,如果未能解决你的问题,请参考以下文章
spring mvc中 Circular view path 问题