变量表达式 

  变量表达式即OGNL表达式或Spring EL表达式(在Spring术语中也叫model attributes)。像这种:${user.name}
 
    <p>UserName is :  <span th:text="${user.name}">xiaoming</span> !</p>
 
  默认运行结果是:UserName is : xiaoming ! 如果${user.name}的值是xiaohong则输出UserName is : xiaohong !
  这种与JSP的方式一样

URL表达式

  Thymeleaf对于URL的处理是通过语法@{…}来处理,举个简单的例子:
 
    <a th:href="@{http://www.baidu.com}">Thymeleaf</a>
 
  Thymeleaf支持绝对路径URL也支持相对路径的URL:
    相对路径写法:
      @{…/thymeleaf/hello(userName=${user.username})}
    添加参数:
 
      @{/thymeleaf/hello(userName=${user.username},userPassWord=${user.userpassword})}
 
  URL后面的(userName=${user.username},userPassWord=${user.userpassword})表示将括号内的内容作为参数处理,多个参数用逗号隔开,该语法避免使用字符串拼接,大大提高了可读性
 
  如果要对URL进行修改,可以使用 th:href和th:src
 
    <!-- 访问的地址是 'http://localhost:8080/thymeleaf/hello?userName=xiaoming)'  -->
    <a href="hello.html"
         th:href="@{http://localhost:8080/thymeleaf/hello(userName=${user.username})}">view</a>
 
    <a href="hello.html" th:href="@{/thymeleaf/hello(userName=${user.username})}">view</a>

算术运算

  表达式支持算数运算,比如:+、-(减号)、*、/、%以及单独的 -(负号)
    简单举两个例子
    语法     实例                       运行结果
    +     <p th:text="${Product.price+999}" ></p>     1000(设Product.price为1)
    -     <p th:text="${Product.price-999}" ></p>     1(设Product.price为1000)

逻辑运算

  逻辑运算符>, <, <=,>=,==,!=,!都可以使用,唯一需要注意的是使用<,>时需要用它的HTML转义符

条件运算

  If-then: (if) ? (then)
 
  If-then-else: (if) ? (then) : (else)
 
  Default: (value) ?: (defaultvalue)

Thymeleaf常用标签

  关键字         操作                                    例子
  th:id         替换id                           <input th:id="'xxx' + ${collect.id}"/>
  th:text        文本替换                       <p th:text="${collect.description}">description</p>
  th:utext        支持html的文本替换                  <p th:utext="${htmlcontent}">conten</p>
  th:object       替换对象                       <div th:object="${session.user}">
  th:value          属性赋值                       <input th:value="${user.name}" />
  th:with           变量赋值运算                     <div th:with="isEven=${prodStat.count}%2==0"></div>
  th:style          设置样式                       th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"
  th:onclick        点击事件                       th:"'getCollect()'"
  th:each           属性赋值                       tr th:each="user,userStat:${users}"
  th:if             判断条件                       <a th:if="${userId == collect.userId}" >
  th:unless         和th:if判断相反                    <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
  th:href           链接地址                       <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
  th:switch         多路选择 配合th:case 使用              <div th:switch="${user.role}">
  th:case           th:switch的一个分支                  <p th:case="'admin'">User is an administrator</p>
  th:fragment       布局标签,定义一个代码片段,方便其它地方引用     <div th:fragment="alert">
  th:include        布局标签,替换内容到引入的文件            <head th:include="layout :: htmlhead" th:with="title='xx'"></head>
  th:replace        布局标签,替换整个标签到引入的文件          <div th:replace="fragments/header :: title"></div>
  th:selected       selected选择框 选中                  th:selected="(${xxx.id} == ${configObj.dd})"
  th:src            图片类地址引入                    <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
  th:inline         定义js脚本可以使用变量                <script type="text/javascript" th:inline="javascript">
  th:action         表单提交的地址                    <form action="subscribe.html" th:action="@{/subscribe}">
  th:remove         删除某个属性                     <tr th:remove="all"> 1.all:删除包含标签和所有的孩子。’2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。
  th:attr           设置标签属性,多个属性可以用逗号分隔         比如 th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。
 
 posted on 2019-12-04 20:52  wnwn  阅读(193)  评论(0编辑  收藏  举报