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

手摸手3分钟教你整合springboot和thymeleaf

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

手摸手3分钟教你整合springboot和thymeleaf

github仓库

https://github.com/itwell/springboot-thymeleaf (原码下载)

引入 Thymeleaf 依赖
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
创建模板文件



    Thymeleaf demo


description字段值为:

这里显示的是 description 字段内容

controller
package com.itwell.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;

@Controller
public class ThymeleafController {
    @GetMapping("/thymeleaf")
    public String hello(HttpServletRequest request, @RequestParam(value = "description", required = false, defaultValue = "springboot-thymeleaf") String description) {
        request.setAttribute("description", description);
        return "thymeleaf";
    }
}
关闭缓存
# thymeleaf模板缓存 开发环境下推荐打开 实时看到页面的变化
spring.thymeleaf.cache=false
#{} 消息表达式 在resource文件夹下创建 i18n文件夹 创建不同语言的配置文件

hello.properties
hello_en_US.properties
hello_zh_CN.properties

application.properties配置文件
#i18n国际化
spring.messages.basename = i18n.hello
模板文件测试

...

*{} 星号表达式 controller
Book book = new Book();
book.setTitle("三国演义");
request.setAttribute("book", book);
html
    

*{}取的是${}里面的数据
${}取的是上下文里面的数据

@{} 链接表达式

相对的链接表达式


服务器相对


协议相对


绝对链接表达式


th:insert th:replace

新建一个footer.html




    
    Title


    
        © 2020 
    



在thymeleaf.html 引入 footer.html


字面量

文本

...

数字

...

数字计算

...

布尔

null

th:class 条件运算符

三元表达式

无操作

no user authenticated

设置属性值

内置属性

自定义属性

固定布尔值



迭代器
        ArrayList books = new ArrayList<>();
        Book book1 = new Book();
        book1.setTitle("水浒传");
        Book book2 = new Book();
        book2.setTitle("西游记");
        Book book3 = new Book();
        book3.setTitle("红楼梦");
        Book book4 = new Book();
        book4.setTitle("三国演义");
        books.add(book1);
        books.add(book2);
        books.add(book3);
        books.add(book4);

遍历

  • default
  • 状态变量
    index count size current even/odd first last

        
    ... default
    条件语句th:if th:unless th:switch th:case

    th:if

    如果 boolean 是 true ,本句话就会显示

    th:unless

    如果 unless 是 false ,本句话就会显示

    th:switch

        

    显示a

    显示b

    显示c

    引用片段th:fragment

    定义片段

    
        
            © 2020 
        
    
    

    引用片段

     
    不使用th:fragment 
    

    定义片段

    
        
            片段
        
    
    

    引用片段

     
    th:insert th:replace th:include 
    

    th:insert 将被插入内容连同标签 放入到主页面当中.
    th:replace 将被插入内容连同标签 放入到主页面当中 但是会替换掉主页面的标签
    th:include 类似于th:insert ,将被插入内容不包含标签 放入到主页面当中(3.x版本后,不在推荐使用)

    属性优先级
    OrderFeatureAttributes
    1Fragment inclusionth:insert th:replace
    2Fragment iterationth:each
    3Conditional evaluationth:if th:unless th:switch th:case
    4Local variable definitionth:object th:with
    5General attribute modificationth:attr th:attrprepend th:attrappend
    6Specific attribute modificationth:value th:href th:src ...
    7Text (tag body modification)th:text th:utext
    8Fragment specificationth:fragment
    9Fragment removalth:remove
    内联表达式

    [[…]]或[(…)]分别对应于th:text和th:utext

    [[…]]不会解析html标签
    [(…)]会解析html标签

    controller

    request.setAttribute("msg", "great!");
    

    html

    The message is "[(${msg})]"

    The message is "[[${msg}]]"

    表达式基本对象

    #ctx :上下文对象。

    ${#ctx.request}
    ${#ctx.response}
    ${#ctx.session}
    ${#ctx.servletContext}
    

    希望对热爱编程的小伙伴有所帮助,博学谷-IT在线教育培训机构-传智教育旗下IT在线学习平台

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

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

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