热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

springboot与thymeleaf(3):设置属性、条件、遍历、局部变量、优先级、内联语法

前面记录了thymeleaf基本表达式,这里继续看一下其他功能.一.设置属性值这里的controller,html框架还是沿用上一篇的部分.html:<div

前面记录了 thymeleaf 基本表达式, 这里继续看一下其他功能.

一. 设置属性值

 这里的controller, html框架 还是沿用上一篇的部分.

html:

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">设置属性值h3>
    div>
    <div class="panel-body">
        
        <p th:elvin="${book.name}">1.自定义属性 th : elvinp>

        
        <p th:attr="elvin=${book.name},title=${book.name}">2.attr设置属性值p>

        
        <p th:title="${book1.name}">3.html原来的属性p>
        
        <input type="checkbox" th:checked="${book.price % 2 == 0}" th:disabled="${book.price % 2 == 0}" />a
        <input type="checkbox" th:checked="1" th:disabled="${book.price % 2 gt 0}" />b
        <input type="checkbox" th:checked="no" />c

        
        <p class="pc" th:classappend="cp" style="border: 1px solid blue;background-color:red;"
           th:styleappend="'background-color:#82af86;'">4.追加class和stylep>

        
        <p title="原来的title" th:attrappend="title=' 添加的title'" th:attrprepend="title='最前面的title '">5.前置,后置添加属性值p>

        
        <p th:onclick="|console.log('${book.publishTime}')|">6.设置事件属性p>

    div>
div>

 

结果展示:

 

二. 条件运算

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">条件运算h3>
    div>
    <div class="panel-body">
        
        
        <p th:if="${book.price > 10000}" th:text="|本书单价为:${book.price / 100}元, 有点小贵|">p>
        <p th:unless="${book.price > 10000}" th:text="|本书单价为:${book.price / 100}元, 价格可以接受|">p>

        <p th:switch="${book.price}">
            <span th:case="100" th:text="1块钱">span>
            <span th:case="${book.price}" th:text="${book.price / 100} + '元'">span>
            <span th:case="*" th:text="居然都不是, 只能选这个了">span>
        p>
    div>
div>

if 和 unless 是相反的, 所以如果只有一个 if , unless确实可以当成是if对应的else来使用. 当然, 如果是 if - else if - else if - else也是可以实现的, 但是要嵌套进去, 不方便阅读. 就不写了.

switch里面, case="*" 相当于java里面的default, 只要有一个满足条件, 别的都不会再显示和判断了.

结果展示:

 

三. 遍历

 数据准备: 在原来controller的方法下面继续添加

List bookList = new ArrayList<>();
bookList.add(book);
bookList.add(book1);
bookList.add(new Book("bootstrap", new DateTime().toString("yyyy-MM-dd"), 11000L));
bookList.add(new Book("Javascript", new DateTime().toString("yyyy-MM-dd"), 1020L));

model.addAttribute("bookList", bookList);

Map map = new HashMap<>();
String pre = "index_";

for (int i = 0; i ) {
    map.put(pre + i, bookList.get(i));
}

model.addAttribute("bookMap", map);

html:

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">列表Listh3>
    div>
    <div class="panel-body">
        <ul class="list-group">
            
            <li class="list-group-item" th:each="item,iterInfo:${bookList}">
                <span th:text="'index:' + ${iterInfo.index}">span>
                <span th:text="'size:' + ${iterInfo.size}" style="margin-left:10px;">span>
                <span th:text="'count:' + ${iterInfo.count}" style="margin-left:10px;">span>
                <span th:text="'odd:' + ${iterInfo.odd}" style="margin-left:10px;">span>
                <span th:text="'even:' + ${iterInfo.even}" style="margin-left:10px;">span>
                <span th:text="'first item:' + ${iterInfo.first}" style="margin-left:10px;">span>
                <span th:text="'last item:' + ${iterInfo.last}" style="margin-left:10px;">span>
                <span th:text="${item.name}" style="margin-left:10px;">span>
                <span th:text="${item.price} + '分'" style="margin-left:10px;">span>
                <span th:text="${item.publishTime}" style="margin-left:10px;">span>
            li>
        ul>
    div>
div>

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">列表Maph3>
    div>
    <div class="panel-body">
        <ul class="list-group">
            
            <li class="list-group-item" th:each="item,iterInfo:${bookMap}">
                <span th:text="'index:' + ${iterInfo.index}">span>
                <span th:text="'size:' + ${iterInfo.size}" style="margin-left:10px;">span>
                <span th:text="'count:' + ${iterInfo.count}" style="margin-left:10px;">span>
                <span th:text="'odd:' + ${iterInfo.odd}" style="margin-left:10px;">span>
                <span th:text="'even:' + ${iterInfo.even}" style="margin-left:10px;">span>
                <span th:text="'first item:' + ${iterInfo.first}" style="margin-left:10px;">span>
                <span th:text="'last item:' + ${iterInfo.last}" style="margin-left:10px;">span>

                <span th:text="${item.key}" style="margin-left:10px;">span>
                <span th:text="${item.value.name}" style="margin-left:10px;">span>
                <span th:text="${item.value.price} + '分'" style="margin-left:10px;">span>
                <span th:text="${item.value.publishTime}" style="margin-left:10px;">span>
            li>
        ul>
    div>
div>

结果展示:

 

四. 局部变量

在each中,  item:${list}, 这个item, 其实就是一个局部变量. 

但是如果要定义自己的局部变量, 怎么操作呢.

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">局部变量h3>
    div>
    <div class="panel-body">
        
        <p th:with="first='123abc'" th:text="${first}">p>
        
        <p th:text="${two.name}" th:with="two=${book}">p>
        
        <p th:with="name=${book.name}, price=${book.price}" th:text="|${name} : ${price}|">p>
    div>
div>

结果展示:

 

五. 优先级

在上一part中, 发现了, 在同一个tag中, th:with 和 th:text 的顺序并不影响解析结果. 这肯定是有一个优先级顺序在里面

优先级从上往下, 逐渐变小

Order Feature Attributes
1 Fragment inclusion th:insert   th:replace
2 Fragment iteration th:each
3 Conditional evaluation

th:if   th:unless

th:switch  th:case

4 Local variable definition th:object  th:with
5 General attribute modification th:attr   th:attrprepend  th:attrappend
6 Specific attribute modification th:value  th:href  th:src ...
7 Text(tag body modification) th:text   th:utext
8 Fragment specification th:fragment
9 Fragment removal th:remove

 

六. th:remove

这里remove没有出现过, 正好在这里就看一下, remove 具体是要删除什么

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">removeh3>
    div>
    <div class="panel-body">
        <ul class="list-group" th:msg="all" th:remove="all">
            <li>all-1li>
            <li>all-2li>
        ul>
        <ul class="list-group" th:msg="body" th:remove="body">
            <li>body-1li>
            <li>body-2li>
        ul>
        <div class="list-group" th:msg="tag" th:remove="tag">
            <ul>
                <li>tag-1li>
                <li>tag-2li>
            ul>
        div>
        <div class="list-group" th:msg="all-but-first" th:remove="all-but-first">
            <p>all-but-first-1p>
            <p>all-but-first-2p>
        div>

        <div class="list-group" th:msg="none" th:remove="none">
            <p>none-1p>
            <p>none-2p>
        div>
    div>
div>

结果展示:

all: 将所有的都删除掉了, 仿佛没有出现过

body: 删除标签内部的内容

tag: 删除标签, 但是保留子项

all-but-first: 删除除第一个以外的所有后代

none: 什么都不做

 

七. 内联语法

其实这个在上一篇已经出现过. 内联语法, 主要是在模板中, 或者css, js中使用.

thymeleaf除了可以通过标签来使用变量, 还可通过[[${...}]]的方式来使用. 

1. 在css中, 想要使用变量

 <style th:inline="css">
        /*通过[ [ $ { }]]的方式访问model中的属性*/
        .bgcolor {
            background-color: [[${color}]]
        }
 style>

2. 在js中, 使用

 

3. 在html中使用

  <p th:inline="text">买了一本书:[[${book.name}]], 花了[[${book.price/100}]]块钱p>

不建议在标签内容里面这么写, 当然, 不利于原型的展示. 

4. 禁用[[${...}]]

<p th:inline="none">买了一本书:[[${book.name}]], 花了[[${book.price/100}]]块钱p>

Demo:

<div class="panel panel-primary">
    <div class="panel-heading">
        <h3 class="panel-title">内联语法h3>
    div>
    <div class="panel-body">
        
        <style th:inline="css">
            .color{
                background-color: [[${color}]];
            }
        style>

        
        <p th:inline="text" class="color">买了一本书:[[${book.name}]], 花了[[${book.price/100}]]块钱p>

        
        <p th:inline="none">买了一本书:[[${book.name}]], 花了[[${book.price/100}]]块钱p>

        
        <script th:inline="Javascript">
            var book = [[${book1}]];
            console.log(book);

            [# th:each="item:${bookList}"] console.log([[${item}]]); [/]
        script>
    div>
div>

 

结果展示:

这里有个 [# ] [/] , 可以当成是 标签 来用. 是thymeleaf提供的

 


推荐阅读
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • Nginx使用(server参数配置)
    本文介绍了Nginx的使用,重点讲解了server参数配置,包括端口号、主机名、根目录等内容。同时,还介绍了Nginx的反向代理功能。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文介绍了绕过WAF的XSS检测机制的方法,包括确定payload结构、测试和混淆。同时提出了一种构建XSS payload的方法,该payload与安全机制使用的正则表达式不匹配。通过清理用户输入、转义输出、使用文档对象模型(DOM)接收器和源、实施适当的跨域资源共享(CORS)策略和其他安全策略,可以有效阻止XSS漏洞。但是,WAF或自定义过滤器仍然被广泛使用来增加安全性。本文的方法可以绕过这种安全机制,构建与正则表达式不匹配的XSS payload。 ... [详细]
  • Android工程师面试准备及设计模式使用场景
    本文介绍了Android工程师面试准备的经验,包括面试流程和重点准备内容。同时,还介绍了建造者模式的使用场景,以及在Android开发中的具体应用。 ... [详细]
  • 网址:https:vue.docschina.orgv2guideforms.html表单input绑定基础用法可以通过使用v-model指令,在 ... [详细]
  • 纠正网上的错误:自定义一个类叫java.lang.System/String的方法
    本文纠正了网上关于自定义一个类叫java.lang.System/String的错误答案,并详细解释了为什么这种方法是错误的。作者指出,虽然双亲委托机制确实可以阻止自定义的System类被加载,但通过自定义一个特殊的类加载器,可以绕过双亲委托机制,达到自定义System类的目的。作者呼吁读者对网上的内容持怀疑态度,并带着问题来阅读文章。 ... [详细]
author-avatar
手机用户2602909207
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有