thymeleaf的简单使用

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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值