spring Boot 静态资源与拦截器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cszhang570221322/article/details/78137213

spring Boot 静态资源与拦截器

优先级:META-INF/resources > resources > static > public

 

一、映射资源路径

1、

spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:F:/收藏图片和视频以及音乐 勿删/视频/界面新闻视频/

直接在后,加上资源路径

2、继承WebMvcConfigurerAdapter,重写 addResourceHandlers方法

registey.addResourceHandle("pattrn-url").addResourceLocations("file位置")

 

二、url进入controller然后再跳转

输入http://localhost:8989/toindex 会跳转到 templates/index.html 页面,前提必须加入前台框架包,比如freemakerthymeleafjsp等等

二、拦截器addInterceptors

  • 1、创建我们自己的拦截器类并实现 HandlerInterceptor 接口
  • 2、其实重写WebMvcConfigurerAdapter中的addInterceptors方法把自定义的拦截器类添加进来即可

 

1. boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handle)方法:该方法将在请求处理之前进行调用,只有该方法返回true,才会继续执行后续的InterceptorController,当返回值为true 时就会继续调用下一个InterceptorpreHandle 方法,如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法; 
2.void postHandle (HttpServletRequest request, HttpServletResponse response, Object handle, ModelAndView modelAndView)
方法:该方法将在请求处理之后,DispatcherServlet进行视图返回渲染之前进行调用,可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。 
3.void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handle, Exception ex)
方法:该方法也是需要当前对应的InterceptorpreHandle方法的返回值为true时才会执行,该方法将在整个请求结束之

后,也就是在DispatcherServlet 渲染了对应的视图之后执行。用于进行资源清理。

 

猜你喜欢

转载自blog.csdn.net/cszhang570221322/article/details/78137213