解决Springboot2中无法访问在static/image/中的静态图片!终于解决啦

最近使用springboot2做项目发现,存放在resource/templates文件中的html中

竟然访问不到resource/static/image文件下的图片!

我想要在hellozll.html中访问seveny.png这个图片

然后使用了以下方法:(都访问失败)

<img src="../image/seveny.png"/> 

<img src="/image/seveny.png"/> 

<img src="../static/image/seveny.png"/>

<img src="/static/image/seveny.png"/>

图片无法显示,图片破了 

 解决方法:

 我们要知道Spring Boot 默认的访问静态资源的文件夹有以下四个

classpath:/static
classpath:/public
classpath:/resources
classpath:/META-INF/resources

我们可以看到,springboot默认可以找到static中的资源文件,但是我在static中又创建了image文件夹

所以啊,springboot在static中没找到seveny.png这个图片,所以就显示不了啊!

怎么解决呢!

在配置文件中加入以下配置

application.yml 的格式

spring:
   resources:
       static-locations: classpath:/templates/,classpath:/static/image/

application.properties 的格式

spring.resources.static-locations=classpath:/templates/,classpath:/static/image/

然后再html中直接这样访问图片,就可以啦 

<img src="/seveny.png">

 下面是运行的截图!图片好啦

 

补充:

下面是我的hellozll.html中写的内容 

 

下面是用controller中定义路径去访问hellozll.html文件

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值