大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專(zhuān)注Java教育14年 全國(guó)咨詢(xún)/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) hot資訊 Thymeleaf模板引擎的使用

Thymeleaf模板引擎的使用

更新時(shí)間:2021-06-10 16:14:40 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽1229次

在早期開(kāi)發(fā)的時(shí)候,完成的都是靜態(tài)頁(yè)面也就是html頁(yè)面,隨著時(shí)間軸的發(fā)展,慢慢的引入了jsp頁(yè)面,當(dāng)在后端服務(wù)查詢(xún)到數(shù)據(jù)之后可以轉(zhuǎn)發(fā)到j(luò)sp頁(yè)面,可以輕松的使用jsp頁(yè)面來(lái)實(shí)現(xiàn)數(shù)據(jù)的顯示及交互,jsp有非常強(qiáng)大的功能,但是,在使用springboot的時(shí)候,整個(gè)項(xiàng)目是以jar包的方式運(yùn)行而不是war包,而且還嵌入了tomcat容器,因此,在默認(rèn)情況下是不支持jsp頁(yè)面的。如果直接以純靜態(tài)頁(yè)面的方式會(huì)給開(kāi)發(fā)帶來(lái)很大的麻煩,springboot推薦使用模板引擎。

?模板引擎有很多種,jsp,freemarker,thymeleaf,模板引擎的作用就是方便編寫(xiě)一個(gè)頁(yè)面模板,比如有些值呢,是動(dòng)態(tài)的,寫(xiě)一些表達(dá)式。而這些值,從哪來(lái)呢,組裝一些數(shù)據(jù),把這些數(shù)據(jù)找到。然后把這個(gè)模板和這個(gè)數(shù)據(jù)交給模板引擎,模板引擎按照這個(gè)數(shù)據(jù)把這表達(dá)式解析、填充到指定的位置,然后把這個(gè)數(shù)據(jù)最終生成一個(gè)我們想要的內(nèi)容給寫(xiě)出去,不管是jsp還是其他模板引擎,都是這個(gè)思想。只不過(guò)不同的模板引擎語(yǔ)法不同而已,下面重點(diǎn)學(xué)習(xí)下springboot推薦使用的thymeleaf模板引擎,語(yǔ)法簡(jiǎn)單且功能強(qiáng)大

thymeleaf模板引擎

1、thymeleaf的介紹

官網(wǎng)地址:https://www.thymeleaf.org/

thymeleaf在github的地址:https://github.com/thymeleaf/thymeleaf

中文網(wǎng)站:https://raledong.gitbooks.io/using-thymeleaf/content/

導(dǎo)入依賴(lài):

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

在springboot中有專(zhuān)門(mén)的thymeleaf配置類(lèi):ThymeleafProperties

@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
	private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
	public static final String DEFAULT_PREFIX = "classpath:/templates/";
	public static final String DEFAULT_SUFFIX = ".html";
	/**
	 * Whether to check that the template exists before rendering it.
	 */
	private boolean checkTemplate = true;
	/**
	 * Whether to check that the templates location exists.
	 */
	private boolean checkTemplateLocation = true;
	/**
	 * Prefix that gets prepended to view names when building a URL.
	 */
	private String prefix = DEFAULT_PREFIX;
	/**
	 * Suffix that gets appended to view names when building a URL.
	 */
	private String suffix = DEFAULT_SUFFIX;
	/**
	 * Template mode to be applied to templates. See also Thymeleaf's TemplateMode enum.
	 */
	private String mode = "HTML";
	/**
	 * Template files encoding.
	 */
	private Charset encoding = DEFAULT_ENCODING;
	/**
	 * Whether to enable template caching.
	 */
	private boolean cache = true;

2、thymeleaf使用模板

在java代碼中寫(xiě)入如下代碼:

    @RequestMapping("/hello")
    public String hello(Model model){
        model.addAttribute("msg","Hello");
        //classpath:/templates/hello.html
        return "hello";
    }

html頁(yè)面中寫(xiě)入如下代碼:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h1>Hello</h1>
<div th:text="${msg}"></div>
</body>
</html>

3、thymeleaf的表達(dá)式語(yǔ)法

Simple expressions:
	Variable Expressions: ${...}
	Selection Variable Expressions: *{...}
	Message Expressions: #{...}
	Link URL Expressions: @{...}
	Fragment Expressions: ~{...}
Literals
	Text literals: 'one text', 'Another one!',…
	Number literals: 0, 34, 3.0, 12.3,…
	Boolean literals: true, false
	Null literal: null
	Literal tokens: one, sometext, main,…
Text operations:
	String concatenation: +
	Literal substitutions: |The name is ${name}|
Arithmetic operations:
	Binary operators: +, -, *, /, %
	Minus sign (unary operator): -
	Boolean operations:
	Binary operators: and, or
	Boolean negation (unary operator): !, not
Comparisons and equality:
	Comparators: >, <, >=, <= (gt, lt, ge, le)
	Equality operators: ==, != (eq, ne)
	Conditional operators:
	If-then: (if) ? (then)
	If-then-else: (if) ? (then) : (else)
	Default: (value) ?: (defaultvalue)
Special tokens:
	No-Operation: _

4、thymeleaf實(shí)例演示

1、th的常用屬性值

?一、th:text:設(shè)置當(dāng)前元素的文本內(nèi)容,相同功能的還有th:utext,兩者的區(qū)別在于前者不會(huì)轉(zhuǎn)義html標(biāo)簽,后者會(huì)。優(yōu)先級(jí)不高:order=7

?二、th:value:設(shè)置當(dāng)前元素的value值,類(lèi)似修改指定屬性的還有th:src,th:href。優(yōu)先級(jí)不高:order=6

?三、th:each:遍歷循環(huán)元素,和th:text或th:value一起使用。注意該屬性修飾的標(biāo)簽位置,詳細(xì)往后看。優(yōu)先級(jí)很高:order=2

?四、th:if:條件判斷,類(lèi)似的還有th:unless,th:switch,th:case。優(yōu)先級(jí)較高:order=3

?五、th:insert:代碼塊引入,類(lèi)似的還有th:replace,th:include,三者的區(qū)別較大,若使用不恰當(dāng)會(huì)破壞html結(jié)構(gòu),常用于公共代碼塊提取的場(chǎng)景。優(yōu)先級(jí)最高:order=1

?六、th:fragment:定義代碼塊,方便被th:insert引用。優(yōu)先級(jí)最低:order=8

?七、th:object:聲明變量,一般和*{}一起配合使用,達(dá)到偷懶的效果。優(yōu)先級(jí)一般:order=4

?八、th:attr:修改任意屬性,實(shí)際開(kāi)發(fā)中用的較少,因?yàn)橛胸S富的其他th屬性幫忙,類(lèi)似的還有th:attrappend,th:attrprepend。優(yōu)先級(jí)一般:order=5

thymeleaf.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p th:text="${thText}"></p>
    <p th:utext="${thUText}"></p>
    <input type="text" th:value="${thValue}">
    <div th:each="message:${thEach}">
        <p th:text="${message}"></p>
    </div>
    <div>
        <p th:text="${message}" th:each="message:${thEach}"></p>
    </div>
    <p th:text="${thIf}" th:if="${not #strings.isEmpty(thIf)}"></p>
    <div th:object="${thObject}">
        <p>name:<span th:text="*{name}"/></p>
        <p>age:<span th:text="*{age}"/></p>
        <p>gender:<span th:text="*{gender}"/></p>
    </div>

</body>
</html>

ThymeleafController.java

package com.example.controller;

import com.example.entity.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ThymeleafController {

    @RequestMapping("thymeleaf")
    public String thymeleaf(ModelMap map){

        map.put("thText","th:text設(shè)置文本內(nèi)容 <b>加粗</b>");
        map.put("thUText","th:utext 設(shè)置文本內(nèi)容 <b>加粗</b>");
        map.put("thValue","thValue 設(shè)置當(dāng)前元素的value值");
        map.put("thEach","Arrays.asList(\"th:each\", \"遍歷列表\")");
        map.put("thIf","msg is not null");
        map.put("thObject",new Person("zhangsan",12,"男"));
        return "thymeleaf";
    }
}

以上就是動(dòng)力節(jié)點(diǎn)小編介紹的"Thymeleaf模板引擎的使用",希望對(duì)大家有幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢(xún),有專(zhuān)業(yè)老師隨時(shí)為您服務(wù)。

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 老太太毛片 | 一级片 在线播放 | 无遮挡一级毛片视频 | 在线观看精品视频一区二区三区 | 国产日产欧美精品 | 爱爱网站在线观看免费 | 成人看的午夜免费毛片 | a免费毛片在线播放 | 日日日视频 | 伊人99在线观看 | 亚洲福利一区二区三区 | www黄在线观看 | 四虎永久在线免费观看 | 亚洲区精品久久一区二区三区 | 精品伊人 | 精品亚洲无人区一区二区 | 人成精品视频三区二区一区 | 丁香综合在线 | 日本欧美黄色 | 另类色视频 | 亚洲国产成人精品区 | 狠狠色噜噜狠狠狠狠狠色综合久久 | 婷婷激情五月网 | 成 人国产在线观看高清不卡 | 美女hdxxxx中国| 亚洲综合五月天婷 | 亚洲综合成人在线 | 亚洲一区二区三区久久 | 国产精品久久亚洲不卡4k岛国 | 大乳妇女bd视频在线观看 | 国产精品一区二区综合 | 香蕉网久久| 国产免费69成人精品视频 | 亚洲爱v| 国产精品品福利视频 | 麻豆精品在线 | 99久久免费费视频在线观看 | 欧美一区在线观看视频 | 4huh34四虎最新 | 不一样的天空在线高清观看 | 97在线免费观看 |