SpringMVC拦截器代码示例解析

作者:袖梨 2022-06-29

本篇文章小编给大家分享一下SpringMVC拦截器代码示例解析,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

web.xml文件





    
        springMVC
        org.springframework.web.servlet.DispatcherServlet

        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        1
    


    
        springMVC

        /

    













springMVC配置文件:



    
    
    
    
    
    
    
        
        jsp">
    

    

                
        
            
            
        
        
            
            
            
        

   


controller类:

package cn.hp.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
public class UserAction {
    @RequestMapping("/test1.do")
    public  String test01(){
        System.out.println("正在执行test1这个业务逻辑");
        return "index";
    }
    @RequestMapping("/test2.do")
    public  String test02(){
        System.out.println("正在执行test2这个业务逻辑");
        return "index";
    }
}

第一个拦截器配置:

package cn.hp.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestInterceptor1 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("第一个拦截器,执行业务逻辑之前执行此方法");
        return true;
    }
    @Override
    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("第一个拦截器,执行视图渲染之后执行此方法");
    }
}


第二个拦截器:

package cn.hp.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestInterceptor2 implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("第二个拦截器,执行业务逻辑之前执行此方法");
        return true;
    }
    @Override
    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("第二个拦截器,执行视图渲染之后执行此方法");
    }
}

相关文章

精彩推荐