一、目录介绍:
src/main/java:存放代码
src/main/resources
static:存放静态文件,比如 css、js、image
templates:存放静态页面,比如jsp、html、tpl
config:存放配置文件,application.properties
二、同个文件的加载顺序(静态资源文件)
SpringBoot默认会挨个从META/resource>>resources>>static>>public里面查找是否存在相应的资源,如果有则直接返回。
三、访问不是默认文件下的静态资源文件
引入模版引擎(在pom中添加如下依赖)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在src/main/resources文件下创建html文件夹,在html文件夹下创建index.html文件,要访问index.html文件,需要在application.properties中添加下列配置:
spring.resources.static-locations = classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/html/
访问http://localhost:8080/index.html即可。
四、通过controller跳转页面
在templates文件夹下创建测试页面Hello.html,创建controller
HelloController如下:
@Controller
public class HelloController {
@RequestMapping(value = "/hello")
public Object hello(){
return "hello";
}
}
页面访问http://localhost:8080/hello即可进入hello.html页面。