static和templates 是springboot默认创建的文件夹,static中放静态页面,而templates中放动态页面
存放在static目录下,浏览器可以直接访问 http://localhost:8080/index.html,图片资源以及css,js可以放在此目录下,而templates 不能访问
若整合thymeleaf,需要引入jar
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
编写controller进行跳转
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import java.util.HashMap; @Controller public class IndexController { /* 本地访问内容地址 :http://localhost:8080/hello */ @RequestMapping("/hello") public String helloHtml(HashMap<String, Object> map) { map.put("hello", "欢迎进入HTML页面"); return "/index"; } /* 当引入了thymeleaf组件,动态跳转会覆盖默认的静态跳转 想跳转到/static/index.html,可以使用重定向return "redirect:/index.html" */ @RequestMapping("/hello2") public String hello2Html() { return "redirect:/index.html"; } }
动态的html代码
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"/> <title>templates中放动态页面</title> </head> <body> <h1>templates中放动态页面</h1> <p th:text="${hello}"></p> </body> </html>
静态html代码
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>static中放静态页面</title> </head> <body> <h1> static中放静态页面 </h1> </body> </html>
效果图如下
若在static下创建文件夹如css,js,images,必须在application.yml文件中进行声明,即使是默认的配置,否者无法访问
mvc: static-path-pattern: /**