• 扩展默认的spirngMVC功能


    扩展默认的spirngMVC功能

    步骤

    1、使用@Configuration标注为配置类
    2、实现WebMvcConfigurer接口
    3、 根据需要实现接口方法

    代码

    CustomMvcConfig

    @Configuration
    public class CustomMvcConfig implements WebMvcConfigurer {
    
        //自定义拦截器
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
    
            registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/test1");
    
        }
    
        //访问/showLogin时跳转到login视图
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
    
            registry.addViewController("/showLogin").setViewName("login");
        }
    
        
    }
    
    

    MyInterceptor

    public class MyInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                throws Exception {
    
            System.out.println("MyInterceptor.preHandle");
            return true;
        }
    
        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                ModelAndView modelAndView) throws Exception {
            System.out.println("MyInterceptor.postHandle");
        }
    
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
                throws Exception {
            System.out.println("MyInterceptor.afterCompletion");
        }
    }
    
  • 相关阅读:
    linux系统下MySQL表名区分大小写问题
    linux下查看Mysql默认编码、修改默认编码
    mysql的下载及安装(windows)
    数据库进阶
    mysql练习
    数据库基础
    jQuery
    JavaScript 基础学习(二)
    344.Reverse String
    计蒜客课程数据结构(顺序表)
  • 原文地址:https://www.cnblogs.com/lilihai/p/10167912.html
Copyright © 2020-2023  润新知