Spring Boot 学习笔记(六)——静态资源和拦截器处理

xiaoxiao2025-04-11  17

嘿,看讲静态资源了吧

一个很重要的类:WebMvcConfigurerAdapter

默认资源映射

classpath相当于编程是看到的src/main/resources classpath:/META-INF/resources classpath:/resources classpath:/static classpath:/public 以上路径优先级递减。

经过试验,还加了一句: registry.addResourceHandler("/public/**").addResourceLocations(“classpath:/public/”);就可以访问public下面的资源了,之前是不可以的。 又经过其他测试得知:只有优先级最高的默认路径不需要设置,只要在路径输入访问的文件就可以;其他的路径都要进行配置,并在路径中输入 路径+文件名 才能访问。

# 默认值为 /** //这是对外暴露的访问路径,在地址栏写路径前加上的主路径 spring.mvc.static-path-pattern= # 默认值为 classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/ spring.resources.static-locations=这里设置要指向的路径,多个使用英文逗号隔开

插一句:Controller类里,可以返回json,返回视图,还可以直接返回静态页面,返回一个字符串的路径即可。

Spring Boot 的Web配置

全部自定义

傻了才会这么干吧… 创建配置类,也就是注解有@Configuration的类,然后再加上@EnableWebMvc注解,就能完全控制MVC配置了。

一般情况

创建配置类 extends WebMvcConfigurerAdapter,不用@EnableWebMvc注解,然后重写里面的方法即可。

1、自定义资源映射 addResourceHandlers
@Configuration public class MyWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter { /** * 配置静态访问资源 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/my/**").addResourceLocations("classpath:/my/"); super.addResourceHandlers(registry); } } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/my/**").addResourceLocations("file:E:/my/"); super.addResourceHandlers(registry); }

addResourceLocations指的是文件放置的目录,addResoureHandler指的是对外暴露的访问路径。这里说的文件都叫 静态资源。

2、页面跳转 addViewControllers

跟在Controller类里写跳转页面的方法是一样的功能。

/** * 以前要访问一个页面需要先创建个Controller控制类,再写方法跳转到页面 * 在这里配置后就不需要那么麻烦了,直接访问http://localhost:8080/toLogin就跳转到login.htm页面了 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/toLogin").setViewName("login"); super.addViewControllers(registry);

值的指出的是,在这里重写addViewControllers方法,并不会覆盖WebMvcAutoConfiguration中的addViewControllers(在此方法中,Spring Boot将“/”映射至index.html),这也就意味着我们自己的配置和Spring Boot的自动配置同时有效,这也是我们推荐添加自己的MVC配置的方式。

那么记一下笔记:所以这个方法和Controller类的方法默认访问的路径都是: src/main/resources/templates。所以这样的文件为什么就成了模板了?到底啥叫模板?

3、拦截器 addInterceptors

首先创建拦截器类 并实现 HandlerInterceptor接口; 然后重写WebMvcConfigurerAdapter 中的 addInterceptor方法,添加自定义的 拦截器类。

package com.dudu.interceptor; public class MyInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { boolean flag =true; User user=(User)request.getSession().getAttribute("user"); if(null==user){ response.sendRedirect("toLogin"); //重定向方法里,写的字符串是访问路径,这个路径是根据上面WebMvcConfigurerAdaper 中的addviewControllers方法,或Controller类中的路径方法配置的。 flag = false; }else{ flag = true; } return flag; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }

然后在WebMvcConfigurerAdapter 中重写addInterceptors方法

/** * 拦截器 * @param registry */ @Override public void addInterceptors(InterceptorRegistry registry) { // addPathPatterns 用于添加拦截规则 // excludePathPatterns 用户排除拦截 registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").excludePathPatterns("/toLogin","/login"); super.addInterceptors(registry); }

经过测试发现:静态资源是没有办法拦截的,至少这个拦截器类不行,或者WebMvcConfigurerAdapter的addInterceptors方法不行吧? 我觉得看一下这两个类的api可能找到解决的方法。

Reference: [1]:http://tengj.top/2017/03/30/springboot6/

转载请注明原文地址: https://www.6miu.com/read-5027992.html

最新回复(0)