package com.zhang.controller.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//注意当前类必须受Spring容器控制
@Component//定义拦截器类,实现HandlerInterceptor接口
public class ProjectInterceptor implements HandlerInterceptor { //原始方法调用前执行的内容
//返回值类型可以拦截控制的执行,true放行,false终止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("Controller执行前面");
return true;
}
//原始方法调用后执行的内容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("Controller执行后面");
}
//原始方法调用完成后执行的内容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { System.out.println("Controller执行完成后");
}
}
3.2 配置加载拦截器,定义一个类继承 WebMvcConfigurationSupport这个类注意要被SpringMvcConfig配置类扫描到,不然不生效
package com.zhang.config;
import com.zhang.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcSupportConfig extends WebMvcConfigurationSupport {@Autowired
private ProjectInterceptor projectInterceptor;
protected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(projectInterceptor).addPathPatterns("/**");//拦截所有请求
}
}
3.3 Controllerpackage com.zhang.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/books")
public class BookController {@GetMapping()
public String save(){System.out.println("正在执行Controller:"+"Book...Save");
return "Hello SpringMvc";
}
}
3.4 posatman发送请求,后台打印结果4. 拦截器流程分析5. 拦截器参数
5.1 前置处理//原始方法调用前执行的内容
//返回值类型可以拦截控制的执行,true放行,false终止
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle..."+contentType);
return true;
}
参数
返回值
返回值为false,后面的Controller方法不执行
//原始方法调用后执行的内容
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...");
}
5.3 完成后处理注意:如果处理器方法出现异常了,该方法不会执行
//原始方法调用完成后执行的内容
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...");
}
参数
ex:如果处理器执行过程中出现异常对象,可以针对异常情况进行单独处理
6. 拦截器链配置 6.1 多个拦截器配置注意:无论处理器方法内部是否出现异常,该方法都会执行。
定义第二个拦截器
@Component
public class ProjectInterceptor2 implements HandlerInterceptor {@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("preHandle...222");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println("postHandle...222");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println("afterCompletion...222");
}
}
配置第二个拦截器
package com.zhang.config;
import com.zhang.controller.interceptor.ProjectInterceptor;
import com.zhang.controller.interceptor.ProjectInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class MvcSupportConfig extends WebMvcConfigurationSupport {@Autowired
private ProjectInterceptor2 projectInterceptor2;
@Autowired
private ProjectInterceptor projectInterceptor;
protected void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(projectInterceptor).addPathPatterns("/**");//拦截所有请求
registry.addInterceptor(projectInterceptor2).addPathPatterns("/**");
}
}
6.2 多个连接器工作流程分析你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
分享题目:9.SpringMvc拦截器-创新互联
浏览地址:https://www.cdcxhl.com/article38/dedipp.html
成都网站建设公司_创新互联,为您提供做网站、动态网站、App设计、微信公众号、营销型网站建设、电子商务
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联