PageRenderTime 79ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/spring4-2015/src/main/java/com/doctor/spring4/config/WebConfig.java

https://gitlab.com/doctorwho1986/doctor
Java | 131 lines | 85 code | 20 blank | 26 comment | 0 complexity | a729cc82d134aef6c2ec2470d0ec17ef MD5 | raw file
  1. package com.doctor.spring4.config;
  2. import java.util.Arrays;
  3. import java.util.List;
  4. import java.util.Properties;
  5. import javax.annotation.Resource;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.annotation.ImportResource;
  10. import org.springframework.http.MediaType;
  11. import org.springframework.http.converter.HttpMessageConverter;
  12. import org.springframework.web.accept.ContentNegotiationManager;
  13. import org.springframework.web.accept.ContentNegotiationManagerFactoryBean;
  14. import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
  15. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  16. import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
  17. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  18. import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
  19. import org.thymeleaf.spring4.SpringTemplateEngine;
  20. import org.thymeleaf.spring4.view.ThymeleafViewResolver;
  21. import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
  22. import org.thymeleaf.templateresolver.TemplateResolver;
  23. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
  24. import com.alibaba.fastjson.support.spring.FastJsonJsonView;
  25. /**
  26. * 视图配置感觉xml配置要好,随时可动态改变,重启应用即可.
  27. *
  28. * @author doctor
  29. *
  30. * @since 2015年3月5日 上午1:12:25
  31. */
  32. @Configuration
  33. @EnableWebMvc
  34. @ComponentScan(basePackages = { "com.doctor.spring4.controller" })
  35. @ImportResource({ "classpath:/spring4_2015Config/contentNegotiationManager-config.xml" })
  36. public class WebConfig extends WebMvcConfigurerAdapter {
  37. // @Bean
  38. // public ViewResolver viewResolver() {
  39. // InternalResourceViewResolver internalResourceViewResolver = new
  40. // InternalResourceViewResolver();
  41. // internalResourceViewResolver.setPrefix("/WEB-INF/jsp/");
  42. // internalResourceViewResolver.setSuffix(".jsp");
  43. // return internalResourceViewResolver;
  44. // }
  45. @Resource(name = "mediaTypes")
  46. private Properties mediaTypes;
  47. @Resource(name = "defaultContentType")
  48. private MediaType defaultContentType;
  49. @Override
  50. public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
  51. configurer.enable();// Configure static content handling
  52. }
  53. @Override
  54. public void addViewControllers(ViewControllerRegistry registry) {
  55. // 等价xml配置: <mvc:view-controller path="/" view-name="home"/>
  56. registry.addViewController("/").setViewName("index");
  57. }
  58. @Override
  59. public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  60. converters.add(new FastJsonHttpMessageConverter());
  61. }
  62. @Bean
  63. public TemplateResolver templateResolver() {
  64. TemplateResolver templateResolver = new ServletContextTemplateResolver();
  65. templateResolver.setPrefix("/WEB-INF/html/");
  66. templateResolver.setSuffix(".html");
  67. templateResolver.setTemplateMode("HTML5");
  68. return templateResolver;
  69. }
  70. @Bean
  71. public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) {
  72. SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();
  73. springTemplateEngine.setTemplateResolver(templateResolver);
  74. return springTemplateEngine;
  75. }
  76. @Bean
  77. public ThymeleafViewResolver thymeleafViewResolver(SpringTemplateEngine templateEngine) {
  78. ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
  79. thymeleafViewResolver.setTemplateEngine(templateEngine);
  80. thymeleafViewResolver.setCharacterEncoding("utf-8");// 解决页面中文乱码
  81. return thymeleafViewResolver;
  82. }
  83. @Bean
  84. public ContentNegotiationManager contentNegotiationManager() {
  85. ContentNegotiationManagerFactoryBean contentNegotiationManagerFactoryBean = new ContentNegotiationManagerFactoryBean();
  86. contentNegotiationManagerFactoryBean.setFavorPathExtension(true);
  87. contentNegotiationManagerFactoryBean.setFavorParameter(false);
  88. // contentNegotiationManagerFactoryBean.addMediaType("json", MediaType.APPLICATION_JSON);
  89. // contentNegotiationManagerFactoryBean.addMediaType("html", MediaType.TEXT_HTML);
  90. // contentNegotiationManagerFactoryBean.setDefaultContentType(MediaType.APPLICATION_JSON);
  91. contentNegotiationManagerFactoryBean.setMediaTypes(mediaTypes);
  92. contentNegotiationManagerFactoryBean.setDefaultContentType(defaultContentType);
  93. return contentNegotiationManagerFactoryBean.getObject();
  94. }
  95. /**
  96. * 根据ContentNegotiatingViewResolver选择视图后缀策略,不同路径展现不同视图,详见spring 文档。
  97. *
  98. * @param contentNegotiationManager
  99. * @param thymeleafViewResolver
  100. * @return
  101. */
  102. @Bean
  103. public ContentNegotiatingViewResolver contentNegotiatingViewResolver(ContentNegotiationManager contentNegotiationManager, ThymeleafViewResolver thymeleafViewResolver) {
  104. ContentNegotiatingViewResolver contentNegotiatingViewResolver = new ContentNegotiatingViewResolver();
  105. contentNegotiatingViewResolver.setContentNegotiationManager(contentNegotiationManager);
  106. contentNegotiatingViewResolver.setOrder(0);
  107. contentNegotiatingViewResolver.setViewResolvers(Arrays.asList(thymeleafViewResolver));
  108. contentNegotiatingViewResolver.setDefaultViews(Arrays.asList(new FastJsonJsonView()));
  109. return contentNegotiatingViewResolver;
  110. }
  111. }