1、什么是模板?模板是后端用于渲染数据的文件,服务端读取这些模板,并将一些数据填充到一些指定位置之后,返回给浏览器html。
2、springboot 的模板,默认是Thymeleaf ,但是也可以指定html,其实本质都一样,不过是文件后缀不一样罢了。当然html模板可以直接在浏览器打开,不过渲染的数据依旧没有。
3、如何使用模板渲染数据
package com.example.servingwebcontent;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class GreetingController {
@GetMapping("/greeting")
public String greeting(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) {
model.addAttribute("name", name);
return "greeting";
}
}
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
4、如果需要传参,则需要model对象。
5、模板文件的位置需要注意(默认src/main/resources/templates/greeting.html)
6、静态文件的位置resource/static
7、模板使用语法参考 http://www.ityouknow.com/springboot/2016/05/01/spring-boot-thymeleaf.html