springboot 删除路径下面所有文件_springboot的目录文件结构

本文介绍了SpringBoot中静态文件如CSS、JS、图片的存放位置及访问方式,包括`static`和`templates`目录。还讨论了SpringBoot如何加载静态资源,从`META-INF/resources`到`public`的顺序,并提供了配置自定义静态资源路径的方法。
摘要由CSDN通过智能技术生成

1目录讲解

src/main/java:存放代码

src/main/resources

static:存放静态文件,比如:css  js image   (访问方式:http://localhost:8080/js/main.js)

templates:存放静态页面  jsp ,html,tpl

config : 存放配置文件,例如,application.properties

resources:存放脚本

2  pom.xml中引入依赖 Thymeleaf

org.springframework.boot

spring-boot-starter-thymeleaf

3文件的加载顺序,先加载静态文件

springboot会默认从 META/recources  > recources>staitic>public      里面是否存在相应的资源,如果有则直接返回,如果一直找不到,就会报错

可以自己在staic 下面多建几个文件,文件加中新建test.js。里面写一个测试js  console.info("xxx")  ,

然后run application  ,

通过地址访问  例如:http://localhost:8080/test.js

如果是访问templates下面的index.html,还是需要通过controller进行映射index.html

上例子:

3.1 在templates下面新建一个index.html的页面

3.2在pom.xml中添加模板依赖

org.springframework.boot

spring-boot-starter-thymeleaf

3.3在controller文件夹下添加FileController

请注意:controller中应该注释掉有相对路径的方法,否则无法访问到对应的 图片

4默认配置

(1)spring官网的默认配置

https://docs.spring.io/springboot/docs/current/reference/html/boot-features-developing-web-application.html#boot-features-spring-mvc-static-content

(2)在src/amin/resources中新建一个test文件夹,在里面有一个test.js,正常的localhost:8080/test/test.js是无法访问的,需要在src/amin/resourcesxia 新建一个application.properties,里面复制

spring.resources.static-location=classpath:/META-INF/resources/,classpath:/recources/,classpath:/static/,classpath:/public/,classpath:/test/

然后重新启动application,使用localhost:8080/test/test.js才能访问到test.js

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值