当前位置:   article > 正文

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

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 

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.bjbs</groupId>
  6. <artifactId>spring-boot-view-thymeleaf-text-strings</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <parent>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-parent</artifactId>
  11. <version>1.5.13.RELEASE</version>
  12. </parent>
  13. <!-- 修改jdk版本 -->
  14. <properties>
  15. <java.version>1.8</java.version>
  16. <!-- 指定thymeleaf和thymeleaf-layout-dialect高版本可以防止html标签不规范报错 -->
  17. <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
  18. <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
  19. </properties>
  20. <dependencies>
  21. <!-- springBoot的启动器 -->
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. <!-- thymeleaf的启动器 -->
  27. <dependency>
  28. <groupId>org.springframework.boot</groupId>
  29. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  30. </dependency>
  31. </dependencies>
  32. </project>

3. 创建存放视图的目录, 目录位置: src/main/resources/templates。templates: 该目录是安全的, 意味着该目录下的内容是不允许外界直接访问的。

4. 编写text-strings.html 

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Thymeleaf变量输出与字符串操作</title>
  6. </head>
  7. <body>
  8. <h3>Thymeleaf直接输出变量</h3>
  9. <span th:text="'Hello Thymeleaf'"></span>
  10. <hr/>
  11. <h3>Thymeleaf输出作用域中的变量</h3>
  12. <span th:text="${msg}"></span>
  13. <hr/>
  14. <h3>Thymeleaf将变量输出到input标签的value中</h3>
  15. <input type="text" name="username" th:value="${msg}"/>
  16. <hr/>
  17. <h3>Thymeleaf判断字符串是否为空</h3>
  18. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否为空: <span th:text="${#strings.isEmpty(msg)}"></span>
  19. <hr/>
  20. <h3>Thymeleaf判断字符串是否包含指定的子串</h3>
  21. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否包含"_变量": <span th:text="${#strings.contains(msg,'_变量')}"></span><br />
  22. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否包含"变量": <span th:text="${#strings.contains(msg,'变量')}"></span>
  23. <hr/>
  24. <h3>Thymeleaf判断当前字符串是否以子串开头</h3>
  25. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"_Thy"开头: <span th:text="${#strings.startsWith(msg,'_Thy')}"></span><br />
  26. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"Thy"开头: <span th:text="${#strings.startsWith(msg,'Thy')}"></span>
  27. <hr/>
  28. <h3>Thymeleaf判断当前字符串是否以子串结尾</h3>
  29. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"_案例"结尾: <span th:text="${#strings.endsWith(msg,'_案例')}"></span><br />
  30. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;是否以"案例"结尾: <span th:text="${#strings.endsWith(msg,'案例')}"></span>
  31. <hr/>
  32. <h3>Thymeleaf返回字符串的长度</h3>
  33. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;的长度: <span th:text="${#strings.length(msg)}"></span>
  34. <hr/>
  35. <h3>Thymeleaf查找子串的位置</h3>
  36. 'h'在&#34;<span th:text="${msg}" style="color: red;"></span>&#34;中的位置: <span th:text="${#strings.indexOf(msg,'h')}"></span>
  37. <hr/>
  38. <h3>Thymeleaf截取子串</h3>
  39. 截取&#34;<span th:text="${msg}" style="color: red;"></span>&#34;从13(包含13)位置开始的字符串: <span th:text="${#strings.substring(msg, 13)}"></span><br />
  40. 截取&#34;<span th:text="${msg}" style="color: red;"></span>&#34;从13(包含13)到14(不包含14)位置的字符串: <span th:text="${#strings.substring(msg, 13, 14)}"></span>
  41. <hr/>
  42. <h3>Thymeleaf字符串大小写转换</h3>
  43. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;转换为大写: <span th:text="${#strings.toUpperCase(msg)}"></span><br />
  44. &#34;<span th:text="${msg}" style="color: red;"></span>&#34;转换为小写: <span th:text="${#strings.toLowerCase(msg)}"></span>
  45. </body>
  46. </html>

5. 新建UserController.java

  1. package com.bjbs.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @Controller
  6. public class UserController {
  7. @RequestMapping("/show")
  8. public String showInfo(Model model) {
  9. model.addAttribute("msg", "Thymeleaf 变量输出与字符串操作案例");
  10. return "text-strings";
  11. }
  12. }

6. 新建App.java

  1. package com.bjbs;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * SpringBoot启动类
  6. */
  7. @SpringBootApplication
  8. public class App {
  9. public static void main(String[] args) {
  10. SpringApplication.run(App.class, args);
  11. }
  12. }

7. 启动项目, 并使用浏览器访问

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/程序自动化专家/article/detail/62673
推荐阅读
相关标签
  

闽ICP备14008679号