当前位置:网站首页>MVC custom configuration
MVC custom configuration
2022-08-04 06:36:00 【Louzen】
学习资料
MVC自定义配置
实现WebMvcConfigurer接口,并重写相关方法
自定义拦截器(Login interception as an example)
// 1、先定义一个拦截器,实现HandlerInterceptorInterface and implement the three methods inside
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return Objects.nonNull(request.getAttribute("loginUser"));
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
// 2、实现WebMvcConfigurer接口对web进行自定义配置,重写addInterceptorsmethod to add a custom interceptor
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/**")
.excludePathPatterns("/", "/login");
}
}
边栏推荐
猜你喜欢
随机推荐
在AWS-EC2中安装Minikube集群
集合--LinkedList
LeetCode_Nov_3rd_Week
LeetCode_Dec_2nd_Week
LeetCode_Nov_4th_Week
管道重定向
An abstract class, internal classes and interfaces
C语言对文件的操作(完整版)
JDBC第一学之进行数据库连接时出现The server time zone.....解决办法
动态内存管理-C语言
(导航页)OpenStack-M版-双节点手工搭建-附B站视频
Design and implementation of legal aid platform based on asp.net (with project link)
[日常办公][ssh]cheatsheet
(位操作符)按位与、按位或、按位异或
The usefulness of bind() system call
(Navigation page) OpenStack-M version - manual construction of two nodes - with video from station B
arm交叉编译
LeetCode_Nov_4th_Week
arm学习-1-开发板
Detailed steps to install MySQL