SpringMVC中拦截器的介绍

系列文章目录

SpringMVC中拦截器的学习


文章目录

  • 系列文章目录
  • 前言
  • 一、拦截器概念
  • 二、入门案例
  • 三、拦截器参数
  • 四、拦截器工作流程分析
  • 总结


前言


一、拦截器概念

SpringMVC中拦截器的介绍_第1张图片
SpringMVC中拦截器的介绍_第2张图片

二、入门案例

SpringMVC中拦截器的介绍_第3张图片
SpringMVC中拦截器的介绍_第4张图片
SpringMVC中拦截器的介绍_第5张图片

SpringMVC中拦截器的介绍_第6张图片
SpringMVC中拦截器的介绍_第7张图片
案例的项目结构
SpringMVC中拦截器的介绍_第8张图片

BookController

package org.example.controller;


import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;

/*@Controller
@ResponseBody*/
//这俩个注解合为@RestController
@RestController
@RequestMapping("/books")
public class BookController {
    //@RequestMapping(method = RequestMethod.POST)等价于@PostMapping
    @PostMapping
    public String save(){
        System.out.println("book save ..");
        return "{'module':'book save'}";
    }

    //@RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/{id}")
    public String delete(@PathVariable Integer id){
        System.out.println("book delete..."+id);
        return "{'module':'book delete'}";
    }

    //    @RequestMapping(method = RequestMethod.PUT)
    @PutMapping
    public String update(@RequestBody Book book){
        System.out.println("book update..."+book);
        return "{'module':'book update'}";
    }


    //    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public String getById(@PathVariable Integer id){
        System.out.println("book getById..."+id);
        return "{'module':'book getById'}";
    }

    //    @RequestMapping(method = RequestMethod.GET)
    @GetMapping
    public String getAll(){
        System.out.println("book getAll...");
        return "{'module':'book All'}";
    }
}


Book

package org.example.domain;

public class Book {
}

ProjectInterceptor

package org.example.controller.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ProjectInterceptor implements HandlerInterceptor {
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle....");
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion");
    }
}

SpringMvcSupport

package org.example.config;

import org.example.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
    @Autowired
    private ProjectInterceptor projectInterceptor;
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","books/*");
    }
}

SpringMvcConfig

package org.example.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan("org.example")
@EnableWebMvc
public class SpringMvcConfig {
}

ServletInitConfig

package org.example.config;

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

import javax.servlet.Filter;

public class ServletInitConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    protected Class<?>[] getServletConfigClasses() {
        return new Class[0];
    }

    protected String[] getServletMappings() {
        return new String[]{"/"};
    }


    //乱码处理

    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter=new CharacterEncodingFilter();
        filter.setEncoding("UTF-8");
        return new Filter[]{};
    }
}

三、拦截器参数

SpringMVC中拦截器的介绍_第9张图片

SpringMVC中拦截器的介绍_第10张图片

SpringMVC中拦截器的介绍_第11张图片

四、拦截器工作流程分析

SpringMVC中拦截器的介绍_第12张图片


总结

本节主要是为了了解拦截器及其使用方法

参考视频

你可能感兴趣的:(ssm,SpringMVC,拦截器)