SpringBoot(十六):Thymeleaf使用

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

内容简介:个人博客: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

学习笔记,仅供参考


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

查看所有标签

猜你喜欢:

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

Go Web编程

Go Web编程

谢孟军 / 电子工业出版社 / 2013-6-1 / 65.00元

《Go Web编程》介绍如何用Go语言进行Web应用的开发,将Go语言的特性与Web开发实战组合到一起,帮读者成功地构建跨平台的应用程序,节省Go语言开发Web的宝贵时间。有了这些针对真实问题的解决方案放在手边,大多数编程难题都会迎刃而解。 在《Go Web编程》中,读者可以更加方便地找到各种编程问题的解决方案,内容涵盖文本处理、表单处理、Session管理、数据库交互、加/解密、国际化和标......一起来看看 《Go Web编程》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换