thymeleaf的简单使用
基础写法:
th:xx="${}";
<form id="userForm">
<input id="id" name="id" th:value="${user.id}"/>
<input id="username" name="username" th:value="${user.username}"/>
<input id="password" name="password" th:value="${user.password}"/>
</form>
<div th:text="hello"></div>
<div th:text="${user.username}"></div>
选择变量表达式{}*
首先通过th:object 获取对象,然后使用th:xx = "*{}"获取对象属性。
<form id="userForm" th:object="${user}">
<input id="id" name="id" th:value="*{id}"/>
<input id="username" name="username" th:value="*{username}"/>
<input id="password" name="password" th:value="*{password}"/>
</form>
超链接标签@{/xx/xxx}
<a th:href="@{/url/请求的路径}" />
单机事件标签th:"${addTask()}"
<a th:"addTask()"></a>
list迭代循环
<div th:each="user:${userList}">
账号:<input th:value="${user.username}"/>
密码:<input th:value="${user.password}"/>
</div>
map迭代循环
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="maps : ${map}">
<td th:each="entry:${maps}" th:text="${entry.value.userid}" ></td>
<td th:each="entry:${maps}" th:text="${entry.value.username}"></td>
<td th:each="entry:${maps}" th:text="${entry.value.userage}"></td>
</tr>
</table>
内置七大对象
${#ctx}
上下文对象,可用于获取其它内置对象。
${#vars}:
上下文变量。
${#locale}:
上下文区域设置。
${#request}:
HttpServletRequest对象。
${#response}:
HttpServletResponse对象。
${#session}:
HttpSession对象。
${#servletContext}:
ServletContext对象。
常用的工具类
#strings:
字符串工具类
#lists:
List 工具类
#arrays:
数组工具类
#sets:
Set 工具类
#maps
:常用Map方法。
#objects:
一般对象类,通常用来判断非空
#bools:
常用的布尔方法。
#dates:
时间操作和时间格式化等。
#calendars:
用于更复杂时间的格式化。
#numbers:
格式化数字对象的方法。
日期格式化
日期格式化工具掌握format方法就够用了。
<input type="text" th:value="${#dates.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"/>
条件的判断
<div th:if="${userList}">
<div>的确存在..</div>
</div>
以及最最最重要的内联写法
标准格式为:[[${xx}]]
,可以读取服务端变量,也可以调用内置对象的方法。
个人理解:如果不想在<input/><a/>
等一系列标签的内部书写thymeleaf的标签,那么你直接就使用内联方式即可。
<a>
<strong>[[${ship.USER_NAME}]]</strong>
【[[${ship.type_name}]]】[[${ship.task_name}]]
</a>
<script th:inline="javascript">
var user = [[${user}]];`
var APP_PATH = [[${#request.getContextPath()}]];
var LANG_COUNTRY = [[${#locale.getLanguage()+'_'+#locale.getCountry()}]];
</script>