在SpringBoot中访问静态资源

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

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

这里写图片描述

user.html

这里写图片描述

2.png

这里写图片描述

在user.html中引用2.png

这里写图片描述

第二种方式:放在classpath下

ResourceProperties中的说明

org.springframework.boot.autoconfigure.web.ResourceProperties

    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/public/" };
  • 1
  • 2
  • 3

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

这里写图片描述

person/index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link href="/css/main.css" 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里写图片描述

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

application.properties

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

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值