前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >引入模板引擎thymeleaf

引入模板引擎thymeleaf

作者头像
桑鱼
发布2020-03-17 18:14:54
9700
发布2020-03-17 18:14:54
举报

1、pom.xml 注入模板引擎thymeleaf依赖,切换thymeleaf版本为3.0

代码语言:javascript
复制
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<!--        布局功能的支持程序thymeleaf3主程序 layout2以上版本-->
<!--        thymeleaf2 layoout1-->
    <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、thymeleaf语法

代码语言:javascript
复制
@Controller
public class HelloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }
    @RequestMapping("/success")
    public String success(){
        // classpath:/templates/success.html
        return "success";
    }
}

给sucess.html中设置内容

代码语言:javascript
复制
@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World";
    }

    @RequestMapping("/success")
    public String success(Map<String,Object> map){
        // classpath:/templates/success.html
        map.put("hello","你好");
        return "success";
    }
}
代码语言:javascript
复制
<!DOCTYPE html>
<!--导入thymeleaf语法提示-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>success!!!!!!!</h1>
<!--th:text 改变当前元素里面的文本内容-->
<!--th:任意html属性;来替换原生属性的值-->
<div th:text="${hello}"></div>
</body>
</html>

3、th的语法

代码语言:javascript
复制
th:insert 片段包含
th:replace 片段包含
th:each 遍历
th:if 判断
th:unless 条件判断
th:switch 条件判断
th:case 条件判断
th:object 声明变量
th:with 声明变量
th:attr 任意属性修改支持prepend,append
th:attrprepend 任意属性修改支持prepend,append
th:attrappend 任意属性修改支持prepend,append
th:value 修改指定属性默认值
th:href 修改指定属性默认值
th:src 修改指定属性默认值
th:text 修改标签体内容,转义特殊字符
th.utext 修改标签体内容,不转义特殊字符
th:fragment 声明片段
th:remove 声明片段
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档