1. 基本目录结构
src/main/java: 存放源码
src/main/resources
static/: 存放静态文件,比如css、js、image(访问方式 http://localhost:8080/js/main.js)
templetes/: 存放静态页面jsp,html,tpl
config/: 存放配置文件,application.properties
resources/:
public/:
application.properties
2. 静态资源文件的加载顺序
-
静态资源路径默认配置:
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/
-
SpringBoot 默认会挨个从 META/resources > resources > static > public 里面找是否存在相应的资源,如果有则直接返回。
3. 自定义静态资源文件目录
-
修改
src/main/resources/application.properties
文件,覆盖springboot的默认配置spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/test/
4. 访问静态页面
1. 方法一
-
引入依赖
<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注解
2. 方法二
- 将静态页面放到加载目录中,就可以直接访问。