美文网首页
SpringBoot入门(二)SpringBoot整合Thyme

SpringBoot入门(二)SpringBoot整合Thyme

作者: JesseaKylin | 来源:发表于2019-07-20 22:41 被阅读0次

1.1 模板引擎简介

  1. 模板引擎是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的文档,就是将模板文件和数据通过模板引擎生成一个HTML代码。
  2. 现在主流的Java引擎有:FreeMarker、JSP(springboot官方不推荐)、Thymeleaf

1.2 Thymeleaf模板引擎

  1. Thymeleaf 是 Web 和独立环境的现代服务器端 Java 模板引擎,能够处理HTML,XML,JavaScript,CSS 甚至纯文本。
  2. SpringBoot推荐使用的Thymeleaf,语法更简单,功能更强大。

2.1 SpringBoot整合Thymeleaf

  1. 新建一个SpringBoot项目,创建SpringBoot项目

  2. pom文件引入依赖


    在这里插入图片描述
  3. 在resources下的templates里新建一个html文件


    在这里插入图片描述
  4. 后台控制器


    在这里插入图片描述
  5. 运行项目


    在这里插入图片描述

3.1 代码

TestController.java.

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author junyh
 * @version 1.0
 * @date 2019/5/14 20:39
 */
@Controller
public class TestController {
    /**
     * 测试thymeleaf
     */
    @RequestMapping("testThymeleaf")
    public String testThymeleaf(Model model){
        model.addAttribute("name","张三");
        return "test";
    }

}

test.html.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
   <meta charset="UTF-8">
   <title>Title</title>
</head>
<body>
   <p th:text="${name}"></p>
</body>
</html>

相关文章

网友评论

      本文标题:SpringBoot入门(二)SpringBoot整合Thyme

      本文链接:https://www.haomeiwen.com/subject/nssplctx.html