使用配置类和注解代替web.xml和SpringMVC配置文件的功能。
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>4.0.1version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.3.1version>
dependency>
<dependency>
<groupId>org.thymeleafgroupId>
<artifactId>thymeleaf-spring5artifactId>
<version>3.0.12.RELEASEversion>
dependency>
//web工程的初始化类,用来代替web.xml
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 执行spring的配置类
* @return
*/
@Override
protected Class<?>[] getRootConfigClasses() {
//Class[]表示创建Class类型的数组
return new Class[]{SpringConfig.class};
}
/**
* 设置springMVC的配置类
* @return
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
/**
* 获取DispatcherServlet的映射路径(URL-pattern)
* @return
*/
@Override
protected String[] getServletMappings() {
//需要将url-pattern的内容放在数组中返回
return new String[]{"/"};
}
/**
* 注册过滤器,方法重写ctrl+o
* @return
*/
@Override
protected Filter[] getServletFilters() {
//设置字符编码的过滤器
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceResponseEncoding(true);
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};
}
}
在Servlet 3.0环境中,容器会在类路径中查找实现
javax.servlet.ServletContainerInitializer
接口的类,如果找到的话,这些接口的实现类可以用来配置Servlet容器。
Spring提供了一个实现了ServletContainerInitializer
接口的类,名为SpringServletContainerInitializer
。这个类会被Servlet容器在启动时自动发现和执行。SpringServletContainerInitializer
的主要任务是查找实现了WebApplicationInitializer
接口的类,并将配置的任务委托给它们来完成。
WebApplicationInitializer
是一个Spring框架提供的接口,允许开发者以编程方式配置Servlet容器。Spring 3.2引入了一个方便的基础实现,名为AbstractAnnotationConfigDispatcherServletInitializer
,开发者可以扩展这个类来配置Servlet上下文,并且当这个类扩展的实现类部署到Servlet容器中时,容器会自动发现它并使用它来配置Servlet上下文。
总结:Spring确实利用了Servlet 3.0的特性,使得在容器启动时能够自动配置Servlet上下文,但需要注意的是WebApplicationInitializer
接口是Spring提供的,而不是Servlet规范的一部分。这个接口允许Spring开发者以程序化的方式配置Servlet容器。
package com.louis.config;
import org.springframework.context.annotation.Configuration;
//表示spring的配置类
@Configuration
public class SpringConfig {
}
package com.louis.config;
import com.louis.interceptor.TestInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
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.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
import java.util.List;
import java.util.Properties;
/**
* 代替SpringMVC的配置文件
* 1、开启扫描组件 2、视图解析器 3、mvc:view-controller 4、mvc:default-servlet-handler
* 5、mvc注解驱动 6、文件上传解析器 7、异常处理 8、拦截器
*/
//将当前类标识为一个配置类
@Configuration
//扫描组件
@ComponentScan("com.louis.controller")
//mvc的注解驱动
@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
//配置生成模板解析器
@Bean
public ITemplateResolver templateResolver(){
WebApplicationContext webApplicationContext = ContextLoader.getCurrentWebApplicationContext();
//ServletContextTemplateResolver需要一个ServletContext作为构造参数,可通过WebApplicationContext的方法获得。
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(webApplicationContext.getServletContext());
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setTemplateMode(TemplateMode.HTML);
return templateResolver;
}
//生成模板引擎并为模板引擎注入模板解析器
@Bean
public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
//生成视图解析器并为解析器注入模板引擎
@Bean
public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setCharacterEncoding("UTF-8");
viewResolver.setTemplateEngine(templateEngine);
return viewResolver;
}
/**
* mvc:default-servlet-handler
* @param configurer
*/
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
/**
* 拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
TestInterceptor testInterceptor = new TestInterceptor();
//可以设置添加或排除拦截规则
registry.addInterceptor(testInterceptor).addPathPatterns("/**");
}
/**
* mvc:view-controller
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//在templates下添加hello.html用于测试
registry.addViewController("/hello").setViewName(("hello"));
}
//文件上传解析器
@Bean
public MultipartResolver multipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
return commonsMultipartResolver;
}
//异常处理
@Override
public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
Properties prop = new Properties();
prop.setProperty("java.lang.ArithmeticException", "error");
exceptionResolver.setExceptionMappings(prop);
exceptionResolver.setExceptionAttribute("exception");
resolvers.add(exceptionResolver);
}
}
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
}
在WEB-INF下创建文件夹,文件夹中添加index.html文件。
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>首页title>
head>
<body>
首页
body>
html>