赞
踩
- package cn.woniu.myInterseption;
-
- import org.springframework.lang.Nullable;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class MyIntercepror implements HandlerInterceptor {
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
- System.out.println("在controller之前执行!!!!");
- return true;
- //HandlerInterceptor.super.preHandle(request, response, handler);
- }
-
- /**
- * value: 只有抛出指定异常类型,才会被该方法捕捉到
- * @return
- */
- @ExceptionHandler(value = {java.lang.ArithmeticException.class})
- public ModelAndView mdv(){
- ModelAndView modelAndView = new ModelAndView();
- modelAndView.setViewName("error1");
- return modelAndView;
- }
-
- /**
- * 在业务代码执行完了但是还没有执行试图解析器,意思就是还没有返回页面前执行
- * * @param request
- * @param response
- * @param handler
- * @param modelAndView
- * @throws Exception
- */
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
- System.out.println("返回页面前");
- HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
- }
-
- /**
- * 页面已经返回后执行
- * @param request
- * @param response
- * @param handler
- * @param ex
- * @throws Exception
- */
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
- System.out.println("页面返回后!!!!");
- HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
- }
- }
-
-
-
创建springmvc配置类
- package cn.woniu.configuration;
-
- import cn.woniu.myInterseption.MyIntercepror;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
- @Configuration
- public class MyMvcHandler implements WebMvcConfigurer {
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(new MyIntercepror())//将我们自定义拦截器
- .addPathPatterns("/**")//所有路径都拦截
- .excludePathPatterns("/","/login");//配置不需要拦截的url
- }
- }
配置拦截器后会造成页面静态资源无法加载的问题
修改配置文件
#配置mvc静态资源目录 不配置默认为"/**" spring.mvc.static-path-pattern=/static/**
要加上"/satic/"
- <link href='/static/bootstrap/css/bootstrap.css' rel="stylesheet">
- <script type="text/javascript" src='/static/js/jquery-3.5.1.js'></script>
- <script type="text/javascript" src='/static/bootstrap/js/bootstrap.js'></script>
返回类
- package cn.woniu.controller;
-
- import cn.woniu.entity.Users;
- import org.apache.ibatis.jdbc.Null;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class HandlerController {
- @RequestMapping("/hhh")
- public String testExcepition(){
- int i =1/0;
- // Users users=null;
- // users.getId();
- throw new NullPointerException();
- //return "111";
- }
-
-
-
- @RequestMapping(value="/login")
- public String login(){
-
- return "login";
- }
- @RequestMapping(value="/index" )
- public String index(){
- System.out.println("执行controller方法");
- return "index";
- }
- }
访问login,显示拦截
释放通行后,得到返回值
我们在哪里控制放行和关闭,就是拦截器第一个方法的返回值,true为放行,false为关闭
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。