个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaweb
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
HandlerInterceptor
接口SpringMvcConfig
类中扫描加载beanpackage com.practice.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;
/**
* @Author YJ
* @Date 2023/8/16 18:34
* Description:拦截器
*/
@Component
public class ProjectInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
}
preHandle()
方法中返回需注意:postHandle()
和afterCompletion()
方法。preHandle()
方法·。WebMvcConfigurationSupport
,实现addInterceptors
方法package com.practice.config;
import com.practice.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;
/**
* @Author YJ
* @Date 2023/8/16 18:39
* Description:
*/
@Configuration
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Override
protected void addInterceptors(InterceptorRegistry registry) {
...
}
}
package com.practice.config;
import com.practice.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;
/**
* @Author YJ
* @Date 2023/8/16 18:39
* Description:
*/
@Configuration
@ComponentScan({"com.practice.controller"})
public class SpringMvcSupport extends WebMvcConfigurationSupport {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
protected void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterceptor).addPathPatterns("/address","/address/*");
}
}
WebMvcConfigurer
简化开发package com.practice.config;
import com.practice.controller.interceptor.ProjectInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author YJ
* @Date 2023/8/15 18:59
* Description:
*/
//3.创建Springmvc配置文件,加载controller对应的bean
@Configuration
@ComponentScan({"com.practice.controller"})
//开启由json数据转换为对象的功能
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
@Autowired
private ProjectInterceptor projectInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(projectInterceptor).addPathPatterns("/address","/address/*");
}
}
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle...");
return true;
}
request
:请求对象response
:响应对象handler
:被调用的处理器对象,本质上是一个方法对象,对反射技术中的Method对象进行了再包装public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle...");
}
modelAndView
:如果处理器执行完成具有返回结果,可以读取到对应数据与页面信息,并进行调整public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion...");
}
ex
:如果处理器执行过程中出现异常对象,可以针对异常情况进行单独处理afterCompletion
操作preHandle
返回均为true时,顺序是:pre1–>pre2–>pre3–>controller–>post3–>post2–>post1–>after3–>after2–>after1preHandle
返回为true,3拦截器的preHandle
返回为false时,顺序是:pre1–>pre2–>pre3–>after2–>after1preHandle
返回为true,2、3拦截器的preHandle
返回为false时,顺序是:pre1–>pre2–>after1preHandle
返回为false时顺序是:pre1–>结束