spring.thymeleaf.cache=false 关闭thymeleaf的缓存,实现页面热部署效果
参考更多:https://zhuanlan.zhihu.com/p/24988845?refer=dreawer
controller层要发送的数据
@Controller
public class HiController {
@RequestMapping("/index")
public String hi(HttpSession session,ModelMap map)
{
map.put("addString", "要拼接的内容");
map.put("flag", "yes");
map.put("name","茂均");
System.out.println(session.getAttribute("ji"));
List<Map<String,Object>>userList=new ArrayList<>();
for(int i=0;i<3;i++){
Map<String,Object> usersMap=new HashMap<>();
usersMap.put("name","我是"+i);
userList.add(usersMap);
}
map.put("users",userList);
return "index";
}
@RequestMapping("/test")
public String test(ModelMap map) {
map.put("thText", "设置文本内容");
map.put("thUText", "设置文本内容");
map.put("thValue", "设置当前元素的value值");
map.put("thEach", Arrays.asList("列表", "遍历列表"));
map.put("thIf", "msg is not null");
map.put("thObject", new Admin("没了kjlkj","这只热部署","sadf",1));
return "test";
}
}
index.html测试页面:
<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<!--index页传参给header页,header接受参数,并嵌入到index页中-->
<head th:include="header::headername" th:with="title='主页标题'">
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:include="copyright::copyright">保留内容</div><!--包含~~~~~文件名称::片段名称-->
<div th:replace="copyright::copyright">替换内容</div><!--替换-->
<span th:text="'这是字符拼接,'+${addString}"></span><br><span th:text="|或者形式2,${addString}|"></span><br>
<a th:if="${flag=='yes'}">显示true----这是if判断标签的用法</a><br>
<a th:unless="${flag!='yes'}">显示true----这是if判断标签的用法</a><br>
<table>
<tr >
<td>名字 </td>
<td>状态count    </td>
<td>状态size    </td>
<td>状态odd/even   </td>
<td>状态first/last   </td>
<td>当前的变量   </td>
</tr>
<tr th:each="user,iterStat:${users}">
<td th:text="${user.name}"></td>
<th th:text="${iterStat.count}">当前下标,count从0,index从1</th>
<th th:text="${iterStat.size}">list长度</th>
<th th:text="${iterStat.even}">even显示奇偶</th>
<th th:text="${iterStat.first}">开始和结尾</th>
<th th:text="${iterStat.current.name}">当前变量的名称</th>
</tr>
</table>
<br>
<!--thymeleaf内嵌变量的使用方法-->
<p th:text="${#dates.format(date,'dd/MMM/yyyy HH:mm')}"></p>
<p th:text="${#dates.createToday()}"></p>
<p th:text="${#strings.isEmpty(name)}">name</p>
<p th:text="${#strings.length(name)}">name</p>
<span th:if="${#httpSession.getAttribute('ok')}">ok</span>
<span th:unless="${#httpSession.getAttribute('ok')}">error no one</span>
<input type="text" th:value="${T(java.util.Calendar).getInstance().getTime().toLocaleString()}" />
<!--<script th:inline="javascript">
var name="hello,"+[[${name}]];
alert(name);
</script>-->
</body>
</html>
test.html页面
<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>TEST</h1>
<h2>Thymeleaf</h2>
<!--th:text 设置当前元素的文本内容,常用,优先级不高-->
<p th:text="${thText}" />
<p th:utext="${thUText}" />
<!--th:value 设置当前元素的value值,常用,优先级仅比th:text高-->
<input type="text" th:value="${thValue}" />
<!--th:each 遍历列表,常用,优先级很高,仅此于代码块的插入-->
<!--th:each 修饰在div上,则div层重复出现,若只想p标签遍历,则修饰在p标签上-->
<div th:each="message : ${thEach}"> <!-- 遍历整个div-p,不推荐-->
<p th:text="${message}" />
</div>
<p th:each="i:${#numbers.sequence(0,5)}" th:text="|循环了${i}次|"/>
<div> <!--只遍历p,推荐使用-->
<p th:text="${message}" th:each="message : ${thEach}" />
</div>
<!--th:if 条件判断,类似的有th:switch,th:case,优先级仅次于th:each, 其中#strings是变量表达式的内置方法-->
<p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p>
<!--th:insert 把代码块插入当前div中,优先级最高,类似的有th:replace,th:include,~{} :代码块表达式 -->
<!--<div th:insert="~{grammar/common::thCommon}"></div>-->
<!--th:object 声明变量,和*{} 一起使用-->
<div th:object="${thObject}">
<p>ID: <span th:text="*{type}" /></p><!--th:text="${thObject.id}"-->
<p>TH: <span th:text="*{userName}" /></p><!--${thObject.thName}-->
<p>DE: <span th:text="*{password}" /></p><!--${thObject.desc}-->
<p>DE: <span th:text="hi" /></p><!--${thObject.desc}-->
<p>DE: <span th:text="我是分" /></p><!--${thObject.desc}-->
</div>
<p th:utext="${'上空的飞机速度快'}" >是反对大师傅撒旦</p>
<div th:switch="3">
<p th:case="3">true</p>
<p th:case="2">false</p>
<p th:case="*">all</p>
</div>
</body>
</html>
被别的页面引用的通用页面header.html
<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head th:fragment="headername">
<meta charset="UTF-8">
<title th:text="${title}!=null?${title}:'layout'">header的标题</title>
</head>
<body>
</body>
</html>
copyright.html
<!DOCTYPE html>
<html lang="cn" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--定义一个代码片段-->
<footer th:fragment="copyright">©2019</footer>
</body>
</html>