二.Springboot+Thymeleaf 整合之前后端(文本)交互

二、Springboot+Thymeleaf 整合之前后端(文本)交互

一、配置环境

1.1 在上一节的pom.xml文件中引入thymeleaf 模板依赖:

   <!--thymeleaf模板-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-java8time</artifactId>
        </dependency>

1.2 在配置文件 application.yml 中 编写thymeleaf 的配置

spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    cache: false
    mode: HTML5
    servlet:
     content-type: text/html
    encoding: utf-8


二、前端显示后端传输的数据

1.变量传输方式

1.1 前端代码

编写index.html文件

注意 文件头部要加上 xmlns:th="http://www.thymeleaf.org"

th:text=" ${xxxx}" -------- thymeleaf 语法 获取文本信息
$ ----------- 取值符
${ xxxx} --------注入的形参 要与后端的一致,不然取不到值

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello word</title>
</head>
<body>
<p>大家好</p>

<!--其中,注释是通过alt+enter进行自动生成的,便于IDEA补全,如果不加,IDEA将会报错cannot reslove,-->
<!--/*@thymesVar id="name" type="Java.lang.String"*/-->
<p th:text=" ${name}"></p>
<!--/*@thymesVar id="year" type="java.lang.String"*/-->
<p th:text=" ${year}"></p>
<!--/*@thymesVar id="hobby" type="java.lang.String"*/-->
<p th:text
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值