springmvc 拦截器

melonjj 2020-07-04

拦截器类 ==> 三个方法

方法前

方法后

页面渲染后

如何使用拦截器?

1)在springmvc.xml中配置拦截器

<!--springmvc 拦截器 -->
        <mvc:interceptors>
            <mvc:interceptor>    
                <mvc:mapping path="/**"/>
                <!-- 自定义的拦截器类 -->
                <bean class="com.itheima.springmvc.interceptor.Interceptor1">
                </bean>
            </mvc:interceptor>
        </mvc:interceptors>

2)自定义拦截器,写处理逻辑

public class Interceptor1 implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
        System.out.println("方法1前");
        return true;
    }
    

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {
        // TODO Auto-generated method stub
        System.out.println("方法1后");
        
    }
    
    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {
        System.out.println("页面1渲染完毕后");
        
    }

}

3) 访问请求 查看结果

//http://localhost/springmvc-mybatis/item/itemlist.action

springmvc 拦截器

相关推荐