javascript
010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作
一. Thymeleaf變量輸出
1. th:text在頁(yè)面中輸出變量。
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)前字符串是否以子串開(kāi)頭, 如果是返回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), 如果沒(méi)找到則返回-1。
8. ${#strings.substring(msg, start)}和${#strings.substring(msg, start, end)}截取子串, 用戶與JDK String類下SubString方法相同。位置從0開(kāi)始, 截取的字串包含起始位置, 不包含結(jié)束位置。
9. ${#strings.toUpperCase(msg)}將字符串轉(zhuǎn)換為大寫(xiě)。
10. ${#strings.toLowerCase(msg)}將字符串轉(zhuǎn)換為小寫(xiě)。
三. 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)容是不允許外界直接訪問(wèn)的。
4. 編寫(xiě)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>"<span th:text="${msg}" style="color: red;"></span>"是否為空: <span th:text="${#strings.isEmpty(msg)}"></span><hr/><h3>Thymeleaf判斷字符串是否包含指定的子串</h3>"<span th:text="${msg}" style="color: red;"></span>"是否包含"_變量": <span th:text="${#strings.contains(msg,'_變量')}"></span><br />"<span th:text="${msg}" style="color: red;"></span>"是否包含"變量": <span th:text="${#strings.contains(msg,'變量')}"></span><hr/><h3>Thymeleaf判斷當(dāng)前字符串是否以子串開(kāi)頭</h3>"<span th:text="${msg}" style="color: red;"></span>"是否以"_Thy"開(kāi)頭: <span th:text="${#strings.startsWith(msg,'_Thy')}"></span><br />"<span th:text="${msg}" style="color: red;"></span>"是否以"Thy"開(kāi)頭: <span th:text="${#strings.startsWith(msg,'Thy')}"></span><hr/><h3>Thymeleaf判斷當(dāng)前字符串是否以子串結(jié)尾</h3>"<span th:text="${msg}" style="color: red;"></span>"是否以"_案例"結(jié)尾: <span th:text="${#strings.endsWith(msg,'_案例')}"></span><br />"<span th:text="${msg}" style="color: red;"></span>"是否以"案例"結(jié)尾: <span th:text="${#strings.endsWith(msg,'案例')}"></span><hr/><h3>Thymeleaf返回字符串的長(zhǎng)度</h3>"<span th:text="${msg}" style="color: red;"></span>"的長(zhǎng)度: <span th:text="${#strings.length(msg)}"></span><hr/><h3>Thymeleaf查找子串的位置</h3>'h'在"<span th:text="${msg}" style="color: red;"></span>"中的位置: <span th:text="${#strings.indexOf(msg,'h')}"></span><hr/><h3>Thymeleaf截取子串</h3>截取"<span th:text="${msg}" style="color: red;"></span>"從13(包含13)位置開(kāi)始的字符串: <span th:text="${#strings.substring(msg, 13)}"></span><br />截取"<span th:text="${msg}" style="color: red;"></span>"從13(包含13)到14(不包含14)位置的字符串: <span th:text="${#strings.substring(msg, 13, 14)}"></span><hr/><h3>Thymeleaf字符串大小寫(xiě)轉(zhuǎn)換</h3>"<span th:text="${msg}" style="color: red;"></span>"轉(zhuǎn)換為大寫(xiě): <span th:text="${#strings.toUpperCase(msg)}"></span><br />"<span th:text="${msg}" style="color: red;"></span>"轉(zhuǎn)換為小寫(xiě): <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)目, 并使用瀏覽器訪問(wèn)
總結(jié)
以上是生活随笔為你收集整理的010_SpringBoot视图层技术thymeleaf-变量输出与字符串操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 008_SpringBoot视图层技术j
- 下一篇: 011_SpringBoot视图层技术t