web.xml 注解配置

package com.bookstore.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;
import javax.servlet.annotation.WebFilter;
/*
在Servlet3.0环境中,容器会在类路径中查找实现javax.servlet.ServletContainerInitializer接口的类
如果找到的话就用它来配置Servlet容器。Spring提供了这个接口的实现,名为SpringServletContainerInitializer
这个类反过来又会查找实现WebApplicationInitializer的类并将配置的任务交给它们来完成。
Spring3.2引入了一个便利的WebApplicationInitializer基础实现,名为AbstractAnnotationConfigDispatcherServletInitializer
当我们的类扩展了AbstractAnnotationConfigDispatcherServletInitializer并将其部署到Servlet3.0容器的时候
容器会自动发现它,并用它来配置Servlet上下文。
*/
/**
* @Author BaiYZ
* @Program BookStoreFrameWork
* @Description web.xml注解配置
* @Date 2022-01-04 22:25:21
*/

public class WebInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* @Description: 根配置就是配置Spring配置类
* @Author: BaiYZ
* @Date: 2022/1/4 22:26
* @return: java.lang.Class<?>[]
*/
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}

/**
* @Description: 配置SpringMVC映射配置(配置控制器)
* @Author: BaiYZ
* @Date: 2022/1/4 22:28
* @return: java.lang.Class<?>[]
*/
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMVCConfig.class};
}

/**
* @Description: 配置DispatcherServlet Mapping(配置控制器映射)URL-Pattern
* @Author: BaiYZ
* @Date: 2022/1/4 22:28
* @return: java.lang.String[]
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}

/**
* @Description: 注册过滤器
* @Author: BaiYZ
* @Date: 2022/1/4 22:35
* @return: javax.servlet.Filter[]
*/
@Override
protected Filter[] getServletFilters() {
//先配置编码过滤器
CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
characterEncodingFilter.setEncoding("UTF-8");
characterEncodingFilter.setForceResponseEncoding(true);
//隐藏请求分发过滤器,使用put delete post get不同请求分发请求
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{characterEncodingFilter,hiddenHttpMethodFilter};
}

}