SpringBoot(十六):Thymeleaf使用

栏目: Java · 发布时间: 6年前

内容简介:个人博客:https://aodeng.cc微信公众号:低调小熊猫QQ群:756796932

版权声明

单纯的广告

个人博客:https://aodeng.cc

微信公众号:低调小熊猫

QQ群:756796932

简介

简介懒得打了.....(此处省略1万字)

使用

这里举例使用thyme leaf的:赋值,拼接,if判断,unless判断,for 循环,URL,三目运算,switch 选择(后面继续添加)

页面代码

<h3>赋值<h3>
    <h3 th:text="${hope1}">Thymeleaf test page</h3>
    <h3>拼接<h3>
    <span th:text="'thymeleaf 普通拼接:'+${hopeName}+'!'"></span>
    <span th:text="|thymeleaf 简洁拼接:${hopeName}!|"></span>
    <h3>if判断<h3>
    true: <a th:if="${hopes=='hopes'} " th:href="@{https://aodeng.cc}">if</a>
    false:<a th:if="${hopes=='hope'} " th:href="@{https://aodeng.cc}">if</a>
    <h3>unless判断<h3>
    true: <a th:unless="${hopes!='hope'} " th:href="@{https://aodeng.cc}">unless</a>
    false:<a th:unless="${hopes=='hope'} " th:href="@{https://aodeng.cc}">unless</a>
    <h3>for 循环</h3>
    <table>
        <tr>
        <tr>
            id:
            name:
            password:
            sex:
            age:
            index:
        </tr>
        </tr>
        <tr th:each="user,iterStat:${userlist}">
            <td th:text="${user.id}"></td>
            <td th:text="${user.name}"></td>
            <td th:text="${user.password}"></td>
            <td th:text="${user.sex}"></td>
            <td th:text="${user.age}"></td>
            <td th:text="${iterStat.index}"></td>
        </tr>
    </table>
    terStat 称作状态变量,属性有:
    index,当前迭代对象的 index(从 0 开始计算);
    count,当前迭代对象的 index(从 1 开始计算);
    size,被迭代对象的大小;
    current,当前迭代变量;
    even/odd,布尔值,当前循环是否是偶数/奇数(从 0 开始计算);
    first,布尔值,当前循环是否是第一个;
    last,布尔值,当前循环是否是最后一个
    <h3>URL</h3>
        <a th:href="@{https://github.com/java-aodeng/{url}(url=${url})}">点击测试</a>
        上例中 URL 最后的(url=${url})表示将括号内的内容作为 URL 参数处理,该语法避免使用字符串拼接,大大提高了可读性
        <h3>三目运算</h3>
        <input th:value="${number gt 2 ? '值为1':'值不为1'}">
        gt:great than(大于)
        ge:great equal(大于等于)
        eq:equal(等于)
        lt:less than(小于)
        le:less equal(小于等于)
        ne:not equal(不等于)
     <h3>switch 选择</h3>
        <div th:switch="${switch}">
            <p th:case="'0'">switch等于0</p>
            <p th:case="'1'">switch等于1</p>
            <p th:case="*">未知</p>
        </div>

后台代码

@RequestMapping("/hope1")
    public String hope1(ModelMap map){
        map.addAttribute("hope1","Thymeleaf 赋值 start");
        map.addAttribute("hopeName","低调小熊猫很帅");
        map.addAttribute("hopes","hopes");
        List<User> userList=new ArrayList<>();
        User user=new User();
        user.setId(1);
        user.setName("admin");
        user.setPassword("123456");
        user.setSex("男");
        user.setAge(20);
        userList.add(user);
        map.addAttribute("userlist",userList);
        map.addAttribute("url","hope");
        map.addAttribute("number",1);
        map.addAttribute("switch","0");
        return "hope1";
    }

运行效果

SpringBoot(十六):Thymeleaf使用

源码

https://github.com/java-aodeng/hope

学习笔记,仅供参考


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ANTLR 4权威指南

ANTLR 4权威指南

Terence Parr / 张博 / 机械工业出版社 / 2017-5-1 / 69元

ANTLR是一款强大的语法分析器生成工具,可用于读取、处理、执行和翻译结构化的文本或二进制文件。它被广泛应用于学术领域和工业生产实践,是众多语言、工具和框架的基石。Twitter搜索使用ANTLR进行语法分析,每天处理超过20亿次查询;Hadoop生态系统中的Hive、Pig、数据仓库和分析系统所使用的语言都用到了ANTLR;Lex Machina将ANTLR用于分析法律文本;Oracle公司在S......一起来看看 《ANTLR 4权威指南》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具