首頁 > Java > java教程 > 主體

SpringBoot之Thymeleaf模板引擎實例分析

WBOY
發布: 2023-05-12 17:28:06
轉載
1307 人瀏覽過

Jsp是最早的模板技術,用來處理視圖層的,用來做資料顯示的模板

SpringBoot之Thymeleaf模板引擎實例分析

B S結構:

B:瀏覽器:用來顯示數據,發送請求,沒有處理能力

發送一個請求,訪問a.jsp,a.jsp在伺服器端變成Servlet,在將輸出的數據回傳給瀏覽器,瀏覽器就可以看到結果數據,jsp最後翻譯過來也是個html頁面

模板技術你就可以把它們當成字串的替換,比如說:這裡{data}這裡有一個字串,你把它換成固定值其他值,但是這個替換有一些附加的功能,透過模板技術​​處理視圖層的內容

SpringBoot之Thymeleaf模板引擎實例分析

第一個例子:

SpringBoot之Thymeleaf模板引擎實例分析

#pom.xml:Thymeleaf依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.bjpowernode</groupId>
    <artifactId>027-thymeleaf-first</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--模板引擎起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <!--web起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
登入後複製

建立Controller控制器:HelloThymeleafController:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(HttpServletRequest request){
        //添加数据到request作用域,模板引擎可以从request中获取数据
        request.setAttribute("data","欢迎使用Thymeleaf模板引擎");
        //指定视图 模板引擎使用的页面(html)
        //逻辑名称
        return "hello";
    }
}
登入後複製

templates:用來放模板用到的視圖檔的,模板引擎用到的模板到放在了template目錄之下:

創建hello.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h4>使用Thymeleaf的例子</h4>
   <!--使用模板th:text=""获取数据-->
   <p th:text="${data}">想显示数据</p>
</body>
</html>
登入後複製

運行主啟動類別Application:

package com.bjpowernode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
登入後複製

可以在hello .html加入:未解決在標籤中th爆紅,和寫的時候沒有提示訊息

xmlns:th="http://www.thymeleaf.org" 在標籤中,再次寫th的時候就有提示記錄了

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h4>使用Thymeleaf的例子</h4>
   <!--使用模板th:text="${data}"获取后端request的作用域中的数据,把data数据替换文本,text表示取数据-->
   <p th:text="${data}">想显示数据</p>
   <p th:text="${data}">显示</p>
</body>
</html>
登入後複製

SpringBoot之Thymeleaf模板引擎實例分析

使用Model:

在Controller:

package com.bjpowernode.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class HelloThymeleafController {
    @RequestMapping("/hello")
    public String helloThymeleaf(Model model, HttpServletRequest request){
        //添加数据到request作用域,模板引擎可以从request中获取数据
        request.setAttribute("data","欢迎使用Thymeleaf模板引擎");
        //使用model和request作用域是一样的 实际上model中的数据就是放到request作用域中的
        model.addAttribute("mydata","model中的数据");
        //指定视图 模板引擎使用的页面(html)
        //逻辑名称
        return "hello";
    }
}
登入後複製

hello.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>hello html</title>
</head>
<body>
   <h4>使用Thymeleaf的例子</h4>
   <!--使用模板th:text="${data}"获取后端request的作用域中的数据,把data数据替换文本,text表示取数据-->
   <p th:text="${data}">想显示数据</p>
   <p th:text="${mydata}">显示</p>
</body>
</html>
登入後複製

SpringBoot之Thymeleaf模板引擎實例分析

speingboot設定檔application.properties:模板引擎的常用相關設置,基本不用設定都是預設的:

#在開發階段,關閉範本發緩存,讓修改立即生效設定為true時,就是使用模板緩存,當第二次訪問的時候,使用內存中的數據,就不在解析模板了
spring.thymeleaf.cache=false
#編碼格式
spring.thymeleaf.encoding=UTF-8
#範本的類型(預設是html,範本是html檔案它不僅支援網頁做範本還支援其他類別的)
spring.thymeleaf.model= HTML
#範本的前綴:預設是類別路徑的classpath:/templates目錄下
spring.thymeleaf.prefix=classpath:/templates/
#後綴
spring.thymeleaf.suffix=.html

以上是SpringBoot之Thymeleaf模板引擎實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!