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

Thymeleaf模板引擎

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

Thymeleaf模板引擎

文章目录
  • 狂神说
  • 1. 模板引擎
  • 2. 引入Thymeleaf
  • 3. Thymeleaf分析
  • 4. Thymeleaf 简单的使用 -- 获取数据
  • 5. Thymeleaf的使用语法

狂神说

https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247483807&idx=1&sn=7e1d5df51cdeb046eb37dec7701af47b&scene=19#wechat_redirect

1. 模板引擎

前端交给我们的页面,是html页面。如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显示,及交互等。

SpringBoot项目首先是以jar的方式,不是war,第二,我们用的还是嵌入式的Tomcat,所以,Spring Boot默认是不支持jsp的。

SpringBoot推荐你可以来使用模板引擎:Thymeleaf
模板引擎,我们其实大家听到很多,其实jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf,

2. 引入Thymeleaf

对于springboot来说,什么事情不都是一个start的事情,我们去在项目中引入一下。给大家三个网址:

  • Thymeleaf 官网:https://www.thymeleaf.org/

  • Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf

  • Spring官方文档:找到我们对应的版本 https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
    找到对应的pom依赖:

    
        org.springframework.boot
        spring-boot-starter-thymeleaf
    
    
3. Thymeleaf分析

我们去找一下Thymeleaf的自动配置类:ThymeleafProperties

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";
    private Charset encoding;
}

我们可以在其中看到默认的前缀和后缀!

我们只需要把我们的html页面放在类路径下的templates下,thymeleaf就可以帮我们自动渲染了。

使用thymeleaf什么都不需要配置,只需要将他放在指定的文件夹下即可!

4. Thymeleaf 简单的使用 – 获取数据

创建请求传输数据

@RequestMapping("/t1")
public String test1(Model model){
    //存入数据
    model.addAttribute("msg","Hello,Thymeleaf");
    //classpath:/templates/test.html
    return "test";
}

使用thymeleaf – 在html文件中导入命名空间的约束

 xmlns:th="http://www.thymeleaf.org"

前端页面接收数据




    
    


测试页面

5. Thymeleaf的使用语法

1、可以使用任意的 th:attr 来替换Html中原生属性的值!

[[${user}]]

2、表达式

  • Simple expressions:(表达式语法)

    • Variable expressions: ${…}:获取变量值;OGNL;

    • Selection Variable expressions: *{…}:选择表达式:和${}在功能上是一样;

    • Message expressions: #{…}:获取国际化内容

    • link URL expressions: @{…}:定义URL;

    • Fragment expressions: ~{…}:片段引用表达式

  • Literals(字面量)

    • Text literals: ‘one text’ , ‘Another one!’ ,…

    • Number literals: 0 , 34 , 3.0 , 12.3 ,…

    • Boolean literals: true , false

    • Null literal: null

    • Literal tokens: one , sometext , main ,…

  • Text operations:(文本操作)

    • String concatenation: +

    • Literal substitutions: |The name is ${name}|

  • Arithmetic operations:(数学运算)

    • Binary operators: + , - , * , / , %

    • Minus sign (unary operator): -

  • Boolean operations:(布尔运算)

    • Binary operators: and , or

    • Boolean negation (unary operator): ! , not

  • Comparisons and equality:(比较运算)

    • Comparators: > , < , >= , <= ( gt , lt , ge , le )

    • Equality operators: == , != ( eq , ne )

  • Conditional operators:条件运算(三元运算符)

    • If-then: (if) ? (then)

    • If-then-else: (if) ? (then) : (else)

    • Default: (value) ?: (defaultvalue)

  • Special tokens:

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

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

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