目的是:使用配置类和注解代替web.xml和Spring.MVC配置文件的功能
1.创建初始化类,代替web.xml
①创建一个Module,配置好pom.xml
②创建webapp和WEB-INF文件夹
③创建一个配置类
2.创建WebInit类代替web.xml,继承AbstractAnnotationConfigDispatcherServletInitializer
①WebInit
public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 指定spring的配置类
*/
@Override
protected Class>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
/**
* 指定springMVC的配置类
*/
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
/**
* 指定DispatcherServlet的映射规则。即url-pattern
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
/**
* 添加过滤器
*/
@Override
protected Filter[] getServletFilters() {
CharacterEncodingFilter encodingFilter = new CharacterEncodingFilter();
encodingFilter.setEncoding("UTF-8");
encodingFilter.setForceRequestEncoding(true);
HiddenHttpMethodFilter hiddenHttpMethodFilter = new HiddenHttpMethodFilter();
return new Filter[]{encodingFilter, hiddenHttpMethodFilter};
}
}
②创建SpringConfig 和 WebConfig配置类
@Configuration
public class SpringConfig {
}
@Configuration
public class WebConfig {
}
3. WebConfig代替SpringMVC配置文件
配置模板
//当前类标识一个配置类
@Configuration
// 开启扫描组件
@ComponentScan ("com.wzl.mvc.controller")
//开启MVC注解驱动
@EnableWebMvc
public class WebConfig {
//配置生成模板解析器
@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;
}
}
②TestController控制器
//访问主页
@Controller
public class TestController {
@RequestMapping("/")
public String index(){
return "index";
}
}
4.配置其他的,实现其他的配置。
实现接口implements WebMvcConfigurer
public class WebConfig implements WebMvcConfigurer{
}
配置默认servlet可用,default-servlet-handler
Ctrl+o 找到
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
配置拦截器
①创建拦截器
public class TestInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
②在WebConfig
@Override
public void addInterceptors(InterceptorRegistry registry) {
TestInterceptor testInterceptor = new TestInterceptor();
// 添加拦截路径
registry.addInterceptor(testInterceptor).addPathPatterns("/admin/**");
}
③在控制器添加
@RequestMapping("/admin/index")
public String index1(){
return "index";
}
④测试
配置view-controller
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
}
跳转到主页面
配置文件上传
//文件上传解析器
@Bean
public MultipartResolver multipartResolver(){
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
return commonsMultipartResolver;
}
配置异常解析器
@Override
public void configureHandlerExceptionResolvers(List resolvers) {
SimpleMappingExceptionResolver exceptionResolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
properties.setProperty("java.lang.ArithmeticException","error");
//设置异常映射
exceptionResolver.setExceptionMappings(properties);
exceptionResolver.setExceptionAttribute("ex");
resolvers.add(exceptionResolver);
}