栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Thymeleaf使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Thymeleaf使用

使用步骤: 1.导入依赖

    org.springframework.boot
    spring-boot-starter-thymeleaf

2.创建.html文件

在中添加xmlns:th=“http://www.thymeleaf.org”,如下


3.语法

th:text="${变量}" 改变文本的值

变量通过controller中model.addAttribute(“msg”,“atguigu”);发送

哈哈

th:href=“${变量}”; //跳转到该页面

th:href=“@{变量}”; //在默认途径后面加上此 路径

路径中添加参数

书名:
    [[${Product?.name}]]
售价:¥[[${Product?.price}]]

去百度 去百度

th:src



获取request中的值**:[[${变量}]]

如 order为Controller中request.setAttribute(“order”,order)中的值

[[${order.id}]]
[[${order.receiverName }]]
[[${order.ordertime}]]
[[${order.paystate==0?"未支付":"已支付"}]]

th:value

th:value="${session?.user?.username}"

th:each 循环

[[${vs.count}]] [[${entry.key.name }]] [[${entry.key.price }]] [[${entry.key.category}]] [[${entry.value}]] [[${entry.key.price*entry.value}]]

th:if 判断

  • th:style设置背景

    th:
    

    th:onclick

    传递参数

    如:传递三个参数

    
    

    前提是:

    			
    
    onclick="javascript:return cart_del()">X
    

    获取session中的值:[[${session.变量}]]

    user为Controller中session.setAttribute(“user”,user)中的值;

    如果session中的变量不存在,则会报错,可使用[[${session?.变量}]]进行判断,如果变量不存在,则不显示

    你好,[[${session?.user?.username}]]!欢迎您来到网上书城结算中心

    th:insert 引入其他html

    client/head为其他静态资源的相对路径

    #divhead表示client/head中id为divhead的部分

    注意:如果导入的client/head::#divhead中引入了其他的css或者js文件需要将css/js等静态资源包含在div中才可生效

    th:incline ->script标签中使用thymeleaf语法

    
    

    th:checked

     男
           
    
    springboot集成thymeleaf

    springboot集成thymeleaf的注意事项

    如果controller中使用return “/admin/login/home.html”,在打包成jar包之后会显示找不到templates下的静态资源

    正确写法:将前面的"/"去掉。

    @GetMapping("/admin/home.html")
    public String home(){
        return "admin/login/home.html";
    }
    

    总结:由于对于thymeleaf完全不熟悉,在使用过程中需要了好多坑。而且thymeleaf对于语法的要求比较严格,稍微写错可能就会报错,虽然springboot官方推荐使用thymeleaf,但是thymeleaf的语法比较难记。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/749033.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号