国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot整合模板引擎Thymeleaf(4)

這篇具有很好參考價值的文章主要介紹了SpringBoot整合模板引擎Thymeleaf(4)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報違法"按鈕提交疑問。


版權(quán)聲明

  • 本文原創(chuàng)作者:谷哥的小弟
  • 作者博客地址:http://blog.csdn.net/lfdfhl

概述

在之前的教程中,我們介紹了Thymeleaf的基礎(chǔ)知識。在此,以案例形式詳細(xì)介紹Thymeleaf的基本使用。
SpringBoot整合模板引擎Thymeleaf(4)

項目結(jié)構(gòu)

要點(diǎn)概述:

  • 1、在static下創(chuàng)建css文件夾用于存放css文件
  • 2、在static下創(chuàng)建img文件夾用于存放圖片文件
    SpringBoot整合模板引擎Thymeleaf(4)

依賴文件

請在pom.xml文件中添加如下依賴。

<?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.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cn</groupId>
    <artifactId>SpringBootThymeleaf01</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SpringBootThymeleaf01</name>
    <description>SpringBootThymeleaf01</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <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>

配置文件

請在application.properties文件添加以下配置。

# 緩存設(shè)置。開發(fā)中設(shè)置為false,線上時設(shè)置為true
spring.thymeleaf.cache=false
# 模板的編碼方式
spring.thymeleaf.encoding=UTF-8
# 模式
spring.thymeleaf.mode=HTML5
# 模板頁面存放路徑
spring.thymeleat.prefix=classpath:/resources/templates/
# 模板頁面名稱后綴
spring.thymeleaf.suffix=.html

LoginController

package com.cn.springbootthymeleaf02.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Calendar;

/**
 * 本文作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
@Controller
@RequestMapping("/LoginController")
public class LoginController {
    @RequestMapping("/toLogin")
    public String toLogin(Model model){
        int year = Calendar.getInstance().get(Calendar.YEAR);
        model.addAttribute("year", year);
        //跳轉(zhuǎn)至login.html
        return "login";
    }
}

login.html

在templates中創(chuàng)建login.html頁面。

<!DOCTYPE html>
<!--  引入Thymeleaf命名空間 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1,shrink-to-fit=no">
  <title>用戶登錄界面</title>
  <!--  通過th:href引入css -->
  <link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
  <link th:href="@{/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<!--  用戶登錄表單 -->
<form class="form-signin">
  <!--  通過th:src引入圖片 -->
  <img class="mb-4" th:src="@{/img/icon.jpg}" width="72" height="72"/>
  <h1 class="h3 mb-3 font-weight-normal">請登錄</h1>
  <input type="text" class="form-control" th:placeholder="用戶"/>
  <input type="password" class="form-control" th:placeholder="密碼"/>
  <div class="checkbox mb-3">
    <label>
      <input type="checkbox" value="remember-me"/> 記住我
    </label>
  </div>
  <button class="btn btn-lg btn-primary btn-block" type="submit">登錄</button>
  <!--  通過th:text顯示后臺傳遞的數(shù)據(jù)year -->
  <p class="mt-5 mb-3 text-muted">? <span th:text="${year}">2030</span>-<span th:text="${year}+1">2040</span></p>
</form>
</body>
</html>

SpringBootThymeleaf02Application

SpringBoot整合模板引擎Thymeleaf(4)

測試

測試地址:http://localhost:8080/LoginController/toLogin

SpringBoot整合模板引擎Thymeleaf(4)

從此結(jié)果可以看出:html頁面顯示了后臺傳遞至前端的數(shù)據(jù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-495735.html

到了這里,關(guān)于SpringBoot整合模板引擎Thymeleaf(4)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點(diǎn)擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【SpringBoot學(xué)習(xí)筆記】04. Thymeleaf模板引擎

    【SpringBoot學(xué)習(xí)筆記】04. Thymeleaf模板引擎

    ?所有的html元素都可以被thymeleaf替換接管? th:元素名 templates下的只能通過Controller來跳轉(zhuǎn),templates前后端分離,需要模板引擎thymeleaf支持 ?? 模板引擎的作用就是我們來寫一個頁面模板,比如有些值呢,是動態(tài)的,我們寫一些表達(dá)式。而這些值,從哪來呢,就是我們在后臺封

    2024年02月13日
    瀏覽(21)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解①

    SpringBoot自帶模板引擎Thymeleaf使用詳解①

    目錄 前言 一、SpringBoot靜態(tài)資源相關(guān)目錄 二、變量輸出 2.1 在templates目錄下創(chuàng)建視圖index.html 2.2 創(chuàng)建對應(yīng)的Controller 2.3 在視圖展示model中的值 三、操作字符串和時間 3.1 操作字符串 3.2 操作時間 ????????Thymeleaf是一款用于渲染XML/HTML5內(nèi)容的模板引擎,類似JSP。它可以輕易的

    2024年02月08日
    瀏覽(20)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解②

    SpringBoot自帶模板引擎Thymeleaf使用詳解②

    目錄 一、條件判斷和迭代遍歷 1.1 條件判斷 2.2 迭代遍歷 二、獲取域中的數(shù)據(jù)和URL寫法 2.1 獲取域中的數(shù)據(jù) 2.2 URL寫法 三、相關(guān)配置 語法 作用 th:if 條件判斷 準(zhǔn)備數(shù)據(jù) model.addAttribute(\\\"sex\\\",\\\"男\(zhòng)\\"); 使用實例 div ??? span th:if=\\\"${sex}==\\\'女\\\'\\\"這是女生/span ??? span th:if=\\\"${sex}==\\\'男\(zhòng)\\'\\\"這是男

    2024年02月08日
    瀏覽(17)
  • 15 springboot項目——thymeleaf語法與關(guān)閉模板引擎

    15 springboot項目——thymeleaf語法與關(guān)閉模板引擎

    ? ? ? ? 在html文件中,有些是需要使用本地的css樣式,使用thymeleaf語法加載: ? ? ? ? 首先對head標(biāo)簽上面的html標(biāo)簽進(jìn)行更改: ? ? ? ? 其次,導(dǎo)入thymeleaf依賴: ? ? ? ? 接著,使用thymeleaf語法: ? ? ? ? 碰到href或者src后邊與靜態(tài)資源有關(guān)的的本地路徑要進(jìn)行修改,把要

    2024年02月14日
    瀏覽(18)
  • 【Spring Boot】Thymeleaf模板引擎 — Thymeleaf入門

    主要介紹什么是Thymeleaf以及Spring Boot如何集成使用Thymeleaf模板,最后介紹Spring Boot支持的Thymeleaf的一些常用的配置參數(shù)。 Thymeleaf是一款非常優(yōu)秀的服務(wù)器端頁面模板引擎,適用于Web和獨(dú)立環(huán)境,具有豐富的標(biāo)簽語言和函數(shù),能夠處理HTML、XML、JavaScript甚至文本。 Thymeleaf相較于

    2024年02月05日
    瀏覽(24)
  • thymeleaf模板引擎

    thymeleaf模板引擎

    ThymeleafProperties 配置類 1.默認(rèn)編碼 2.前綴 3.后綴 相當(dāng)于視圖解析器? ? 這是學(xué)SpringBoot的必經(jīng)之路,非常重要?。。。ǔ悄闶菍W(xué)前端的) ? 只改了前端代碼點(diǎn)一下這個就可以刷新? ? 傳值過來了? th:text=\\\"${msg}\\\"爆紅,但是可以顯示,F(xiàn)ile-Settings-Editor-Inspection ?取消“Expression

    2024年02月14日
    瀏覽(19)
  • 【Spring Boot】Thymeleaf模板引擎 — 表達(dá)式的語法

    模板的主要作用是將后臺返回的數(shù)據(jù)渲染到HTML中。那么Thymeleaf是如何解析后臺數(shù)據(jù)的呢?接下來從變量、方法、條件判斷、循環(huán)、運(yùn)算(邏輯運(yùn)算、布爾運(yùn)算、比較運(yùn)算、條件運(yùn)算)方面學(xué)習(xí)Thymeleaf表達(dá)式支持的語法。 (1)文本賦值 賦值就是通過${}標(biāo)簽將后臺返回的數(shù)據(jù)替

    2024年02月14日
    瀏覽(24)
  • SpringBoot整合thymeleaf

    SpringBoot整合thymeleaf

    JavaEE領(lǐng)域有幾種常用的模板引擎: Jsp, Thymeleaf, Freemarker, Velocity等.對于前端頁面渲染效率來說 JSP 其實還是最快的, Velocity次之.Thymeleaf雖然渲染效率不是很快,但語法比較輕巧. Thymeleaf 支持html5標(biāo)準(zhǔn), Thymeleaf頁面無需部署到servlet開發(fā)到服務(wù)器上,以 .html 后綴結(jié)尾,可直接通過瀏覽器就

    2024年02月10日
    瀏覽(18)
  • 40、Thymeleaf的自動配置和基本語法、springboot 整合 Thymeleaf

    40、Thymeleaf的自動配置和基本語法、springboot 整合 Thymeleaf

    要使用這個 Thymeleaf ,需要在頁面引入這個命名空間。 pom文件也需要加入thymeleaf的依賴 html 是根元素,把這個th命名空間引入進(jìn)去,表示這整個html頁面都能使用這個thymeleaf語法,都可以使用這個 th 前綴。 代碼演示: ${objList} 就是要遍歷的集合 obj 就是集合中的每個元素 iter

    2024年02月10日
    瀏覽(19)
  • SpringBoot 整合Thymeleaf教程及使用

    Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 內(nèi)容的模板引擎。它與 JSP,Velocity,F(xiàn)reeMaker 等模板引擎類似,也可以輕易地與 Spring MVC 等 Web 框架集成。與其它模板引擎相比,Thymeleaf 最大的特點(diǎn)是,即使不啟動 Web 應(yīng)用,也可以直接在瀏覽器中打開并正確顯示模板頁面 。 目錄 一、整合

    2024年02月06日
    瀏覽(19)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包