0
点赞
收藏
分享

微信扫一扫

Spring MVC学习笔记四

闲云困兽 2021-09-25 阅读 95

拦截器的配置和使用:

描述:
实现方式:

具体实现:

一,自定义拦截器:
/**
 * xialu的自定义拦截器。
 *
 * @author xialu
 * @version 1.0
 * @date 2021/6/24 11:19 下午
 */
public class XialuHandlerInterceptor implements HandlerInterceptor {

    /**
     * 会在handler方法业务逻辑执行之前执行
     * 往往在这里完成权限校验工作
     *
     * @param request
     * @param response
     * @param handler
     * @return 返回值boolean代表是否放行,true代表放行,false代表中止
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("xialu:preHandle" + " hello");

        return true;
    }

    /**
     * 会在handler方法业务逻辑执行之后尚未跳转页面时执行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView 封装了视图和数据,此时尚未跳转页面呢,你可以在这里针对返回的数据和视图信息进行修改
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

        String xialu = (String) modelAndView.getModel().get("xialu");
        System.out.println("xialu:postHandle " + xialu);

    }

    /**
     * 页面已经跳转渲染完毕之后执行
     *
     * @param request
     * @param response
     * @param handler
     * @param ex       可以在这里捕获异常
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("xialu:afterCompletion " + "hi");
    }
}
  
二,添加配置到springmvc.xml
 <mvc:interceptors>
        <!--拦截所有handler-->
        <!--<bean class="com.lagou.edu.interceptor.MyIntercepter01"/>-->

        <mvc:interceptor>
            <!--配置当前拦截器的url拦截规则,**代表当前目录下及其子目录下的所有url-->
            <mvc:mapping path="/**"/>
            <!--exclude-mapping可以在mapping的基础上排除一些url拦截,表示不拦截handle下的请求-->
            <mvc:exclude-mapping path="/handle/**"/>
            <bean class="xialu.interceptor.XialuHandlerInterceptor"/>
        </mvc:interceptor>

    </mvc:interceptors>
三,拦截器中三个方法的执行顺序:



上面介绍了单个拦截器时,方法的执行顺序,如果程序中存在多个拦截器,执行的顺序又是怎样呢,
再创建一个拦截器,看下结果。

public class XiamiHandlerInterceptor implements HandlerInterceptor {
    /**
     * 会在handler方法业务逻辑执行之前执行
     * 往往在这里完成权限校验工作
     *
     * @param request
     * @param response
     * @param handler
     * @return 返回值boolean代表是否放行,true代表放行,false代表中止
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("xiami:preHandle");

        return true;
    }

    /**
     * 会在handler方法业务逻辑执行之后尚未跳转页面时执行
     *
     * @param request
     * @param response
     * @param handler
     * @param modelAndView 封装了视图和数据,此时尚未跳转页面呢,你可以在这里针对返回的数据和视图信息进行修改
     * @throws Exception
     */
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {

        System.out.println("xiami:postHandle");

    }

    /**
     * 页面已经跳转渲染完毕之后执行
     *
     * @param request
     * @param response
     * @param handler
     * @param ex       可以在这里捕获异常
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("xiami:afterCompletion");
    }

}
<mvc:interceptors>
        <!--拦截所有handler-->
        <!--<bean class="com.lagou.edu.interceptor.MyIntercepter01"/>-->

        <mvc:interceptor>
            <!--配置当前拦截器的url拦截规则,**代表当前目录下及其子目录下的所有url-->
            <mvc:mapping path="/**"/>
            <!--exclude-mapping可以在mapping的基础上排除一些url拦截,表示不拦截handle下的请求-->
            <mvc:exclude-mapping path="/handle/**"/>
            <bean class="xialu.interceptor.XialuHandlerInterceptor"/>
        </mvc:interceptor>

        <mvc:interceptor>
            <!--配置当前拦截器的url拦截规则,**代表当前目录下及其子目录下的所有url-->
            <mvc:mapping path="/**"/>
            <!--exclude-mapping可以在mapping的基础上排除一些url拦截,表示不拦截handle下的请求-->
<!--            <mvc:exclude-mapping path="/handle/**"/>-->
            <bean class="xialu.interceptor.XiamiHandlerInterceptor"/>
        </mvc:interceptor>

    </mvc:interceptors>

执行结果:





SpringMVC中的全局异常处理:

实例:
@ControllerAdvice
public class GlobalExceptionResolver {

    @ExceptionHandler(XialuException.class)
    public ModelAndView handleException(XialuException xialu, HttpServletResponse response) {
        // 实现自定义逻辑
        return null;
    }

    @ExceptionHandler(XiamiException.class)
    public ModelAndView handleException(XiamiException xiami, HttpServletResponse response) {
        // 实现自定义逻辑
        return null;
    }

    /**
     * 可以选择捕捉所有异常,再根据类型处理。
     * @param exception
     * @param response
     * @return
     */
    @ExceptionHandler(Exception.class)
    public ModelAndView handleException(Exception exception, HttpServletResponse response) {
        if (exception instanceof XialuException) {
            System.out.println("xialu hello");
        } else if (exception instanceof XiamiException) {
            System.out.println("xiami hi");
        } else {
            //......
        }
        return null;
    }
}
@ExceptionHandler(XialuException.class)
    public ModelAndView handleException(XialuException xialu, HttpServletResponse response) {
        // 实现自定义逻辑
        return null;
    }
举报

相关推荐

0 条评论