一. 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. 启动项目, 并使用浏览器访问

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

  1. Thymeleaf 变量输出与字符串操作

    变量输出 th:text 在页面中输出值 th:value 将一个值放入到 input 标签的 value 中 字符串操作 ${#strings.isEmpty(key)} String 操作用来判空 ...

  2. linux 变量替换字符串,shell中常用的变量处理、字符串操作(之一)

    在shell中,当我们已经获取到某个变量的变量值的时候,可能还需要对变量值进行一定的处理,才能到我们最终想要的结果,今天我们就来聊聊shell中常见的变量处理方法,我们通常会对变量进行如下处理. 一. ...

  3. 015_SpringBoot视图层技术thymeleaf-URL表达式

    1. url表达式: th:href和th:src. 2. url表达式基本语法: @{}. 3. 绝对路径: <a th:href="@{http://www.baidu.com}& ...

  4. 014_SpringBoot视图层技术thymeleaf-访问域对象

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-scope项目 2. pom.xml <project xmlns="http: ...

  5. 013_SpringBoot视图层技术thymeleaf-迭代遍历

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-each项目 2. pom.xml <project xmlns="http:/ ...

  6. 012_SpringBoot视图层技术thymeleaf-条件判断

    1. 使用maven构建SpringBoot的名叫spring-boot-view-thymeleaf-if-switch项目 2. pom.xml <project xmlns="h ...

  7. 011_SpringBoot视图层技术thymeleaf-日期格式化

    一. 日期格式化 1. ${#dates.format(birthday)}格式化日期, 默认的以浏览器默认语言为格式化标准. 2. ${#dates.format(birthday,'yyy/MM/ ...

  8. 008_SpringBoot视图层技术jsp

    1. 使用maven构建SpringBoot的名叫spring-boot-view-jsp项目 2. pom.xml <project xmlns="http://maven.apac ...

  9. 【C++ 语言】C++字符串 ( string 类 | 创建方法 | 控制台输出 | 字符串操作 | 栈内存字符串对象 | string* )

    文章目录 C++ 字符串 对象 C++ 字符串 对象 创建方法 C++ 字符串 对象 输出到控制台 C++ 字符串 拼接 C++ 字符串 方法调用 ( 栈内存对象 ) C++ 字符串 方法调用 ( 堆 ...

最新文章

  1. 基于LightGBM算法实现数据挖掘!
  2. ArcGIS Engine控件运行许可学习总结
  3. C++判断一个数是否为armstrong number阿姆斯特朗数(附完整源码)
  4. 什么是随机存取_SRAM存储器是什么存储器
  5. 训练神经网络时如何确定batch size?
  6. bst 删除节点_在BST中删除大于或等于k的节点
  7. 轨道运营管理专业自荐书_轨道运营管理专业主要是学习什么_毕业后薪资待遇怎么样...
  8. python中int对象不可调用_'int'对象在python中不可调用
  9. 首次超过苹果!华为高端智能手机Q1国内市场出货量占48%
  10. 架构之技术复杂度与业务复杂度
  11. 无法执行dex:多个dex文件定义了Lcom / myapp / R $ array;
  12. scikit-image 库简介
  13. 50 years, 50 colors HDU - 1498(最小点覆盖或者说最小顶点匹配)
  14. 《浪潮之巅》笔记之七
  15. 八大排序算法(Java实现)
  16. apdu 移动sim_在Android中将APDU命令发送到USIM / SIM卡
  17. 浏览器打开pdf文件默认全屏设置方法
  18. 别有幽愁暗恨生,此时无声胜有声——python循环结构
  19. 日语自我介绍 自己紹介・自己PR
  20. 大数据人工智能时代你跟上步伐了吗?(号称第四次技术革命)

热门文章

  1. 求多个四元数的平均数
  2. Python 浮点数运算
  3. 卡特兰数Catalan Number
  4. 2010年基于Linux的10大技术趋势
  5. .net讀取指定節點的值
  6. 将dos格式文件转换为unix格式
  7. 关于在ROS kinetic下arbotix报错的问题
  8. kafka 分区和副本以及kafaka 执行流程,以及消息的高可用
  9. Codeforces Round #395 (Div. 2)(未完)
  10. 【KVM】Ubuntu14.04 安装KVM