• Spring学习日志之Spring MVC启动配置


    对DispatcherServlet进行配置

    Spring MVC的配置实际上就是对DispatcherServlet的配置

    public class DispatcherServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer
    {
    	@Override
    	protected Class<?>[] getRootConfigClasses()     // 加载自定义的配置类
    	{
    		return new Class<?>[]{BeanConfig.class, SecurityConfig.class};
    	}
    
    	@Override
    	protected Class<?>[] getServletConfigClasses()  // 加载web配置类,如viewResolver
    	{
    		return new Class<?>[]{WebConfig.class};
    	}
    
    	@Override
    	protected String[] getServletMappings()
    	{
    		return new String[]{"/"};
    	}
    }
    

    WebConfig的实现

    @Configuration
    @EnableWebMvc   // 在此开启WebMvc
    @ComponentScan(basePackageClasses = {BasePackageMarker.class})
    public class WebConfig extends WebMvcConfigurerAdapter
    {
    	@Bean
    	public ViewResolver viewResolver(TemplateEngine templateEngine)
    	{
    		ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    		viewResolver.setTemplateEngine(templateEngine);
    		viewResolver.setCharacterEncoding("UTF-8");
    
    		return viewResolver;
    	}
    
    	@Bean
    	public TemplateEngine templateEngine(ITemplateResolver templateResolver)
    	{
    		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    		templateEngine.setTemplateResolver(templateResolver);
    		templateEngine.addDialect(new SpringSecurityDialect());
    		return templateEngine;
    	}
    
    	@Bean
    	public ITemplateResolver templateResolver()
    	{
    		SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
    		templateResolver.setPrefix("/WEB-INF/templates/");
    		templateResolver.setSuffix(".html");
    		templateResolver.setTemplateMode("HTML5");
    		templateResolver.setCharacterEncoding("UTF-8");
    		templateResolver.setCacheable(false);
    
    		return templateResolver;
    	}
    
    	@Override
    	public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    	{
    		configurer.enable();
    	}
    }
    
  • 相关阅读:
    [mock open]PyUnit执行单元测试时使用字符串模拟文件对象
    bottle 0.5中的key-value数据库
    bottle模板中的替换
    返回不同值的小技巧
    带有参数的装饰器
    常用命令速查
    SQLAlchemy多线程下事务隔离机制详解
    Bancor 协议浅析
    Flask中 endpoint 解析
    pip 相关问题
  • 原文地址:https://www.cnblogs.com/xz816111/p/8017243.html
Copyright © 2020-2023  润新知