• 04点睛Spring MVC 4.1-拦截器


    转发地址:https://www.iteye.com/blog/wiselyman-2214292

    4.1 拦截器

    • 拦截器实现了对每一个请求处理之前和之后进行相关的处理,类似于Servlet的filter;
    • 可以实现HandlerInterceptor接口或者继承HandlerInterceptorAdapter类;
    • 继承HandlerInterceptorAdapter类,因为使用接口要实现接口的所有方法;

    4.2 示例

    • 新建拦截器
    package com.wisely.interceptors;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Service;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    public class DemoInteceptor extends HandlerInterceptorAdapter {
        @Override
        public boolean preHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler) throws Exception {
            System.out.println("request开始前");
            long startTime = System.currentTimeMillis();
            request.setAttribute("startTime", startTime);
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request,
                HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            System.out.println("request结束后");
            long startTime = (Long) request.getAttribute("startTime");
            request.removeAttribute("startTime");
            long endTime = System.currentTimeMillis();
            request.setAttribute("handlingTime", endTime - startTime);
        }
    }
    
    • 修改配置,注册拦截器
    package com.wisely;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    import org.springframework.web.servlet.view.JstlView;
    import org.springframework.web.servlet.view.UrlBasedViewResolver;
    
    import com.wisely.interceptors.DemoInteceptor;
    
    @Configuration
    @ComponentScan("com.wisely")
    @EnableWebMvc
    public class DemoMVCConfig extends WebMvcConfigurerAdapter {
    
    
        @Bean
        public UrlBasedViewResolver viewResolver(){
            UrlBasedViewResolver resolver = new UrlBasedViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            return resolver;
        }
        //注册拦截器
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(demoInteceptor());
        }
        //自定义拦截器
        @Bean
        public DemoInteceptor demoInteceptor(){
            return new DemoInteceptor();
        }
    
    }
    
    • 在index.jsp添加代码接受拦截器里放置的handlingTime
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    index.jsp  is here
    
    value from inteceptor 处理耗时:${handlingTime}ms
    
    </body>
    </html>

    输出结果

  • 相关阅读:
    「Luogu2397」 yyy loves Maths VI (mode)
    「Luogu2014」 选课
    「Luogu2972」 [USACO10HOL]岩石和树木Rocks and Trees
    中国剩余定理
    点双连通分量
    Miller_Rabin大质数检验
    manachaer算法
    Kruskal重构树
    世界,你好!
    [Luogu P1450] [HAOI2008]硬币购物 背包DP+容斥
  • 原文地址:https://www.cnblogs.com/Jeely/p/11950243.html
Copyright © 2020-2023  润新知