SpringMVC注解配置

SpringMVC注解配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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("/**");
}

}