6、SpringBoot Web开发 thymeleaf 模板引擎
1、SpringBoot中存放静态资源的目录有:resources static public。然后访问:http://localhost:8080/1.js
优先级:resources>static>public
2、定制一个首页,可以把首页放到 resources static public 这三个目录下,然后访问http://localhost:8080/直接跳到首页
3、SpringBoot 的模板引擎
1、引入thymeleaf依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2、把页面放在templates目录下即可:test.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div th:text="${msg}"></div> </body> </html>
3、编写Controller进行页面跳转
@Controller
public class IndexController {
@RequestMapping(value = "/test")
public String test(Model model){
model.addAttribute("msg","Hello Springboot");
model.addAttribute("lists", Arrays.asList("edwin","6666666666666"));
return "test";
}
}
4、thymeleaf的语法
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div th:text="${msg}"></div> <div th:each="list: ${lists}" th:text="${list}"></div> </body> </html>

浙公网安备 33010602011771号