springboot整合thymeleaf
目录结构

TestController
package com.itheima.controller; import com.itheima.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import java.util.*; /** * @author ljh * @version 1.0 * @date 2020/12/30 09:47 * @description 标题 * @package com.itheima.controller */ @Controller @RequestMapping("/test") public class TestController { @RequestMapping("/hello") public String hello(Model model){ //设置简单的数据类型 model.addAttribute("hello","hello world"); //设置复杂类型(POJO类型)需要在页面显示 User user = new User(); user.setName("zhangsan"); user.setId(1); user.setAddress("深圳"); model.addAttribute("user",user); //设置 List数据类型 List<User> users = new ArrayList<User>(); users.add(new User(1,"zhangsan","深圳")); users.add(new User(2,"李四","北京")); users.add(new User(3,"王五","武汉")); model.addAttribute("users",users); model.addAttribute("url","/test/hello"); model.addAttribute("key1","key1"); model.addAttribute("key2","key2"); //循环遍历Map Map<String,Object> dataMap = new HashMap<String,Object>(); dataMap.put("No","123"); dataMap.put("address","深圳"); model.addAttribute("dataMap",dataMap); //输出日期 model.addAttribute("date",new Date()); //后台设置一个年龄值 model.addAttribute("age",19); return "demo1"; } }
User
package com.itheima.pojo; /** * @author ljh * @version 1.0 * @date 2020/12/30 09:59 * @description 标题 * @package com.itheima.pojo */ public class User { private Integer id; private String name; private String address; public User() { } public User(Integer id, String name, String address) { this.id = id; this.name = name; this.address = address; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
MyThymeleafApplication
package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @author ljh * @version 1.0 * @date 2020/12/30 09:46 * @description 标题 * @package com.itheima */ @SpringBootApplication public class MyThymeleafApplication { public static void main(String[] args) { SpringApplication.run(MyThymeleafApplication.class,args); } }
demo1.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="${hello}">xianshihello world</p>
asjldfjalfjalfj
<br>
afdsafdsaf
asdfafdasf
<form>
输入姓名:<p th:text="${user.name}"></p>
输入ID:<p th:text="${user.id}"></p>
输入地址:<p th:text="${user.address}"></p>
</form>
<table>
<tr>
<td>id</td>
<td>姓名</td>
<td>地址</td>
<td>下标</td>
</tr>
<tr th:each="abc,mystat:${users}">
<td th:text="${abc.id}"></td>
<td th:text="${abc.name}">zhangsan</td>
<td th:text="${abc.address}">深圳</td>
<td th:text="${mystat.index+1}">深圳</td>
</tr>
</table>
<a href="http://www.itheima.com?id=1&name=2">去黑马</a><br>
<a th:href="@{/test/hello(id=1,name=2)}">去黑马</a><br>
<a th:href="@{/test/hello(id=${key1},name=${key2})}">去黑马</a><br>
<a th:href="@{${url}}">去黑马</a><br>
<a th:href="@{${url}(id=${key1},name=${key2})}">去黑马</a><br>
<div>
<div th:each="entry:${dataMap}">
key: <span th:text="${entry.key}"></span>
;
value:<span th:text="${entry.value}"></span>
</div>
</div>
<br>
日期:<span th:text="${#dates.format(date,'yyyy年MM月dd HH:mm:ss')}"></span>
<br>
<div th:if="${age}>18">当年龄超过18岁展示</div>
<br>
<button onclick="login()">点我</button>
</body>
<!--内联-->
<script th:inline="javascript">
var age = [[${age}]];
var login = function () {
alert(age);
}
</script>
</html>
application.yml
spring: thymeleaf: cache: false # 去除缓存

浙公网安备 33010602011771号