日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作

發(fā)布時(shí)間:2025/5/22 javascript 15 豆豆
生活随笔 收集整理的這篇文章主要介紹了 010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一. Thymeleaf變量輸出

1. th:text在頁面中輸出變量。

2. th:value將變量輸出到input標(biāo)簽的value中。

二. Thymeleaf字符串操作

1. Thymeleaf調(diào)用內(nèi)置對(duì)象一定要用#, 大部分的內(nèi)置對(duì)象都以s結(jié)尾strings、numbers、dates。

2. ${#strings.isEmpty(msg)}判斷字符串是否為空, 如果為空返回true, 否則返回false。

3. ${#strings.contains(msg, 'a')}判斷字符串是否包含指定的子串, 如果包含返回true, 否則返回false。

4. ${#strings.startsWith(msg, 'a')}判斷當(dāng)前字符串是否以子串開頭, 如果是返回true, 否則返回false。

5. ${#strings.endsWith(msg, 'a')}判斷當(dāng)前字符串是否以子串結(jié)尾, 如果是返回true, 否則返回false。

6. ${#strings.length(msg)}返回字符串的長(zhǎng)度。

7. ${#strings.indexOf(msg, 'a')}查找子串的位置, 并返回該子串的下標(biāo), 如果沒找到則返回-1。

8. ${#strings.substring(msg, start)}和${#strings.substring(msg, start, end)}截取子串, 用戶與JDK String類下SubString方法相同。位置從0開始, 截取的字串包含起始位置, 不包含結(jié)束位置。

9. ${#strings.toUpperCase(msg)}將字符串轉(zhuǎn)換為大寫。

10. ${#strings.toLowerCase(msg)}將字符串轉(zhuǎn)換為小寫。

三. Thymeleaf變量輸出與字符串操作案例

1. 使用maven構(gòu)建SpringBoot的名叫spring-boot-view-thymeleaf-text-strings項(xiàng)目

2. pom.xml?

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.bjbs</groupId><artifactId>spring-boot-view-thymeleaf-text-strings</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.13.RELEASE</version></parent><!-- 修改jdk版本 --><properties><java.version>1.8</java.version><!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html標(biāo)簽不規(guī)范報(bào)錯(cuò) --><thymeleaf.version>3.0.2.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version></properties><dependencies><!-- springBoot的啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- thymeleaf的啟動(dòng)器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency></dependencies> </project>

3. 創(chuàng)建存放視圖的目錄, 目錄位置: src/main/resources/templates。templates: 該目錄是安全的, 意味著該目錄下的內(nèi)容是不允許外界直接訪問的。

4. 編寫text-strings.html?

<!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>Thymeleaf變量輸出與字符串操作</title></head><body><h3>Thymeleaf直接輸出變量</h3><span th:text="'Hello Thymeleaf'"></span><hr/><h3>Thymeleaf輸出作用域中的變量</h3><span th:text="${msg}"></span><hr/><h3>Thymeleaf將變量輸出到input標(biāo)簽的value中</h3><input type="text" name="username" th:value="${msg}"/><hr/><h3>Thymeleaf判斷字符串是否為空</h3>&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否為空: <span th:text="${#strings.isEmpty(msg)}"></span><hr/><h3>Thymeleaf判斷字符串是否包含指定的子串</h3>&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否包含"_變量": <span th:text="${#strings.contains(msg,'_變量')}"></span><br />&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否包含"變量": <span th:text="${#strings.contains(msg,'變量')}"></span><hr/><h3>Thymeleaf判斷當(dāng)前字符串是否以子串開頭</h3>&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"_Thy"開頭: <span th:text="${#strings.startsWith(msg,'_Thy')}"></span><br />&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"Thy"開頭: <span th:text="${#strings.startsWith(msg,'Thy')}"></span><hr/><h3>Thymeleaf判斷當(dāng)前字符串是否以子串結(jié)尾</h3>&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"_案例"結(jié)尾: <span th:text="${#strings.endsWith(msg,'_案例')}"></span><br />&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"案例"結(jié)尾: <span th:text="${#strings.endsWith(msg,'案例')}"></span><hr/><h3>Thymeleaf返回字符串的長(zhǎng)度</h3>&#34;<span th:text="${msg}" style="color: red;"></span>&#34;的長(zhǎng)度: <span th:text="${#strings.length(msg)}"></span><hr/><h3>Thymeleaf查找子串的位置</h3>'h'在&#34;<span th:text="${msg}" style="color: red;"></span>&#34;中的位置: <span th:text="${#strings.indexOf(msg,'h')}"></span><hr/><h3>Thymeleaf截取子串</h3>截取&#34;<span th:text="${msg}" style="color: red;"></span>&#34;從13(包含13)位置開始的字符串: <span th:text="${#strings.substring(msg, 13)}"></span><br />截取&#34;<span th:text="${msg}" style="color: red;"></span>&#34;從13(包含13)到14(不包含14)位置的字符串: <span th:text="${#strings.substring(msg, 13, 14)}"></span><hr/><h3>Thymeleaf字符串大小寫轉(zhuǎn)換</h3>&#34;<span th:text="${msg}" style="color: red;"></span>&#34;轉(zhuǎn)換為大寫: <span th:text="${#strings.toUpperCase(msg)}"></span><br />&#34;<span th:text="${msg}" style="color: red;"></span>&#34;轉(zhuǎn)換為小寫: <span th:text="${#strings.toLowerCase(msg)}"></span></body> </html>

5. 新建UserController.java

package com.bjbs.controller;import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping;@Controller public class UserController {@RequestMapping("/show")public String showInfo(Model model) {model.addAttribute("msg", "Thymeleaf 變量輸出與字符串操作案例");return "text-strings";} }

6. 新建App.java

package com.bjbs;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;/*** SpringBoot啟動(dòng)類*/ @SpringBootApplication public class App {public static void main(String[] args) {SpringApplication.run(App.class, args);} }

7. 啟動(dòng)項(xiàng)目, 并使用瀏覽器訪問

總結(jié)

以上是生活随笔為你收集整理的010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。