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

SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎

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

SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎

SpringBoot 学习笔记 Part12 1. thymeleaf简介

SpringBoot默认不支持 JSP,需要引入第三方模板引擎技术实现页面渲染。

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, Javascript, CSS and even plain text.

即 thymeleaf 是现代化、服务端的Java模板引擎。


2. 基本语法

表达式

表达式名语法作用
变量取值${…}获取请求域、session域、对象等值
选择变量*{…}获取上下文对象值
消息#{…}获取国际化等值
链接@{…}生成链接
片段表达式~{…}jsp:include 作用,引入公共页面片段
行内表达式[[…]]HTML 文本中嵌套表达式

字面量

文本值:‘one text’ , ‘another one’

数字:0 , 34 , 3.0 , 12.3

布尔值:true , false

空值:null

变量:one,two

文本操作

字符串拼接: +

变量替换: |The name is ${name}|

数字运算

运算符: + , - , * , / , %

布尔运算

运算符: and , or

一元运算: ! , not

比较运算

比较: > , < , >= , <= ( gt , lt , ge , le )

等式: == , != ( eq , ne )

条件运算

If-then: (if) ?(then)

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

Default: (value) ? :(defaultvalue)

特殊操作

无操作: _


3. 标签th:attr

th:attr 用来设置属性值。

设置单个值:

设置多个值:


SpringBoot学习笔记【part12】Web开发——Thymeleaf模板引擎

以上两个的代替写法:


所有h5兼容的标签写法可参考官方文档:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#setting-value-to-specific-attributes


4. 迭代

foreach:

迭代普通类型:


        Onions
        2.41
        yes

迭代对象:


  Onions
  2.41
  yes


5. 条件运算

if:

view

switch:

User is an administrator

User is a manager

User is some other thing


6. 属性优先级

所有Thymeleaf属性都有一个数字优先级,以建立他们在标签中的顺序执行。这个顺序是:

OrderFeatureAttributes
1Fragment inclusionth:include 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, etc.
7Text (tag body modification)th:text th:utext
8Fragment specificationth:fragment
9Fragment removalth:remove

7. thymeleaf的使用

首先,我们需要引入thymeleaf的starter场景启动器。


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

我们可以在 springboot 中找到 thymeleaf 的自动配置类,这说明springboot已经为我们做好了默认配置的工作。

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnClass({ TemplateMode.class, SpringTemplateEngine.class })
@AutoConfigureAfter({ WebMvcAutoConfiguration.class, WebFluxAutoConfiguration.class })
public class ThymeleafAutoConfiguration { }

查看 springboot 的 thymeleaf 的自动配置类,在自动配置类里我们可以找到一些已经配好的重要组件,如spring模板引擎 SpringTemplateEngine、thymeleaf 的视图解析器 ThymeleafViewResolver 等。并且我们发现所有thymeleaf的配置值都在 ThymeleafProperties 类里,这个是存储配置信息的配置类,之前学习过。

public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";  //xxx.html

ThymeleafProperties 类中这两个重要属性表明了我们开发的页面路径和映射规则,因此我们不需要别的配置,直接进行开发就行了。

注:在html页面开发中为了更方便地使用thymeleaf标签属性,应在标签中引入thymeleaf的命名空间 xmlns:th=“http://www.thymeleaf.org”,如下:




    
    Title






8. 开发小案例

h5中编写如下:




    
    Title



Hello H5 !

去百度

Controller中编写如下:

@Controller
public class thymeleafController {

    @GetMapping("/thymeleaf")
    public String gotoThymeleaf(Model model){
        model.addAttribute("msg","Hello Thymelef !");
        model.addAttribute("link","http://www.baidu.com");
        return "success";
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726283.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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