服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - 在SpringBoot中静态资源访问方法

在SpringBoot中静态资源访问方法

2021-02-24 14:28王学政 JAVA教程

这篇文章给大家介绍了在SpringBoot中静态资源访问方法,非常不错,具有参考借鉴价值,需要的朋友参考下吧

一、概述

springboot 默认静态资源访问的路径为:/static 或 /public 或 /resources 或 /meta-inf/resources 这样的地址都必须定义在src/main/resources目录文件中,这样可以达到在项目启动时候可以自动加载为项目静态地址目录到classpath下 ,静态访问地址其实是使用 resourcehttprequesthandler 核心处理器加载到webmvcconfigureradapter进行对addresourcehandlers方法进行覆盖.将静态访问目录进行重新定义。我们也可以实现其中方法,手动指定静态访问路径通过继承webmvcconfigureradapter重写内部方法addresourcehandlers也可以达到我们想要的效果。

第一种方式 : 放在src/main/webapp目录下

放在webapp目录下的静态资源是可以直接访问的

在SpringBoot中静态资源访问方法

user.html

在SpringBoot中静态资源访问方法

2.png

在SpringBoot中静态资源访问方法

在user.html中引用2.png

在SpringBoot中静态资源访问方法

第二种方式:放在classpath下

resourceproperties中的说明

?
1
2
3
4
org.springframework.boot.autoconfigure.web.resourceproperties
 private static final string[] classpath_resource_locations = {
   "classpath:/meta-inf/resources/", "classpath:/resources/",
   "classpath:/static/", "classpath:/public/" };

静态资源默认放在classpath路径下:defaults to classpath:[/meta-inf/resources/,/resources/, /static/, /public/] plus context:/ (the root of the servlet context).

在SpringBoot中静态资源访问方法

person/index.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>insert title here</title>
<link href="/css/main.css" rel="external nofollow" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="/js/main.js"></script>
<script type="text/javascript">
 sayhello();
</script>
</head>
<body>
 <h3>person page html</h3>
</body>
</html>

在SpringBoot中静态资源访问方法

通过修改配置项,设置静态资源的位置

?
1
2
3
application.properties
# 修改默认的静态资源存放目录
spring.resources.static-locations=classpath:/web/

在SpringBoot中静态资源访问方法

总结

以上所述是小编给大家介绍的在springboot中静态资源访问方法,希望对大家有所帮助如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:http://blog.csdn.net/w_x_z_/article/details/55657512

延伸 · 阅读

精彩推荐