本文共 871 字,大约阅读时间需要 2 分钟。
最近使用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:/staticclasspath:/publicclasspath:/resourcesclasspath:/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中直接这样访问图片,就可以啦
下面是运行的截图!图片好啦
补充:
下面是我的hellozll.html中写的内容
下面是用controller中定义路径去访问hellozll.html文件
转载地址:http://bizci.baihongyu.com/