Springboot 目录结构及其资源文件访问

mac2022-06-30  133

1. 基本目录结构


src/main/java: 存放源码 src/main/resources static/: 存放静态文件,比如 html、css、js、image(访问方式 http://localhost:8080/js/main.js) templetes/: 存放动态页面,比如 jsp,html,tpl config/: 存放配置文件,application.properties ,applicatio.yml resources/: public/: application.properties

2. 配置文件存放位置及读取

详细请查看 https://blog.csdn.net/mrluzle/article/details/79164342

3. 静态资源文件的加载顺序

spring boot项目只有src目录,没有webapp目录,会将静态访问(html/图片等)映射到其自动配置的静态目录,如下

/static

/public

/resources

/META-INF/resources

比如,在resources建立一个static目录和index.htm静态文件,访问地址 http://localhost:8080/index.html 

 

原文:https://blog.csdn.net/wangb_java/article/details/71775637 


官方文档地址

静态资源路径默认配置:

spring.resources.static-locations = classpath:/META-INF/resources/, classpath:/resources/, classpath:/static/, classpath:/public/

SpringBoot 默认会挨个从 META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回。

 自定义静态资源文件目录


修改 src/main/resources/application.properties 文件,覆盖springboot的默认配置

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

3. 访问动态页面


① 方法一

引入依赖

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>

添加controller

@RequestMapping(value = "/api/v1/gopage") public Object index() { return "index"; } 此处Controller不能使用RestController注解,只能使用Controller注解

② 方法二

将动态页面放到加载目录中,就可以直接访问。

转载于:https://www.cnblogs.com/Shunia123/p/10593336.html

最新回复(0)