暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

thymeleaf常用笔记

JAVA不归路 2021-09-09
241

thymeleaf基础

springboot引入thymeleaf

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

相关配置

spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
#开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false

在html添加

<html xmlns:th="http://www.thymeleaf.org">

if条件判断

<span style="color: white"  th:if="${session.username!=null}">[[${session.username}]]</span>
判断是否相同:
<span th:if="${name} eq 'jack'">相同于jack,</span>
<span th:if="${name} eq 'ywj'">相同于ywj,</span>
<span th:if="${name} ne 'jack'">不相同于jack,</span>

取值的两种方式:通过{}]],一个写在标签内,一个写在文本内,可以带上域名,可以不带

<span style="color: white"  ${session.username}">[[${session.username}]]</span>

循环,th:each="变量名:${域中的值}"

<div class="mingxing fl" th:each="mxsp:${mxsp}">
    <div class="sub_mingxing">
        <a th:href="@{toProduct(sname=${mxsp.getTname()})}">
            <img th:src="${mxsp.getImgadrr()}" alt="">
        </a>
    </div>
    <div class="pinpai" th:text="${mxsp.getTname()}"></a></div>
    <div class="youhui" th:text="${mxsp.getDescription()}"></div>
    <div class="jiage" th:text="${mxsp.getPrice()}"></div>
</div>

超链接和css,js引入等用@符号

<li><a th:href="@{http://www.mi.com/}" target="_blank">小米商城</a></li>
<link rel="stylesheet" type="text/css" th:href="@{/css/style.css}">

定义公共代码片段

th:fragment属性来定义一段公共的代码片段,然后你可以通过使用th:insert、th:replace属性来将这些公共的代码片段引入到模板文件中来。
<div th:fragment="side">xxxx</div>  给fragment起个名字
  <div th:replace="~{commons/commons::side}"></div>  
引用commons包下定义的公共代码片段  第一个commons是包名,第二个commons是页面名

<p th:text="'Welcome to ' + ${location} + '!'"></p>  字符串拼接
<p th:text="${user.age <= 60}"></p>        比较和相等
<p th:text="${user.online ? '在线' : '离线'}"></p>    三元运算
<p th:text="${token} ?: '你还没有登录,请先登录'"></p> 二元运算

字符串处理,大体与JAVA一致

${#strings.startsWith(name,'o')}
${#strings.endsWith(name, 'o')}
${#strings.indexOf(name,frag)}// 下标
${#strings.substring(name,3,5)}// 截取
${#strings.substringAfter(name,prefix)}// 从 prefix之后的一位开始截取到最后,比如 (ywj,y) = wj, 如果是(abccdefg,c) = cdefg//里面有2个c,取的是第一个c
${#strings.substringBefore(name,suffix)}// 同上,不过是往前截取
${#strings.replace(name,'las','ler')}// 替换
${#strings.prepend(str,prefix)}// 拼字字符串在str前面
${#strings.append(str,suffix)}// 和上面相反,接在后面
${#strings.toUpperCase(name)}
${#strings.toLowerCase(name)}
${#strings.trim(str)}
${#strings.length(str)}
${#strings.abbreviate(str,10)}// 我的理解是 str截取0-10位,后面的全部用…这个点代替,注意,最小是3位

参数传递

<div class="sub_mingxing">
    <a th:href="@{toProduct(sname=${mxsp.getTname()})}">    
     th:href="@{/Controller/behavior(param1=1,param2=${person.id})}"
传递参数用小括号括起来,多个用逗号隔开

日期处理

<span th:text="${#dates.format(user.date, ‘yyyy-MM-dd‘)}">4564546</span>
或者
<span th:text="${#dates.format(billingForm.startTime,‘yyyy-MM-dd HH:mm:ss‘)}">4564546</span>


文章转载自JAVA不归路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论