SprngBoot对静态资源的映射
$.对前端js类库和框架的引用
使用webjars打包成jar的形式进行引用
webjars地址:https://www.webjars.org/;

选择使用版本-- > 选择管理方式--> 复制依赖到项目的pom。xml中 。
等到依赖的加载完成 ,查看是否存在当前环境中

运行jquery包 ,测试是否可用。
浏览器url输入相关url地址:例如:
http://localhost:5200/webjars/jquery/3.3.1-2/jquery.js

添加成功!
2.直接饮用
在对相关web的自动装配 类的查看中,发现有很多中对静态资源映射的方法,
截取部分ResourseProperties.class类中的部分代码
@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/" }; /**
* Locations of static resources. Defaults to classpath:[/META-INF/resources/,
* /resources/, /static/, /public/].
综合源码的定义 ,推荐有以下几种静态资源的映射方法: /根路径。

3.index.html
对于大部分学习SpringBoot的都会经历一个坎,使用localhost:8080/会显示一个错误页面 并且报(status=404)。
解决springboot因启动项是否在controller等其他类之上的问题。
实际上在不存在index.html 发起一个空的url地址会报404,因为Springboot会自动去寻找index.html 并去优先加载。
@Bean
//配置欢迎页index.html
public WelcomePageHandlerMapping welcomePageHandlerMapping(
ApplicationContext applicationContext) {
return new WelcomePageHandlerMapping(
new TemplateAvailabilityProviders(applicationContext),
applicationContext, getWelcomePage(),
this.mvcProperties.getStaticPathPattern());
}
Ctrl 进
getWelcomePage()方法 。
static String[] getResourceLocations(String[] staticLocations) {
String[] locations = new String[staticLocations.length
+ SERVLET_LOCATIONS.length];
System.arraycopy(staticLocations, 0, locations, 0, staticLocations.length);
System.arraycopy(SERVLET_LOCATIONS, 0, locations, staticLocations.length,
SERVLET_LOCATIONS.length);
return locations;
}
private Optional<Resource> getWelcomePage() {
String[] locations = getResourceLocations(
this.resourceProperties.getStaticLocations());
return Arrays.stream(locations).map(this::getIndexHtml)
.filter(this::isReadable).findFirst();
}
//获取首页html
private Resource getIndexHtml(String location) {
return this.resourceLoader.getResource(location + "index.html");
}
public void setStaticLocations(String[] staticLocations) {
this.staticLocations = appendSlashIfNecessary(staticLocations);
}
private String[] appendSlashIfNecessary(String[] staticLocations) {
String[] normalized = new String[staticLocations.length];
for (int i = 0; i < staticLocations.length; i++) {
String location = staticLocations[i];
normalized[i] = location.endsWith("/") ? location : location + "/";
}
return normalized;
}
获取到首页资源并和路径拼接形成欢迎页自动映射index.html
所以将自动进入index.html 如果查询不到index.html的存在将会(status=404)。
查不到index.html。

成功界面!

4.
我注意到下面还有个方法
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
faviconRequestHandler()));
return mapping;
}

更改这些小图标的。


添加一个以此命名的图片当做小图标,

SprngBoot对静态资源的映射的更多相关文章
- SpringBoot对静态资源的映射规则
在WebMvcAutoConfiguration类中有相对应的方法addResourceHandlers public void addResourceHandlers(ResourceHandler ...
- SpringBoot 对静态资源的映射规则
一.所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源 webjars:以jar包的方式引入静态资源,如下:引入 jquery ...
- 【串线篇】spring boot对静态资源的映射规则
WebMvcAutoConfiguration的内部类 WebMvcAutoConfigurationAdapter 其中ResourceProperties点进去 其中addResourceHand ...
- SpringBoot中的五种对静态资源的映射规则
目录 1. webjars:以jar包的方式引入静态资源 2./** 访问当前项目的任何资源 3.首页index.html,被" /** "映射 4.自定义图标 / favico ...
- Spring Boot对静态资源的映射规则
规则一:所有 " /webjars/** " 请求都去classpath:/META-INF/resources/webjars/找资源 webjars:以jar包的方式引入静态资 ...
- JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术
在上篇博客中,我们聊了<JavaEE开发之SpringMVC中的自定义拦截器及异常处理>.本篇博客我们继续的来聊SpringMVC的东西,下方我们将会聊到js.css这些静态文件的加载配置 ...
- springboot06(静态资源映射)
xxxxAutoConfiguration xxxxproperties 对静态资源的映射规则 webjars @ConfigurationProperties(prefix = "spri ...
- 零基础学习java------39---------json格式交互,Restful(不懂),静态资源映射,SSM整合(ssm整合思想,application.xml文件详解(声明式事务管理),)
一. json格式交互(知道) 1 . 回顾ajax基本语法 $.ajax({ url:"", // 请求的后台路径 data:{"":"" ...
- Spring Boot 静态资源处理
spring Boot 默认的处理方式就已经足够了,默认情况下Spring Boot 使用WebMvcAutoConfiguration中配置的各种属性. 建议使用Spring Boot 默认处理方式 ...
随机推荐
- 使用RampTexture来控制diffuse shading
[RampTexture] RampTexture(渐变纹理),可以是1D/2D纹理. This allows you to accentuate the surface's colors to fa ...
- 欢迎加入本人建的QQ群,讨论技术,生活及每天都有招聘信息
银狐AS3游戏2 3438131 银狐AS3Help 1803321 银狐U3D 167024653 银狐AS3转H5 467565102 本人在MSDN上的帐号 https://social.msd ...
- SpringBoot30 整合Mybatis-Plus、整合Redis、利用Ehcache实现二级缓存、利用SpringCache和Redis作为缓存
1 环境说明 JDK: 1.8 MAVEN: 3. SpringBoot: 2.0.4 2 SpringBoot集成Mybatis-Plus 2.1 创建SpringBoot 利用IDEA创建Spri ...
- 电商项目面试题 及mysql面试题 太难没啥用
需要按照功能点把系统拆分,拆分成独立的功能.单独为某一个节点添加服务器.需要系统之间配合才能完成整个业务逻辑.叫做分布式.集群:同一个工程部署到多台服务器上.优点:1.把模块拆分,使用接口通信,降低模 ...
- 在Linux下配置.net网站
一.Linux安装 1.1 Linux环境 本篇文章选择VMWare虚拟机安装Linux,使用的Linux是CentOS-7.可以在百度上自行下载一个VMWare和CentOS-7镜像,建议使用最新版 ...
- Rabbit MQ参考资料
https://github.com/ServiceStack/rabbitmq-windows/blob/master/README.md https://github.com/rabbitmq/r ...
- Windows7 64位 安装mysql
Windows上安装MySQL还是比较方便的,之前做过一个Windows10上面的安装方法,但是一个同学说自己的电脑是Windows7的,所以我写一个Windows7上的MySQL安装方法. MySQ ...
- 关于Manifest merger failed : uses-sdk:minSdkVersion 8 cannot be smaller than version 16 declared in lib
日志有些长,标题显示不完,截图如下: 有图可以知道,是因为在引入的libary里面的build.gradle文件里面的minSdkVersion不一致导致这个问题出现的.修改一致即可.这个问题是在co ...
- Solr开发文档(转)
出处:http://www.cnblogs.com/hoojo/archive/2011/10/21/2220431.html Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持 ...
- 【转载】Zookeeper 安装和配置
[转载原文链接 ] Zookeeper的安装和配置十分简单, 既可以配置成单机模式, 也可以配置成集群模式. 下面将分别进行介绍. 单机模式 点击这里下载zookeeper的安装包之后, 解压到合适目 ...