Spring Boot拦截器+视图映射器

1.使用拦截器需要实现两个接口

WebMvcConfigurer
HandlerInterceptor

2.实现WebMvcConfigurer接口的类,需要添加@SpringBootConfiguration注解

3.现在实现这两个类

MyInterceptors.java

package cn.luoruiyuan.login.config;

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

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

public class MyInterceptors implements HandlerInterceptor {
    @Override
    //进入controller之前进入这个方法
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入前。");
        return true;
    }

    @Override
    //进入controller之后进入这个方法
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("进入前后。");
    }

    @Override
    //方法执行完后
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("方法执行完后。");
    }
}

MyConfig.java

package cn.luoruiyuan.login.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.servlet.config.annotation.*;

@SpringBootConfiguration
public class MyConfig implements WebMvcConfigurer{

    @Override
    //拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        //addPathPatterns拦截路径
        //excludePathPatterns不拦截路径
        registry.addInterceptor(new MyInterceptors()).addPathPatterns("/hello").excludePathPatterns("/success");

        //多个拦截器就添加多个
        //registry.addInterceptor(new MyInterceptors()).addPathPatterns("/hello").excludePathPatterns("/success");
    }

    @Override
    //视图映射器
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送/hello请求来到view
        registry.addViewController("/hello") .setViewName("view");
    }

}

MyConfig.java(第二种写法)

package cn.luoruiyuan.login.config;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@SpringBootConfiguration
public class MyConfig {

    @Bean
    public WebMvcConfigurer myConfiguration(){
        WebMvcConfigurer webMvcConfig=new WebMvcConfigurer(){


            @Override
            //拦截器
            public void addInterceptors(InterceptorRegistry registry) {
                //addPathPatterns拦截路径
                //excludePathPatterns不拦截路径
                registry.addInterceptor(new MyInterceptors()).addPathPatterns("/hello").excludePathPatterns("/success");

                //多个拦截器就添加多个
                //registry.addInterceptor(new MyInterceptors()).addPathPatterns("/hello").excludePathPatterns("/success");
            }


            @Override
            //视图映射器
            public void addViewControllers(ViewControllerRegistry registry) {
                //浏览器发送/hello请求来到view
                registry.addViewController("/hello") .setViewName("view");
            }

        };
        return webMvcConfig;
    }



}

4.由于视图映射需要新建一个view.html

<!DOCTYPE>
<html lang="en" html xmlns:th="http://www.thymeleaf.org" xmlns:tiles="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>视图映射成功.</h1>

</body>
</html>

5.来查看结果(拦截器与视图映射都生效了)

26


(1)