1.首先認識一下resources目錄下三個文件夾的作用。
public
相當於web-inf文件夾外的文件,外部通過主機加文件名的方式可以直接訪問。
比如訪問127.0.0.1:8080/mypublic.html
訪問public目錄下任何格式文件都可以,但是子目錄中的不行。
static
目錄名稱為css,html,js下面后綴名為html,css,js的文件可以從瀏覽器直接訪問。
比如訪問http://127.0.0.1:8080/css/easyloader.js
http://127.0.0.1:8080/html/no.html ,但是這些目錄中的子目錄文件不可外部直接訪問。
static目錄下的index.html文件可以直接訪問。訪問主機127.0.0.1會直接訪問127.0.0.1/index.html
template
Thymeleaf轉發會使用該目錄下的html文件
通過Thymeleaf
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Controller中
@Controller public class HelloController { @RequestMapping(value = "/hello", method = RequestMethod.GET) public String say() { return "hello"; } }
這樣返回“hello”就是forward內部templates中的hello.html。
轉發static下的html文件
如圖,static目錄下有goodie.html,unknown子目錄下有leigon.html文件,HTML子目錄下的go目錄有no.html文件
現在通過controller來轉發這些文件。
首先在application.yml中配置視圖解析器,刪除Thymeleaf庫,重新build。
spring:
mvc:
view:
prefix: /
suffix: .html
會轉發到對應的html

@GetMapping("/good") public String goo() { return "goodie"; } @GetMapping("/legion") public String legion() { return "/unknown/legion"; } @GetMapping("/no") public String no() { return "/html/go/no"; }