SpringMVC注解配置

package com.bookstore.config;

import com.bookstore.interceptor.FirstInterceptor;
import org.apache.logging.log4j.core.tools.picocli.CommandLine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.List;
import java.util.Properties;

/**
* @Author BaiYZ
* @Program BookStoreFrameWork
* @Description SpringMVC配置类
* 1、扫描组件
* 2、视图解析器
* 3、view-controller
* 4、default-servlet-handler
* 5、mvc注解驱动
* 6、文件上传解析器
* 7、异常处理
* 8、拦截器
* @Date 2022-01-03 13:41:45
*/
//配置类
@Configuration
//开启注解扫描
@ComponentScan(value = "com.bookstore", includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)})
//开启mvc注解驱动
@EnableWebMvc
public class SpringMVCConfig implements WebMvcConfigurer {
// thymeleaf模板引擎参数

public final static String TEMPLATE_PREFIX = "/WEB-INF/templates/";
public final static String TEMPLATE_SUFFIX = ".html";
public final static String TEMPLATE_ENCODING = "UTF-8";
public final static Boolean TEMPLATE_CACHEABLE = false;
public final static String TEMPLATE_TYPE = "HTML5";
public final static Integer TEMPLATE_ORDER = 1;
//配置生成模板解析器

@Bean
public SpringResourceTemplateResolver getTemplateResolver() {
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
// ServletContextTemplateResolver,注入其中的webApplicationContext,可通过WebApplicationContext 的方法获得
SpringResourceTemplateResolver springResourceTemplateResolver = new SpringResourceTemplateResolver();
springResourceTemplateResolver.setApplicationContext(webApplicationContext);
//设置模板解析前缀
springResourceTemplateResolver.setPrefix(TEMPLATE_PREFIX);
//设置模板解析后缀
springResourceTemplateResolver.setSuffix(TEMPLATE_SUFFIX);
//设置文件编码
springResourceTemplateResolver.setCharacterEncoding(TEMPLATE_ENCODING);
//设置是否开启缓存
springResourceTemplateResolver.setCacheable(TEMPLATE_CACHEABLE);
//设置模板类型
springResourceTemplateResolver.setTemplateMode(TEMPLATE_TYPE);
//设置视图解析器优先级
springResourceTemplateResolver.setOrder(TEMPLATE_ORDER);
return springResourceTemplateResolver;
}
//生成模板引擎并为模板引擎注入模板解析器(使用接口接受子类模板解析器)

@Bean
public SpringTemplateEngine getTemplateEngine(ITemplateResolver templateResolver) {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
//生成视图解析器并未解析器注入模板引擎

@Bean
public ViewResolver getViewResolver(SpringTemplateEngine templateEngine) {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setOrder(1);
return viewResolver;
}
@Bean // 配置JSP视图解析器
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
resolver.setExposeContextBeansAsAttributes(true);
resolver.setOrder(10);
return resolver;
}

//启用ViewController视图控制器
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}

//启用默认Servlet handler
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

//配置文件上传解析器

@Bean
public MultipartResolver getMultipartResolver(){
return new CommonsMultipartResolver();
}

//配置异常解析器
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
//配置默认异常处理解析器
DefaultHandlerExceptionResolver defaultHandlerExceptionResolver = new DefaultHandlerExceptionResolver();
resolvers.add(defaultHandlerExceptionResolver);

//配置自定义异常解析器
SimpleMappingExceptionResolver simpleMappingExceptionResolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
//配置处理的异常信息
properties.setProperty("java.lang.ArithmeticException","error");//设置算数计算异常
simpleMappingExceptionResolver.setExceptionMappings(properties);
//配置输出异常
simpleMappingExceptionResolver.setExceptionAttribute("exception");
resolvers.add(simpleMappingExceptionResolver);
}

//添加拦截器
@Override
public void addInterceptors(InterceptorRegistry registry) {
FirstInterceptor firstInterceptor = new FirstInterceptor();
registry.addInterceptor(firstInterceptor).addPathPatterns("/**");
}

}