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;
@Configuration
@ComponentScan(value = "com.bookstore", includeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Controller.class)})
@EnableWebMvc public class SpringMVCConfig implements WebMvcConfigurer {
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(); 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 public ViewResolver getViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); resolver.setExposeContextBeansAsAttributes(true); resolver.setOrder(10); return resolver; }
@Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); }
@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("/**"); }
}
|