Springboot Thymeleaf字符串对象实例解析

Thymeleaf主要使用 org.thymeleaf.expression.Strings 类处理字符串,在模板中使用 #strings 对象来处理字符串。

开发环境:IntelliJ IDEA 2019.2.2

Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml

加入Thymeleaf依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

2、src/main/resources/application.yml

设置模板缓存为false,这样修改html页面后刷新浏览器能马上看到结果

spring:
thymeleaf:
cache: false

3、src/main/java/com/example/demo/TestController.java

package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
  @RequestMapping("/")
  public String test(){
    return "test";
  }
}

4、src/main/resources/templates/test.html

调用参数的toString方法返回字符串
<div th:text="${#strings.toString('hello')}"></div>
返回字符串的长度
<div th:text="${#strings.length('hello')}"></div>
判断是否为空或null
<div th:text="${#strings.isEmpty('hello')}"></div>
<div th:text="${#strings.isEmpty('')}"></div>
<div th:text="${#strings.isEmpty(null)}"></div>
为空或null时设置默认值
<div th:text="${#strings.defaultString('hello','a')}"></div>
<div th:text="${#strings.defaultString('','b')}"></div>
<div th:text="${#strings.defaultString(null,'c')}"></div>
判断是否包含(区分大小写)
<div th:text="${#strings.contains('hello','he')}"></div>
<div th:text="${#strings.contains('hello','HE')}"></div>
判断是否包含(忽略大小写)
<div th:text="${#strings.containsIgnoreCase('hello','he')}"></div>
<div th:text="${#strings.containsIgnoreCase('hello','HE')}"></div>
判断开头和结尾是否包含(区分大小写)
<div th:text="${#strings.startsWith('hello','he')}"></div>
<div th:text="${#strings.startsWith('hello','HE')}"></div>
<div th:text="${#strings.startsWith('hello','el')}"></div>
<div th:text="${#strings.endsWith('hello','lo')}"></div>
获取字符串的索引(如果不存在返回-1)
<div th:text="${#strings.indexOf('hello','el')}"></div>
<div th:text="${#strings.indexOf('hello','ee')}"></div>
指定开始和结束索引,截取字符串(如果索引超过字符串长度,则抛出异常)
<div th:text="${#strings.substring('hello',1,3)}"></div>
指定从某个字符串后面截取字符串(如果不包含则返回空字符串)
<div th:text="${#strings.substringAfter('hello','e')}"></div>
<div th:text="${#strings.substringAfter('hello','ee')}"></div>
指定从某个字符串前面截取字符串(如果不包含则返回空字符串)
<div th:text="${#strings.substringBefore('hello','e')}"></div>
<div th:text="${#strings.substringBefore('hello','ee')}"></div>
替换字符串
<div th:text="${#strings.replace('hello','e','a')}"></div>
转换为大写
<div th:text="${#strings.toUpperCase('hello')}"></div>
转换为小写
<div th:text="${#strings.toLowerCase('HELLO')}"></div>
首字母转换为大写
<div th:text="${#strings.capitalize('hello')}"></div>
首字母转换为小写
<div th:text="${#strings.unCapitalize('heLLo')}"></div>
每个单词的首字母转为大写
<div th:text="${#strings.capitalizeWords('hello world')}"></div>
根据分隔符将每个单词的首字母转换为大写
<div th:text="${#strings.capitalizeWords('hello-world','-')}"></div>
字符串前面追加
<div th:text="${#strings.prepend('world','hello ')}"></div>
字符串后面追加
<div th:text="${#strings.append('hello',' world')}"></div>
拼接字符串(参数个数不限)
<div th:text="${#strings.concat('hello',' world',' !')}"></div>
从第二个参数之后拼接字符串,如果参数为null,则用第一个参数替代
<div th:text="${#strings.concatReplaceNulls('*','hello',null,'world')}"></div>
删除空白
<div th:text="${#strings.trim(' hello ')}"></div>
字符串截取指定长度(最小为3),后面加...
<div th:text="${#strings.abbreviate('hello,world', 8)}"></div>
产生指定位数的随机字母数字,范围为大写英文字母加0-9数字
<div th:text="${#strings.randomAlphanumeric(4)}"></div>
调用HtmlEscape类的escapeHtml4Xml方法对参数进行编码
<div th:text="${#strings.escapeXml('<span>hello</span>')}"></div>

浏览器访问:http://localhost:8080

页面输出:

调用参数的toString方法返回字符串
hello
返回字符串的长度
判断是否为空或null
false
true
true
为空或null时设置默认值
hello
b
c
判断是否包含(区分大小写)
true
false
判断是否包含(忽略大小写)
true
true
判断开头和结尾是否包含(区分大小写)
true
false
false
true
获取字符串的索引(如果不存在返回-1)
-1
指定开始和结束索引,截取字符串(如果索引超过字符串长度,则抛出异常)
el
指定从某个字符串后面截取字符串(如果不包含则返回空字符串)
llo
指定从某个字符串前面截取字符串(如果不包含则返回空字符串)
h
替换字符串
hallo
转换为大写
HELLO
转换为小写
hello
首字母转换为大写
Hello
首字母转换为小写
heLLo
每个单词的首字母转为大写
Hello World
根据分隔符将每个单词的首字母转换为大写
Hello-World
字符串前面追加
hello world
字符串后面追加
hello world
拼接字符串(参数个数不限)
hello world !
从第二个参数之后拼接字符串,如果参数为null,则用第一个参数替代
hello*world
删除空白
hello
字符串截取指定长度(最小为3),后面加...
hello...
产生指定位数的随机字母数字,范围为大写英文字母加0-9数字
PBAT
调用HtmlEscape类的escapeHtml4Xml方法对参数进行编码
&lt;span&gt;hello&lt;/span&gt;

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • SpringBoot引入Thymeleaf的实现方法

    1.Thymeleaf简介 Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用 Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建模,Thymeleaf的可扩展性也非常棒.你可以使用它定义自己的模板属性集合,这样就可以计算自定义表达式并使用自定义逻辑,Thymeleaf还可以作为模板引擎框架. 2.引入Thymeleaf 引入依赖 在maven(pom.xml)中直接引入: <dependency>

  • SpringBoot使用thymeleaf模板过程解析

    这篇文章主要介绍了SpringBoot使用thymeleaf模板过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导入依赖 <!-- 添加thymeleaf模版的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</art

  • springboot2.1.7整合thymeleaf代码实例

    这篇文章主要介绍了springboot2.1.7整合thymeleaf代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.在pom里面添加thymeleaf依赖 <!--thymeleaf--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymelea

  • SpringBoot thymeleaf eclipse热部署方案操作步骤

    网上找了好多的springboot热部署方案,也尝试了好几种方法,下面是我的成功方案跟大家分享 操作步骤 1.pom中添加热部署依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency&g

  • Spring Boot 整合 Shiro+Thymeleaf过程解析

    这篇文章主要介绍了Spring Boot 整合 Shiro+Thymeleaf过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.导包 <!-- springboot 与 shiro 的集成--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <

  • Spring Boot集成Thymeleaf的方法

    一.Java模板引擎 模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档. 在java中,主要的模板引擎有JSP.Thymeleaf.FreeMarker.Velocity等. 虽然随着前后端分离的崛起和流行,模板引擎已遭受到冷落,但不少旧项目依然使用java的模板引擎渲染界面,而偶尔自己写一些练手项目,使用模板引擎也比起前后端分离要来的快速. 本系列会分别讲解SpringBoot

  • Spring Boot 2 Thymeleaf服务器端表单验证实现详解

    这篇文章主要介绍了Spring Boot 2 Thymeleaf服务器端表单验证实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 表单验证分为前端验证和服务器端验证. 服务器端验证方面,Java提供了主要用于数据验证的JSR 303规范,而Hibernate Validator实现了JSR 303规范. 项目依赖加入spring-boot-starter-thymeleaf时,默认就会加入Hibernate Validator的依赖. 开

  • Spring Boot Thymeleaf实现国际化的方法详解

    前言 开发传统Java WEB工程时,我们可以使用JSP页面模板语言,但是在SpringBoot中已经不推荐使用了.SpringBoot支持如下页面模板语言 Thymeleaf FreeMarker Velocity Groovy JSP 上面并没有列举所有SpringBoot支持的页面模板技术.其中Thymeleaf是SpringBoot官方所推荐使用的,下面来谈谈Thymeleaf实现应用国际化方法. ps:当然现在开发基本上是前后端分离了,但是难免需要维护遗留项目或没有条件前后端分离的团队

  • Springboot Thymeleaf字符串对象实例解析

    Thymeleaf主要使用 org.thymeleaf.expression.Strings 类处理字符串,在模板中使用 #strings 对象来处理字符串. 开发环境:IntelliJ IDEA 2019.2.2 Spring Boot版本:2.1.8 新建一个名称为demo的Spring Boot项目. 1.pom.xml 加入Thymeleaf依赖 <dependency> <groupId>org.springframework.boot</groupId> &

  • Springboot分页插件使用实例解析

    这篇文章主要介绍了Springboot分页插件使用实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在springboot工程下的pom.xml中添加依赖 <!--分页 pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter

  • springboot 默认静态路径实例解析

    这篇文章主要介绍了springboot 默认静态路径实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码如下所示 类ResourceProperties.class private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{"classpath:/META-INF/resources/", "classpath:/resourc

  • springboot日期转换器实现实例解析

    这篇文章主要介绍了springboot日期转换器实现实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 注:该功能并非springboot特有的功能,springmvc同样具有 一.使用方法 创建一个DateConverter类实现Converter接口 注:importorg.springframework.core.convert.converter.Converter; Converter<S,T> @param<S>t

  • Python小整数对象池和字符串intern实例解析

    is用于判断两个对象是否为同一个对象,具体来说是两个对象在内存中的位置是否相同. python为了提高效率,节省内存,在实现上大量使用了缓冲池技术和字符串intern技术. 整数和字符串是不可变对象,也就意味着可以用来共享,如100个"python"字串变量可以共享一个"python"字符串对象,而不是创建100个"python"字符串. 小整数对象池 为了应对小整数的频繁使用,python使用对小整数进行了缓存,默认范围为[-5,256],在这

  • Thymeleaf对象的使用之基本对象实例解析

    Thymeleaf中有许多内置对象,可以在模板中实现各种功能. 下面有几个基本对象. Web对象常用有:request.session.servletContext. Thymeleaf提供了几个内置变量param.session.application,分别可以访问请求参数.session属性.application属性. 其中request的所有属性可以直接使用 ${属性名} 访问. 备注:内置对象与内置变量是两个概念,内置对象使用"${#对象}"形式,内置变量则不需要"

  • C++类和对象实例解析(二)

    C++既是面向对象也是面向过程的语言,在这里就有一个重要的概念--类.         何谓类?类是对对象的一种抽象,举例来讲:每一个实实在在存在的人就是一个对象,人有很多共同的特征(一个头,两条腿,能走,能跑),这具有共同特征的人就成为一个类.类是一个抽象的名词,每一个人(即对象)是这个类的实例.         对象间具有的共同特征是对象的属性和行为.录像机是一个对象,它的属性是生产厂家.牌子.重量.颜色等等,它的行为就是它的功能,如录像.放像.快进.倒退等操作. C++程序中,需要先定义一

  • SpringBoot thymeleaf的使用方法解析

    1.pom.xml添加相应依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> 2.application.properties #thymeleaf spring.thymeleaf.prefix=classpath:/templa

  • Java对象的XML序列化与反序列化实例解析

    上一篇文章我们介绍了java实现的各种排序算法代码示例,本文我们看看Java对象的xml序列化与反序列化的相关内容,具体如下. XML是一种标准的数据交换规范,可以方便地用于在应用之间交换各类数据.如果能在Java对象和XML文档之间建立某种映射,例如Java对象的XML序列化和反序列化,那么就可以使Java的对象方便地与其他应用进行交换. java.beans包里面有两个类XMLEncoder和Decoder,分别用于将符合JabaBeans规范的Java对象以XML方式序列化和反序列化.以下

  • springboot+thymeleaf整合阿里云OOS对象存储图片的实现

    目录 1.先引入pom依赖 2.编写前端thymleeaf代码tetsfile.html 3.service层编写 4.controller层编写 今天再进行创建项目时想使用阿里云oos进行存储图片 下面进行实操 1.先引入pom依赖 <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.9.1

随机推荐