一. Thymeleaf变量输出
1. th:text在页面中输出变量。
2. th:value将变量输出到input标签的value中。
二. Thymeleaf字符串操作
1. Thymeleaf调用内置对象一定要用#, 大部分的内置对象都以s结尾strings、numbers、dates。
2. ${#strings.isEmpty(msg)}判断字符串是否为空, 如果为空返回true, 否则返回false。
3. ${#strings.contains(msg, 'a')}判断字符串是否包含指定的子串, 如果包含返回true, 否则返回false。
4. ${#strings.startsWith(msg, 'a')}判断当前字符串是否以子串开头, 如果是返回true, 否则返回false。
5. ${#strings.endsWith(msg, 'a')}判断当前字符串是否以子串结尾, 如果是返回true, 否则返回false。
6. ${#strings.length(msg)}返回字符串的长度。
7. ${#strings.indexOf(msg, 'a')}查找子串的位置, 并返回该子串的下标, 如果没找到则返回-1。
8. ${#strings.substring(msg, start)}和${#strings.substring(msg, start, end)}截取子串, 用户与JDK String类下SubString方法相同。位置从0开始, 截取的字串包含起始位置, 不包含结束位置。
9. ${#strings.toUpperCase(msg)}将字符串转换为大写。
10. ${#strings.toLowerCase(msg)}将字符串转换为小写。
三. Thymeleaf变量输出与字符串操作案例
1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-text-strings项目
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标签不规范报错 -->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<!-- springBoot的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
3. 创建存放视图的目录, 目录位置: src/main/resources/templates。templates: 该目录是安全的, 意味着该目录下的内容是不允许外界直接访问的。
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标签的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判断当前字符串是否以子串开头</h3>
"<span th:text="${msg}" style="color: red;"></span>"是否以"_Thy"开头: <span th:text="${#strings.startsWith(msg,'_Thy')}"></span><br />
"<span th:text="${msg}" style="color: red;"></span>"是否以"Thy"开头: <span th:text="${#strings.startsWith(msg,'Thy')}"></span>
<hr/>
<h3>Thymeleaf判断当前字符串是否以子串结尾</h3>
"<span th:text="${msg}" style="color: red;"></span>"是否以"_案例"结尾: <span th:text="${#strings.endsWith(msg,'_案例')}"></span><br />
"<span th:text="${msg}" style="color: red;"></span>"是否以"案例"结尾: <span th:text="${#strings.endsWith(msg,'案例')}"></span>
<hr/>
<h3>Thymeleaf返回字符串的长度</h3>
"<span th:text="${msg}" style="color: red;"></span>"的长度: <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)位置开始的字符串: <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字符串大小写转换</h3>
"<span th:text="${msg}" style="color: red;"></span>"转换为大写: <span th:text="${#strings.toUpperCase(msg)}"></span><br />
"<span th:text="${msg}" style="color: red;"></span>"转换为小写: <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启动类
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
7. 启动项目, 并使用浏览器访问