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("/**"); }
}
|