濟(jì)南專業(yè)做網(wǎng)站的公司哪家好信息流優(yōu)化師證書
官網(wǎng)
https://www.thymeleaf.org/
介紹
Thymeleaf 是一個(gè)適用于 Web 和獨(dú)立環(huán)境的現(xiàn)代服務(wù)器端 Java 模板引擎。
模板引擎:為了使用戶界面和業(yè)務(wù)數(shù)據(jù)分離而產(chǎn)生的,它可以生成特定格式的文檔,用于網(wǎng)站的模板引擎會(huì)生成一個(gè)標(biāo)準(zhǔn)的 html 文檔。
模板的作用:做好一個(gè)模板后,套入對(duì)應(yīng)的位置的數(shù)據(jù),最終以 html 的格式進(jìn)行展示。
模板引擎的特點(diǎn):提高頁面、代碼的復(fù)用性。
官網(wǎng)文檔
https://www.thymeleaf.org/doc/tutorials/3.1/thymeleafspring.html
依賴
pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置
thymeleaf 的默認(rèn)配置,可以不改。
application.yml
spring:application:name: systemthymeleaf:prefix: classpath:/templates/ #前綴,默認(rèn)為classpath:/templates/ suffix: .html #后綴,默認(rèn)為.html
接口
在 controller 目錄下,新建 UserController 類。
此處返回的是 user 頁面。
UserController.java
package com.lm.system.controller;import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;/*** @Author: DuHaoLin* @Date: 2024/7/26*/
@Controller
public class UserController {@GetMapping("user")public String user(Model model) {model.addAttribute("name", "Tom");model.addAttribute("age", 18);return "user";}}
返回頁面
在 resource 目錄下,新建 thymeleaf 默認(rèn)獲取的 templates 目錄。
在 templates 目錄下,新建 user.html 文件。
user.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Thymeleaf</title>
</head>
<body><span>姓名:</span><span th:text="${name}"></span><br /><span>年齡:</span><span th:text="${age}"></span>
</body>
</html>
效果圖展示