首页 > Web开发 > 详细

扩展默认的spirngMVC功能

时间:2018-12-24 12:24:19      阅读:207      评论:0      收藏:0      [点我收藏+]

目录

扩展默认的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");
    }
}

扩展默认的spirngMVC功能

原文:https://www.cnblogs.com/lilihai/p/10167912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!