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

一. 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>
		&#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判断当前字符串是否以子串开头</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判断当前字符串是否以子串结尾</h3>
		&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"_案例"结尾: <span th:text="${#strings.endsWith(msg,'_案例')}"></span><br />
		&#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"案例"结尾: <span th:text="${#strings.endsWith(msg,'案例')}"></span>
		<hr/>
		<h3>Thymeleaf返回字符串的长度</h3>
		&#34;<span th:text="${msg}" style="color: red;"></span>&#34;的长度: <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字符串大小写转换</h3>
		&#34;<span th:text="${msg}" style="color: red;"></span>&#34;转换为大写: <span th:text="${#strings.toUpperCase(msg)}"></span><br />
		&#34;<span th:text="${msg}" style="color: red;"></span>&#34;转换为小写: <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. 启动项目, 并使用浏览器访问

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值