静态资源映射规则、定制首页、定制404页面、配置网站的图标

静态资源映射规则

SpringBoot中对于静态资源(css,js,img....)的存放是有规定的;

在SpringBoot中,SpringMVC的web配置都在 WebMvcAtuoConfiguration这个自动配置类中,在其中有个静态内部类,也就是自动配置适配器:WebMvcAutoConfigurationAdapter,其中有个添加资源处理器方法addResourceHandlers,该方法是用来提供静态资源

public void addResourceHandlers(ResourceHandlerRegistry registry) {
// 判断有没有自定义映射规则
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
// 判断是否使用 webjars 方式,所有的 /webjars/**,都需要去 classpath:/META-INF/resources/webjars/ 找对应的资源
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
// 默认静态资源映射路径
// 点击去 getStaticPathPattern()方法
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}

去找 staticPathPattern

/**
* 用于静态资源的路径模式
*/
private String staticPathPattern = "/**";

/** 会访问当前的项目任意资源,它会去找 resourceProperties 这个类,我们可以点进去看一下

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties { private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { "classpath:/META-INF/resources/",
"classpath:/resources/", "classpath:/static/", "classpath:/public/" }; /**
* 静态资源的位置. 默认会到 classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
*/
private String[] staticLocations = CLASSPATH_RESOURCE_LOCATIONS;

所以得出结论:我们可以在 resources 的根目录下新建对应的文件夹,来存放我们对应的静态文件,它们的访问优先级依次从高到低如下:

"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
其中 "/":代表当前项目的根目录

这里的 classpath 代指 resources

比如:我们访问:localhost:8080/1.js,因为上面匹配的模式是:/**,而用于存放静态资源的路径是以上四个;所以它会自动帮我们去找这四个路径中对应的静态文件。

我们也可以在 application.yaml 中自定义静态资源映射路径:

spring:
resources:
# 自定义静态资源映射路径,SpringBoot默认的 4种即失效
static-locations: classpath:/rainszj/, classpath:/hello/

定制首页

继续看WebMvcAtuoConfiguration这个自动配置类

只要我们的index.html在上面的4种 classpath 静态资源映射路径中,访问时就会自动去寻找。

例如,我们访问:localhost:8080/ 或者 localhost:8080/index.html 它会自动帮我们去寻找到该页面。

定制错误页面

如果要显示给定状态代码的自定义HTML错误页,可以将文件添加到/error文件夹。错误页可以是静态HTML(即添加到任何静态资源文件夹下)或使用模板生成。文件名应为确切的状态代码或序列掩码。

例如,要将404映射到静态HTML文件,文件夹结构如下:

src/
+- main/
+- java/
| + <source code>
+- resources/
+- public/
+- error/
| +- 404.html
+- <other public assets>

即:我们只需在静态资源路径下 新建 error 文件夹,放置表示 相应状态码 的页面。

对于更复杂的映射,还可以添加实现 ErrorViewResolve接口的bean,如下例所示:

public class MyErrorViewResolver implements ErrorViewResolver {
@Override
public ModelAndView resolveErrorView(HttpServletRequest request,
HttpStatus status, Map<String, Object> model) { ModelAndView mv = new ModelAndView(); if (status.is4xxClientError()) {
// 设置视图名称,此处需要使用 thymeleaf 模板引擎的jar包
// 该视图放置到 templates 目录下
mv.setViewName("404");
} return mv;
}
}

添加bean:

/**
* 扩展 MVC
*/
@Configuration
public class MyMvcConfig implements WebMvcConfigurer { /**
* 注册Bean
* @return
*/
@Bean
public ErrorViewResolver myErrorViewResolver() {
return new MyErrorViewResolver();
} }

在 pom.xml 中添加 thymeleaf 的依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

配置 icon

在Spring Boot项目的issues中提出,如果提供默认的Favicon可能会导致网站信息泄露。如果用户不进行自定义的Favicon的设置,而Spring Boot项目会提供默认的上图图标,那么势必会导致泄露网站的开发框架。

因此,在Spring Boot2.2.x中,将默认的favicon.ico移除,同时也不再提供上述application.properties中的属性配置。

在SpringBoot 2.2.4 版本,没有 如下这个图标配置类了,为此我们使用 2.1.7版本

   @Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration implements ResourceLoaderAware { private final ResourceProperties resourceProperties; private ResourceLoader resourceLoader; public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
} @Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
} @Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
return mapping;
} @Bean
public ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(resolveFaviconLocations());
return requestHandler;
} private List<Resource> resolveFaviconLocations() {
String[] staticLocations = getResourceLocations(this.resourceProperties.getStaticLocations());
List<Resource> locations = new ArrayList<>(staticLocations.length + 1);
Arrays.stream(staticLocations).map(this.resourceLoader::getResource).forEach(locations::add);
locations.add(new ClassPathResource("/"));
return Collections.unmodifiableList(locations);
} } }

切换SpringBoot版本,只需要修改 pom.xml中的 parent 中的 version即可

配置 ico 只需两步,放置ico,在 application.yaml 中关闭默认的图标,图标名称必须为:favicon.ico

application.yaml

spring:
mvc:
favicon:
enabled=false # 关闭默认的图标

SpringBoot:静态资源映射、定制404、配置icon的更多相关文章

  1. springboot静态资源映射

    springboot静态资源映射 WebMvcAutoConfiguration @Override public void addResourceHandlers(ResourceHandlerRe ...

  2. springboot静态资源映射规则

    一.所有/webjars/**的请求,都会去classpath:/META-INF/resources/webjars/下的目录去找资源. 二.访问/**,即访问任何资源,如果没有controller ...

  3. [bug] springboot 静态资源 layui.css 404

    目录结构 引用路径 <link rel="stylesheet" href="../static/layui/css/layui.css" type=&q ...

  4. Springboot静态资源映射 “/” 引发的血案

    因为少写一个 / 浪费已个下午的时间,

  5. spring-boot配置静态资源映射的坑:properties文件不能添加注释

    如此博文所述,Spring Boot 对静态资源映射提供了默认配置 默认将 /** 所有访问映射到以下目录:classpath:/staticclasspath:/publicclasspath:/r ...

  6. SpringBoot 配置静态资源映射

    SpringBoot 配置静态资源映射 (嵌入式servlet容器)先决知识 request.getSession().getServletContext().getRealPath("/& ...

  7. Spring Boot 静态资源映射与上传文件路由配置

    默认静态资源映射目录 默认映射路径 在平常的 web 开发中,避免不了需要访问静态资源,如常规的样式,JS,图片,上传文件等;Spring Boot 默认配置对静态资源映射提供了如下路径的映射 /st ...

  8. SpringBoot——Web开发(静态资源映射)

    静态资源映射 SpringBoot对于SpringMVC的自动化配置都在WebMVCAutoConfiguration类中. 其中一个静态内部类WebMvcAutoConfigurationAdapt ...

  9. Springboot学习02-webjars和静态资源映射规则

    Springboot学习01-webjars和静态资源映射规则 前言 1-以前我们在IDEA中创建一个项目,添加web依赖包,我们现在是一个web应用,应该在man目录下面有一个webapp文件夹,将 ...

随机推荐

  1. 01-css3之过渡

    一.介绍 过渡(transition)是CSS3中具有颠覆性的特征之一,我们可以在不使用 Flash 动画或 JavaScript 的情况下,当元素从一种样式变换为另一种样式时为元素添加效果,经常和 ...

  2. 今天整理了几个在使用python进行数据分析的常用小技巧、命令。

    提高Python数据分析速度的八个小技巧 01 使用Pandas Profiling预览数据 这个神器我们在之前的文章中就详细讲过,使用Pandas Profiling可以在进行数据分析之前对数据进行 ...

  3. Websocket直播间聊天室教程 - GoEasy快速实现聊天室

    最近两年直播那个火啊,真的是无法形容!经常有朋友问起,我想实现一个直播间聊天或者我想开发一个聊天室, 要如何开始呢? 今天小编就手把手的教你用GoEasy做一个聊天室,当然也可以用于直播间内的互动.全 ...

  4. LCA基础 附例题(落谷)

    https://www.luogu.org/problemnew/solution/P3379 LCA叫做最短公共祖先,用来求距离树上两个节点最近的公共点: 常用倍增算法: #include<i ...

  5. sysbench安装和使用

    sysbench是一款测试工具 主要包括以下几种方式的测试: 1.cpu性能 2.磁盘io性能 3.调度程式性能 4.内存分配及传输速度 5.POSIX线程性能 6.数据库性能(OLTP基准测试)现在 ...

  6. Laravel - 基础

    1.使用 composer 创建项目 composer create-project --prefer-dist laravel/laravel blog 报错1 [ErrorException]pr ...

  7. python 使用记录及问题

    编码问题 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 1: ordinal not in range(12 ...

  8. jQuery(*****)

    参考1 参考2 1. jQuery 1. 选择器 $("") 1. 基本选择器 1. ID --> $("#d1") 2. 标签名 --> $(&q ...

  9. 使用Reactor响应式编程

    介绍 响应式编程 响应式编程不同于我们熟悉的命令式编程,我们熟悉的命令式编程即代码就是一行接一行的指令,按照它们的顺序一次一条地出现.一个任务被执行,程序就需要等到它执行完了,才能执行下一个任务.每一 ...

  10. 尤雨溪在直播中讲到的Vue3.0 Beta的那些特性,快记笔记了

    前言 在那天风雨交加的夜晚,Vue的创作者尤雨溪尤大大在b站直播分享了Vue.js 3.0 Beta最新进展.我对直播的内容进行了一下整理.整整用了三天的空余时间赶上了 1. 全新文档RFCs Vue ...