spring-boot中使用 Thymeleaf模板引擎

第一步首先需要在pom.xml中导入两个依赖

spring管理版本号,所以不需要声明版本号,下面那个nekohtml是可以使用不那么严格的html语法

<!--        Thymeleaf模板引擎需要的两个依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
        </dependency>
第二步在application.yml中配置相关配置
spring:
  application:
    name: spring-boot
  thymeleaf:
    cache: false  #开发时关闭缓存,否则无法看到实时静态页面
    mode: HTML  #用非严格的HTML
    encoding: UTF-8
    servlet:
      content-type: text/html

server:
  port: 8081

user:
  name: 老王
  age: 20
  address:
    city: 广州
#     日志的配置  集成的框架为 logback 与log4j的作者为一个人
logging:
  file:
#    存放日志文件的目录
    name: ./logs/spring-boot-log

就是thymeleaf那一块几行配置,是配置在spring下

第三步创建一个html文件
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.js"></script>
</head>
<body>
 <!--/*@thymesVar id="name" type="com.smart.springboot.controller.TestThymeleaf"*/-->
 <span th:text="${name}">李四</span>
</body>
</html>
第四步创建一个controller
package com.smart.springboot.controller;

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

/**
 * @author Administrator
 */
@Controller
public class TestThymeleaf {
    @GetMapping("/index")
    public String testThy(Model model) {
        model.addAttribute("name", "张三");
        return "index";
    }
}

controller那里是传值去前端,前端使用特定的语法接收.

不启动服务器时的静态页面

在这里插入图片描述

启动是前端接收到的动态页面

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值