Thymeleaf模板引擎入门

Spring Boot 整合 Thymeleaf 实例 给大家送一个小福利附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!给大家送一个小福利[外链图片转存中…(img-sD8d2gnA-1713293503664)]附高清脑图,高清知识点讲解教程,以及一些面试真题及答案解析。送给需要的提升技术、准备面试跳槽、自身职业规划迷茫的朋友们。 阅读详情

Thymealeaf模板引擎入门

一、Thymeleaf简述

简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点:

1、Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

2、Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

3、Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

二、Spring Boot项目Thymeleaf模板页面存放位置

Spring Boot的Thymeleaf支持:

通过Thymeleaf类对集成所需的Bean进行自动配置,包括templateResolver、templateEngine和thymeleafViewResolver的配置。

1、创建Spring Boot项目boot_thymeleaf_demo

 

pom.xml文件:


 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" 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>net.hw </groupId>
  6. <artifactId>boot_thymeleaf_demo </artifactId>
  7. <version>0.0.1-SNAPSHOT </version>
  8. <packaging>jar </packaging>
  9. <name>boot_thymeleaf_demo </name>
  10. <description>Demo project for Spring Boot </description>
  11. <parent>
  12. <groupId>org.springframework.boot </groupId>
  13. <artifactId>spring-boot-starter-parent </artifactId>
  14. <version>1.5.3.RELEASE </version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8 </project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8 </project.reporting.outputEncoding>
  20. <java.version>1.8 </java.version>
  21. </properties>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.boot </groupId>
  25. <artifactId>spring-boot-starter-thymeleaf </artifactId>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.springframework.boot </groupId>
  29. <artifactId>spring-boot-starter-test </artifactId>
  30. <scope>test </scope>
  31. </dependency>
  32. </dependencies>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.springframework.boot </groupId>
  37. <artifactId>spring-boot-maven-plugin </artifactId>
  38. </plugin>
  39. </plugins>
  40. </build>
  41. </project>

2、在templates目录创建index.html

默认的thymeleaf模板页面应该放在resources/templates目录里。


 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>index </title>
  6. </head>
  7. <body>
  8. <h1>Thymeleaf Page: Welcome to Spring Boot World! </h1>
  9. <h1>File Location: resources/templates/index.html </h1>
  10. </body>
  11. </html>

3、创建控制器HomeController,定义请求映射方法

 

4、启动程序,访问http://localhost:8080/index

 

5、在main里创建目录结构webapp/WEB-INF/page,在里面创建index.html


 
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>index </title>
  6. </head>
  7. <body>
  8. <h1>Thymeleaf Page: Welcome to Spring Boot World! </h1>
  9. <h1>File Location: webapp/WEB-INF/page/index.html </h1>
  10. </body>
  11. </html>

6、在application.properties里设置thymeleaf的前缀

 

7、启动程序,访问http://localhost:8080/index

 

由此可见,Spring Boot的自动配置确实很好用,只要设置一下属性就可以轻易搞定。

 


 

引入bootstrap到上述项目:

 

三、Thymeleaf模板基本语法

(一)简单表达式   

  1、变量的表达式:${...}

  2、选择变量表达式:*{...}

  3、信息表达式:#{...}

  4、链接URL表达式:@{...}

(二)字面值

  1、文本文字:'one text', 'Another one!',…

  2、文字数量:0, 34, 3.0, 12.3,…

  3、布尔型常量:true, false

  4、空的文字:null

  5、文字标记:one, sometext, main,…

(四)文本处理

  1、字符串并置:+

  2、文字替换:|The name is ${name}|

(五)表达式基本对象

  1、#ctx:上下文对象

  2、#vars:上下文变量

  3、#locale:上下文语言环境

  4、#httpServletRequest:(只有在Web上下文)HttpServletRequest对象

  5、#httpSession:(只有在Web上下文)HttpSession对象。

 

用法:<span th:text="${#locale.country}">US</span>.

 

(六)实用工具对象 

#dates: java.util的实用方法。对象:日期格式、组件提取等.

#calendars:类似于#日期,但对于java.util。日历对象

#numbers:格式化数字对象的实用方法。

#strings:字符串对象的实用方法:包含startsWith,将/附加等。

#objects:实用方法的对象。

#bools:布尔评价的实用方法。

#arrays:数组的实用方法。

#lists:list集合。

#sets:set集合。

#maps:map集合。

#aggregates:实用程序方法用于创建聚集在数组或集合.

#messages:实用程序方法获取外部信息内部变量表达式,以同样的方式,因为它们将获得使用# {…}语法

#ids:实用程序方法来处理可能重复的id属性(例如,由于迭代)。

 

四、案例演示

1、在application.properties文件定义属性

 

2、在templates里创建showStudent.html

利用thymeleaf消息表达式#{...}访问属性文件的数据,主要用于国际化。

利用thymeleaf超链接表达式@{...}访问静态资源或动态资源。(th:src='@{...}'、th:href='@{...}'、th:action='@{...}')


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>显示学生信息 </title>
  5. <meta charset="UTF-8"/>
  6. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  7. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  8. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  9. rel= "stylesheet"/>
  10. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  11. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="col-md-4">
  17. <div class="panel panel-primary">
  18. <div class="panel-heading text-center">
  19. <span class="panel-title">学生信息 </span>
  20. </div>
  21. <div class="panel-body">
  22. 编号: <span th:text="#{student.id}">2 </span> <br/>
  23. 姓名: <span th:text="#{student.name}">萌萌哒 </span> <br/>
  24. 性别: <span th:text="#{student.gender}">男 </span> <br/>
  25. 年龄: <span th:text="#{student.age}">20 </span> <br/>
  26. 电话: <span th:text="#{student.telephone}">15890905678 </span> <br/>
  27. </div>
  28. <div class="panel-footer text-right">
  29. <span class="panel-title">酒城工作室@2107 </span>
  30. </div>
  31. </div>
  32. </div>
  33. </div>
  34. </div>
  35. </body>
  36. </html>

在客户端显示页面:

 

只是显示静态数据,必须要通过服务端才能获取动态数据,替换静态数据,显示在模板页面。

 

4、在HomeController里添加请求映射方法

 

 

 

此时,启动程序,访问http://localhost:8080/showStudent

 

没有正确读取application.properties文件里定义的学生信息。

 

5、创建国际化配置类I18NConfig,定义资源包消息源Bean


 
  1. package net.hw.config;
  2. import org.springframework.context. annotation.Bean;
  3. import org.springframework.context. annotation.Configuration;
  4. import org.springframework.context.support.ResourceBundleMessageSource;
  5. import java.io.File;
  6. /**
  7. * Created by howard on 2017/4/25.
  8. */
  9. @Configuration
  10. public class I18NConfig {
  11. @Bean
  12. public ResourceBundleMessageSource messageSource() {
  13. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  14. messageSource.setUseCodeAsDefaultMessage( true);
  15. messageSource.setFallbackToSystemLocale( false);
  16. messageSource.setBasename( "application");
  17. messageSource.setDefaultEncoding( "UTF-8");
  18. messageSource.setCacheSeconds( 2);
  19. return messageSource;
  20. }
  21. }

 


说明:

setBaseName设置消息源的文件名,messageSource.setBasename("application");,表明消息源是以applicition打头的属性文件,如果要设置多个属性文件作为消息源,那么就要用setBaseNames方法来设置,比如:messageSource.setBasenames("student", "application"); 这样就有两个消息源:student.properties和application.properties。

 

此时启动程序,访问http://localhost:8080/showStudent

 

替换了静态数据,说明成功读取了application.properties里的数据。

 

5、在resources里创建application_zh_CN.properties


 
  1. student.id= 1
  2. student. name=郭文玲
  3. student.gender=女
  4. student.age= 18
  5. student.telephone= 15890904568

学生信息的中文版,到时会根据系统语言环境读取响应版本的属性文件。

 

此时启动程序,访问http://localhost:8080/showStudent

 

大家可以看到,显示的学生信息中文版,说明读取的是application_zh_CN.properties属性文件里的数据。

 

 

6、创建用户实体类User


 
  1. package net.hw.bean;
  2. import java.util.Date;
  3. /**
  4. * Created by howard on 2017/4/23.
  5. */
  6. public class User {
  7. /**
  8. * 用户标识符
  9. */
  10. private int id;
  11. /**
  12. * 用户名
  13. */
  14. private String username;
  15. /**
  16. * 密码
  17. */
  18. private String password;
  19. /**
  20. * 电话号码
  21. */
  22. private String telephone;
  23. /**
  24. * 注册时间
  25. */
  26. private Date registerTime;
  27. /**
  28. * 权限(0:管理员;1:普通用户)
  29. */
  30. private int popedom;
  31. public int getId() {
  32. return id;
  33. }
  34. public void setId(int id) {
  35. this.id = id;
  36. }
  37. public String getUsername() {
  38. return username;
  39. }
  40. public void setUsername(String username) {
  41. this.username = username;
  42. }
  43. public String getPassword() {
  44. return password;
  45. }
  46. public void setPassword(String password) {
  47. this.password = password;
  48. }
  49. public String getTelephone() {
  50. return telephone;
  51. }
  52. public void setTelephone(String telephone) {
  53. this.telephone = telephone;
  54. }
  55. public Date getRegisterTime() {
  56. return registerTime;
  57. }
  58. public void setRegisterTime(Date registerTime) {
  59. this.registerTime = registerTime;
  60. }
  61. public int getPopedom() {
  62. return popedom;
  63. }
  64. public void setPopedom(int popedom) {
  65. this.popedom = popedom;
  66. }
  67. @ Override
  68. public String toString () {
  69. return "User{" +
  70. "id=" + id +
  71. ", username='" + username + '\'' +
  72. ", password='" + password + '\'' +
  73. ", telephone='" + telephone + '\'' +
  74. ", registerTime=" + registerTime +
  75. ", popedom=" + popedom +
  76. '}';
  77. }
  78. }

7、创建UserService类


 
  1. package net.hw.service;
  2. import net.hw.bean.User;
  3. import org.springframework.stereotype.Service;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7. /**
  8. * Created by howard on 2017/4/25.
  9. */
  10. @Service
  11. public class UserService {
  12. public User findOneUser() {
  13. User user = new User();
  14. user.setId( 1);
  15. user.setUsername( "李文强");
  16. user.setPassword( "12345");
  17. user.setTelephone( "15890904567");
  18. user.setRegisterTime( new Date());
  19. user.setPopedom( 0);
  20. return user;
  21. }
  22. public List<User> findAllUsers() {
  23. List<User> users = new ArrayList<User>();
  24. User user = new User();
  25. user.setId( 1);
  26. user.setUsername( "李文强");
  27. user.setPassword( "12345");
  28. user.setTelephone( "15890904567");
  29. user.setRegisterTime( new Date());
  30. user.setPopedom( 0);
  31. users.add(user);
  32. user = new User();
  33. user.setId( 2);
  34. user.setUsername( "张海洋");
  35. user.setPassword( "11111");
  36. user.setTelephone( "13990904567");
  37. user.setRegisterTime( new Date());
  38. user.setPopedom( 1);
  39. users.add(user);
  40. user = new User();
  41. user.setId( 3);
  42. user.setUsername( "吴文燕");
  43. user.setPassword( "22222");
  44. user.setTelephone( "15890978905");
  45. user.setRegisterTime( new Date());
  46. user.setPopedom( 1);
  47. users.add(user);
  48. user = new User();
  49. user.setId( 4);
  50. user.setUsername( "郑智化");
  51. user.setPassword( "33333");
  52. user.setTelephone( "15990956905");
  53. user.setRegisterTime( new Date());
  54. user.setPopedom( 1);
  55. users.add(user);
  56. return users;
  57. }
  58. }

9、在templates里创建showOneUser.html


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>显示用户信息 </title>
  6. <meta charset="UTF-8"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  9. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  10. rel= "stylesheet"/>
  11. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  12. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="row">
  17. <div class="col-md-5">
  18. <div class="panel panel-primary">
  19. <div class="panel-heading text-center">
  20. <span class="panel-title">用户信息 </span>
  21. </div>
  22. <div class="panel-body">
  23. 编号: <span th:text="${user.id}"> </span> <br/>
  24. 用户名: <span th:text="${user.username}"> </span> <br/>
  25. 密码: <span th:text="${user.password}"> </span> <br/>
  26. 电话: <span th:text="${user.telephone}"> </span> <br/>
  27. 注册时间: <span th:text="${#dates.format(user.registerTime, 'yyyy-MM-dd hh:mm:ss')}"> </span> <br/>
  28. 权限: <span th:text="${user.popedom==0?'管理员':'普通用户'}"> </span> <br/>
  29. </div>
  30. <div class="panel-footer text-right">
  31. <span class="panel-title">酒城工作室@2107 </span>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. <div class="row">
  37. <div class="col-md-5">
  38. <div class="panel panel-primary">
  39. <div class="panel-heading text-center">
  40. <span class="panel-title">用户信息 </span>
  41. </div>
  42. <div class="panel-body">
  43. <div th:object="${user}">
  44. 编号: <span th:text="*{id}"> </span> <br/>
  45. 用户名: <span th:text="*{username}"> </span> <br/>
  46. 密码: <span th:text="*{password}"> </span> <br/>
  47. 电话: <span th:text="*{telephone}"> </span> <br/>
  48. 注册时间: <span th:text="*{#dates.format(registerTime, 'yyyy-MM-dd hh:mm:ss')}"> </span> <br/>
  49. 权限: <span th:text="*{popedom==0?'管理员':'普通用户'}"> </span> <br/>
  50. </div>
  51. </div>
  52. <div class="panel-footer text-right">
  53. <span class="panel-title">酒城工作室@2107 </span>
  54. </div>
  55. </div>
  56. </div>
  57. </div>
  58. </div>
  59. </body>
  60. </html>

说明:此页面采用变量表达式${...}、选择变量表达式*{...}。

为了格式化注册时间,采用#dates对象的format方法。

为了将权限的数字0和1转换成“管理员”和“普通用户”,采用了三元运算符 (逻辑表达式)?(表达式1):(表达式2)。

 

启动程序,访问http://localhost:8080/showOneUser

 

10、创建showAllUsers.html


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>显示全部用户 </title>
  6. <meta charset="UTF-8"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  9. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  10. rel= "stylesheet"/>
  11. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  12. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="row">
  17. <div class="col-md-5">
  18. <div class="panel panel-primary">
  19. <div class="panel-heading text-center">
  20. <span class="panel-title">全部用户信息 </span>
  21. </div>
  22. <div class="panel-body">
  23. <ul class="list-group">
  24. <li class="list-group-item" th:each="user:${users}">
  25. 编号: <span th:text="${user.id}"> </span> <br/>
  26. 用户名: <span th:text="${user.username}"> </span> <br/>
  27. 密码: <span th:text="${user.password}"> </span> <br/>
  28. 电话: <span th:text="${user.telephone}"> </span> <br/>
  29. 注册时间: <span th:text="${#dates.format(user.registerTime, 'yyyy-MM-dd hh:mm:ss')}"> </span> <br/>
  30. 权限: <span th:text="${user.popedom==0?'管理员':'普通用户'}"> </span> <br/>
  31. </li>
  32. </ul>
  33. </div>
  34. <div class="panel-footer text-right">
  35. <span class="panel-title">酒城工作室@2107 </span>
  36. </div>
  37. </div>
  38. </div>
  39. <div class="col-md-7">
  40. <div class="panel panel-primary">
  41. <div class="panel-heading text-center">
  42. <span class="panel-title">全部用户信息 </span>
  43. </div>
  44. <div class="panel-body">
  45. <table class="table-bordered" style="width: 100%">
  46. <tr style="height: 40px; background-color: #f7ecb5">
  47. <th class="text-center">编号 </th>
  48. <th class="text-center">用户名 </th>
  49. <th class="text-center">密码 </th>
  50. <th class="text-center">电话 </th>
  51. <th class="text-center">注册时间 </th>
  52. <th class="text-center">权限 </th>
  53. </tr>
  54. <tr th:each="user:${users}" class="text-center" style="height: 40px">
  55. <td> <span th:text="${user.id}"> </span> </td>
  56. <td> <span th:text="${user.username}"> </span> </td>
  57. <td> <span th:text="${user.password}"> </span> </td>
  58. <td> <span th:text="${user.telephone}"> </span> </td>
  59. <td> <span th:text="${#dates.format(user.registerTime, 'yyyy-MM-dd hh:mm:ss')}"> </span> </td>
  60. <td> <span th:text="${user.popedom==0?'管理员':'普通用户'}"> </span> </td>
  61. </tr>
  62. </table>
  63. </div>
  64. <div class="panel-footer text-right">
  65. <span class="panel-title">酒城工作室@2107 </span>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. </div>
  71. </body>
  72. </html>

启动程序,访问http://localhost:8080/showAllUsers

 

说明:左边面板里是卡片格式显示用户信息,右边面板是表格形式显示用户信息。

 

课堂练习:修改showAllUser.html代码,实现如下显示效果。


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8"/>
  5. <title>显示全部用户 </title>
  6. <meta charset="UTF-8"/>
  7. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  8. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  9. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  10. rel= "stylesheet"/>
  11. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  12. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  13. </head>
  14. <body>
  15. <div class="container">
  16. <div class="row">
  17. <div class="col-md-5">
  18. <div class="panel panel-primary">
  19. <div class="panel-heading text-center">
  20. <span class="panel-title">全部用户信息 </span>
  21. </div>
  22. <div class="panel-body">
  23. <ul class="list-group">
  24. <li class="list-group-item" th:each="user:${users}">
  25. <table class="table-bordered" style="width: 100%">
  26. <tr>
  27. <th>编号 </th>
  28. <td> <span th:text="${user.id}"> </span> </td>
  29. </tr>
  30. <tr>
  31. <th>用户名 </th>
  32. <td> <span th:text="${user.username}"> </span> </td>
  33. </tr>
  34. <tr>
  35. <th>密码 </th>
  36. <td> <span th:text="${user.password}"> </span> </td>
  37. </tr>
  38. <tr>
  39. <th>电话 </th>
  40. <td> <span th:text="${user.telephone}"> </span> </td>
  41. </tr>
  42. <tr>
  43. <th>注册时间 </th>
  44. <td> <span th:text="${#dates.format(user.registerTime, 'yyyy-MM-dd hh:mm:ss')}"> </span> </td>
  45. </tr>
  46. <tr>
  47. <th>权限 </th>
  48. <td> <span th:text="${user.popedom==0?'管理员':'普通用户'}"> </span> </td>
  49. </tr>
  50. </table>
  51. </li>
  52. </ul>
  53. </div>
  54. <div class="panel-footer text-right">
  55. <span class="panel-title">酒城工作室@2107 </span>
  56. </div>
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. </body>
  62. </html>

11、在application.properties里添加属性

 

12、创建welcome.html(访问带参数的消息)


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>欢迎 </title>
  5. <meta charset="UTF-8"/>
  6. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  7. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  8. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  9. rel= "stylesheet"/>
  10. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  11. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="col-md-5">
  17. <div class="panel panel-primary">
  18. <div class="panel-heading text-center">
  19. <span class="panel-title">泸州职业技术学院 </span>
  20. </div>
  21. <div class="panel-body">
  22. <span th:text="#{lzy.welcome(${name})}"> </span>
  23. </div>
  24. <div class="panel-footer text-right">
  25. <span class="panel-title">酒城工作室@2107 </span>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </body>
  32. </html>

12、在HomeController里添加请求映射方法

 

启动程序,访问http://localhost:8080/welcome

 

13、创建testThymeleafObjects.html


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>测试thymeleaf对象 </title>
  5. <meta charset="UTF-8"/>
  6. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  7. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  8. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  9. rel= "stylesheet"/>
  10. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  11. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="col-md-10">
  17. <div class="panel panel-primary">
  18. <div class="panel-heading text-center">
  19. <span class="panel-title">测试Thymeleaf对象 </span>
  20. </div>
  21. <div class="panel-body">
  22. #ctx.#vars: <br/>
  23. <textarea th:text="${#ctx.#vars}" rows="10" cols="150"> </textarea> <br/>
  24. 喜欢看的书: <span th:text="${#vars.book}"> </span> <br/>
  25. 喜欢的城市: <span th:text="${#httpSession.getAttribute('city')}"> </span> <br/>
  26. 你的国家: <span th:text="${#locale.country}+'-'+${#locale.getDisplayCountry()}"> </span> <br/>
  27. 你的母语: <span th:text="${#locale.language}+'-'+${#locale.getDisplayLanguage()}"> </span> <br/>
  28. <hr/>
  29. 时间: <span th:text="${#dates.format(#dates.createNow())}"> </span> <br/>
  30. 收入: <span th:text="'¥'+${#numbers.formatDecimal(2345.5645345, 3, 2)}"> </span> <br/>
  31. </div>
  32. <div class="panel-footer text-right">
  33. <span class="panel-title">酒城工作室@2107 </span>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </body>
  40. </html>

14、在HomeController里添加请求映射方法

 

启动程序,访问http://localhost:8080/test

 

15、在pom.xml文件添加yaml的依赖


 
  1. <dependency>
  2. <groupId>org.yaml </groupId>
  3. <artifactId>snakeyaml </artifactId>
  4. <version>1.18 </version>
  5. </dependency>

16、在resources里创建application.yaml


 
  1. server:
  2. port: 8080
  3. serverHost:
  4. inetAddressA:
  5. ip: 127 .0 .0 .1
  6. length: 160
  7. port: 2000
  8. inetAddressB:
  9. ip: 192 .168 .0 .15
  10. length: 180
  11. port: 2000
  12. inetAddressB:
  13. ip: 192 .168 .0 .16
  14. length: 288
  15. port: 2000
  16. udp:
  17. server:
  18. host: 192 .168 .60 .34
  19. port: 8001

17、创建ServerHostProperties类


 
  1. package net.hw.properties;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.stereotype.Component;
  4. /**
  5. * Created by howard on 2017/4/27.
  6. */
  7. @Component
  8. @ConfigurationProperties( "serverHost")
  9. public class ServerHostProperties {
  10. private InetAddress inetAddressA;
  11. private InetAddress inetAddressB;
  12. private InetAddress inetAddressC;
  13. public static class InetAddress {
  14. private String ip;
  15. private int length;
  16. private int port;
  17. public String getIp() {
  18. return ip;
  19. }
  20. public void setIp(String ip) {
  21. this.ip = ip;
  22. }
  23. public int getLength() {
  24. return length;
  25. }
  26. public void setLength(int length) {
  27. this.length = length;
  28. }
  29. public int getPort() {
  30. return port;
  31. }
  32. public void setPort(int port) {
  33. this.port = port;
  34. }
  35. }
  36. public InetAddress getInetAddressA() {
  37. return inetAddressA;
  38. }
  39. public void setInetAddressA(InetAddress inetAddressA) {
  40. this.inetAddressA = inetAddressA;
  41. }
  42. public InetAddress getInetAddressB() {
  43. return inetAddressB;
  44. }
  45. public void setInetAddressB(InetAddress inetAddressB) {
  46. this.inetAddressB = inetAddressB;
  47. }
  48. public InetAddress getInetAddressC() {
  49. return inetAddressC;
  50. }
  51. public void setInetAddressC(InetAddress inetAddressC) {
  52. this.inetAddressC = inetAddressC;
  53. }
  54. }

18、在webmvc子包里创建ServerHostController


 
  1. package net.hw.webmvc;
  2. import net.hw.properties.ServerHostProperties;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.ui.Model;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * Created by howard on 2017/4/27.
  11. */
  12. @ Controller
  13. public class ServerHostController {
  14. @ Autowired
  15. private ServerHostProperties serverHostProperties;
  16. @ RequestMapping( "/showServerHost")
  17. public String serverHost( Model model) {
  18. List< ServerHostProperties. InetAddress> inetAddresses = new ArrayList< ServerHostProperties. InetAddress>();
  19. inetAddresses.add(serverHostProperties.getInetAddressA());
  20. inetAddresses.add(serverHostProperties.getInetAddressB());
  21. inetAddresses.add(serverHostProperties.getInetAddressC());
  22. model.addAttribute( "inetAddresses", inetAddresses);
  23. return "showServerHost";
  24. }
  25. }

19、在templates里创建showServerHost.html

 


 
  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <title>显示服务器主机信息 </title>
  5. <meta charset="UTF-8"/>
  6. <meta name="viewport" content="width=device-width, initial-scale=1"/>
  7. <link th:href="@{/bootstrap/css/bootstrap.css}" href="../static/bootstrap/css/bootstrap.css" rel="stylesheet"/>
  8. <link th:href="@{/bootstrap/css/bootstrap-theme.css}" href="../static/bootstrap/css/bootstrap-theme.css"
  9. rel= "stylesheet"/>
  10. <script th:src="@{/scripts/jquery-3.1.1.min.js}" src="../static/scripts/jquery-3.1.1.min.js"> </script>
  11. <script th:src="@{/bootstrap/js/bootstrap.js}" src="../static/bootstrap/js/bootstrap.js"> </script>
  12. </head>
  13. <body>
  14. <div class="container">
  15. <div class="row">
  16. <div class="col-md-5">
  17. <div class="panel panel-primary">
  18. <div class="panel-heading text-center">
  19. <span class="panel-title">服务器主机信息 </span>
  20. </div>
  21. <div class="panel-body">
  22. <ul class="list-group">
  23. <li class="list-group-item" th:each="inetAddress:${inetAddresses}">
  24. <div th:if="${inetAddressStat.count==1}">
  25. <p style="font-weight: bold">inetAddressA </p>
  26. </div>
  27. <div th:if="${inetAddressStat.count==2}">
  28. <p style="font-weight: bold">inetAddressB </p>
  29. </div>
  30. <div th:if="${inetAddressStat.count==3}">
  31. <p style="font-weight: bold">inetAddressC </p>
  32. </div>
  33. ip: <span th:text="${inetAddress.ip}"> </span> <br/>
  34. length: <span th:text="${inetAddress.length}"> </span> <br/>
  35. port: <span th:text="${inetAddress.port}"> </span> <br/>
  36. </li>
  37. </ul>
  38. </div>
  39. <div class="panel-footer text-right">
  40. <span class="panel-title">酒城工作室@2107 </span>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </body>
  47. </html>

20、启动程序,访问http://localhost:8080/showServerHost

 

五、小结

采用Thymeleaf模板引擎,关键要注意动态数据与静态数据,一个用于服务器端数据的获取,一个用于客户端数据的显示,前端与后端很好地分离来处理,即使没有启动程序,也能在客户端看到页面的样式效果。通过案例学会正确使用Thymeleaf的各种表达式得到预期的动态数据,呈现在模板页面上。

初探Thymeleaf模板引擎 Thymealeaf模板引擎入门 一、Thymeleaf简述 简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点: 1、Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它... 阅读详情

相关推荐

【Spring Boot】Thymeleaf模板引擎 — Thymeleaf入门

Thymeleaf是一款非常优秀的服务器端页面模板引擎,适用于Web和独立环境,具有丰富的标签语言和函数,能够处理HTML、XML、JavaScript甚至文本。Thymeleaf相较于其他模板引擎更加优雅。它强调自然模板化(允许模板成为工作原型,而Velocity、FreeMarker模板不允许这样做),所以它的语法更干净,更符合当前Web开发的趋势。

衍生星球的博客 2947

Thymeleaf实用实例

文章目录1. 简介2. 开始3. list遍历4. map遍历5. if unless switch6. @7. th:object8. 工具方法9. include replace insert10. 处理本地文件 1. 简介 之前一直使用Freemarker,对Thymeleaf了解但是不熟悉,最近因为其他项目组他们要快速搭建后台,使用了一个三方的框架用到了Thymeleaf,所以进一步了解了一些。 发现Thymeleaf更加像前端的模板语言,所以对静态页面有更好的兼容性,就是,如果是Freemarke

trayvontang的博客 1498

springMVC 中 使用 Thymeleaf 模板引擎项目入门

一、首先创建一个简单的 maven 项目 springmvc-thymeleaf 作为父项目,删除 src 目录,之后再创建一个 子模块 springmvc-thymeleaf-demo,具体流程不再叙述。 二、在子模块中导入项目所需 jar 包(当然你也可以把一些所有模块都需要的 jar 包导入写在 父项目的 pom.xml 文件中),我这里就直接放在子模块的 pom.xml 中。 <?xml version="1.0" encoding="UTF-8"?> <project xmln

TyuIn的博客 1215

Springboot整合Thymeleaf 模板引擎(案例)- 语法详解

Thymeleaf是一个流行的模板引擎,是基于HTML的,语法应用在HTML标签中。该模板引擎采用java语言开发。

qq_60506984的博客 4405

Kindle电子书DRM破解全攻略:从下载到转换的完整流程(含最新DeDRM插件配置)

本文详细介绍了如何合法移除个人购买的Kindle电子书的DRM保护,实现跨设备阅读与备份。核心内容包括使用最新DeDRM插件(如noDRM分支)配合Calibre进行配置,并针对亚马逊最新的KFX加密格式提供了密钥提取与实战操作指南。文章强调该方法仅适用于个人合理使用,旨在帮助读者真正掌控自己的数字资产。

delta的博客 576

Unity PSD分层导入插件ps2d原理与实战指南

PSD作为UI设计主流源文件,其图层结构、混合模式、文字矢量属性等核心语义在传统引擎导入中普遍丢失,导致设计还原度低、多语言适配难、运行时不可控。ps2d通过深度解析PSD二进制规范,将图层映射为GameObject树、文字转为TextMeshPro可编辑组件、形状生成矢量Mesh、蒙版与图层样式编译为定制Shader,实现设计意图的精准程序化表达。该方案显著提升UI开发效率,支撑高保真还原、动态修改、分辨率自适应及运行时热重载等工程需求,是Unity中连接设计师与程序员的关键工作流中枢。

weixin_32943857的博客 348

Thymealeaf基础

什么是thymealeaf 为什么需要 使用 插入坐标 <!-- Thymeleaf --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf</artifactId> <version>3.0.11.RELEASE</version> </dependency> ...

sinat_33940108的博客 502

【Verilog】valid-ready双向握手机制 ——很绕但是很有意思

valid-ready双向握手机制。

GalaxyerKw的博客 1万+

Thymeleaf 模板引擎入门与进阶指南

Thymeleaf 是一种现代的服务器端模板引擎,由 Daniel Fernández 和 David Hernández 于 2010 年创建。随着 Java Web 开发的不断发展,Thymeleaf 凭借其静态原型化、HTML5 兼容性和强大的表达式语言,逐渐成为许多开发者首选的模板引擎。模板片段是 Thymeleaf 的一大亮点,允许开发者将常用的部分提取出来复用,避免重复代码。将业务逻辑放在后端,仅在模板中处理展示逻辑。Thymeleaf 可用于生成动态报表,支持表格、图表等内容的渲染。

1584

语义分割(Semantic Segmentation)近年发展综述

目标:为图像中每个像素分配一个语义类别标签(如“道路”、“行人”、“天空”),不区分同类实例。关键挑战精确边界恢复(高空间分辨率)全局上下文理解(大感受野)计算效率与精度平衡小样本/弱监督泛化语义分割的十年演进,是一条从专用→通用、封闭→开放、慢速→实时、像素→语义2015:FCN/U-Net 奠基;2018:DeepLabv3+/PSPNet 实现高精度;2020:HRNet+OCR 登顶竞赛;2021:SegFormer 开启 Transformer 新纪元;2023+

浩瀚之水的专栏 978

安装 miniconda linux

安装 miniconda linux

jacke121的专栏 1万+

Thymeleaf模板引擎快速入门

一、概念 二、快速入门 (1)创建测试无骨架maven工程,引入依赖 <?xml version="1.0" encoding="UTF-8"?> <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/PO

QAQ 342

ROSTCM6+Gephi的网络语义分析详细教程(附案例实战)

ROSTCM6是武汉大学沈阳教授研发编码的国内目前唯一的以辅助人文社会科学研究的大型免费社会计算平台。该软件可以实现微博分析、聊天分析、全网分析、网站分析、浏览分析、分词、词频统计、英文词频统计、流量分析、聚类分析等一系列文本分析。Gephi是一款开源免费跨平台基于JVM的复杂网络分析软件,可用于各种图形和网络的可视化和探索,是最受欢迎的网络可视化软件之一。

m0_64336780的博客 1万+

前端模板引擎Thymeleaf快速入门

文章目录1. Thymeleaf特点2. 提供数据3. 引入启动器4. 静态页面5. 测试6.模板缓存 1. Thymeleaf特点 简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较于其他的模板引擎,它有如下四个极吸引人的特点: 动静结合:Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html

我真的很不错的博客 2473

多后端系统统一聚合与智能分流:用 Multi Origin Composition 与 Routing 打造一套可扩展的 SAP Gateway 架构

本文针对企业级SAP集成场景,详细介绍了如何利用SAP Gateway的MOC(多源组合)和Routing功能实现多后端系统的智能路由与数据聚合。通过系统别名配置、用户角色绑定和主机名分流等机制,可实现按用户、按域名的精确路由。文章还提供了配置清单、性能优化建议和常见问题解决方案,帮助工程师落地多后端OData服务的最佳实践。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 186

学Simulink——基于直流电机的建模与控制场景实例:他励直流电机的起动与制动过程仿真

直流电机在工业控制、电动车辆、自动化设备中广泛应用。其中,他励直流电机(Separately Excited DC Motor)因其励磁绕组与电枢绕组分别供电,具有良好的调速性能和启动特性,常用于需要平稳启动和精确控制的场合。在实际应用中,电机的启动过程和制动过程对电网、机械系统以及电机本身都具有重要影响。因此,仿真分析他励直流电机的启动与制动特性,是电机控制设计的重要环节。本文将详细介绍如何使用构建一个他励直流电机的启动与制动过程仿真模型,并通过仿真分析其动态响应。本文详细介绍了如何使用构建一个。

xiaoheshang_123的博客 227

Spring Boot 整合 Thymeleaf模板引擎 Thymeleaf入门

Spring Boot 整合 Thymeleaf Thymeleaf简介 1.Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。相较与其他的模板引擎,它有如下三个极吸引人的特点 2.Thymeleaf 在有网络和无网络的环境下皆可运行,即 它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。 3....

yufire的博客 437

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

Spring Boot 模板引擎-Thymeleaf入门

这里写自定义目录标题 ##模板引擎 1、市面上主流的 Java 模板引擎有:JSP、Velocity、Freemarker、Thymeleaf 2、JSP 本质也是模板引擎,Spring Boot 官方推荐使用 “Thymeleaf”模板引擎 3、模板引擎的作用都是将模板(页面)和数据进行整合然后输出显示,区别在于不同的模板使用不同的语法,如 JSP 的JSTL表达式,以及J SP 自己的表达式和...

weixin_43687944的博客 201

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

themleft模板库_thymeleaf模板引擎入门

ThymeLeaf是什么Thymeleaf是一个用于服务器端的java模板引擎,它使用简单但功能强大,目前可以处理的模板类型包括:HTML、XML、TEXT、JavaScript、CSS等。搭建thymeleaf开发环境首先创建一个Maven web项目,pom文件依赖信息如下:junitjunit3.8.1testorg.thymeleafthymeleaf3.0.7.RELEASEjavax....

weixin_39745345的博客 318

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

springboot使用thymeleaf模板引擎之入门

在springboot中使用thymeleaf的入门介绍

qq_39746820的博客 197

thymeleaf模板引擎入门

ThymeLeaf是什么 Thymeleaf是一个用于服务器端的java模板引擎,它使用简单但功能强大,目前可以处理的模板类型包括:HTML、XML、TEXT、JavaScript、CSS等。 搭建thymeleaf开发环境 首先创建一个Maven web项目,pom文件依赖信息如下: dependencies> dependency> groupId>ju

anran334455的博客 179

Thymeleaf模板引擎从入门到实战:Spring Boot整合与核心用法详解

本文全面介绍Thymeleaf模板引擎的核心功能与Spring Boot整合实践,涵盖数据绑定、条件循环、表单处理等关键特性。通过代码示例演示从基础语法到文件模板的进阶使用,详解Spring Boot自动配置原理及常用配置项,并给出Controller与模板交互的完整开发流程,帮助开发者快速掌握现代化Java Web页面渲染技术。

m0_73941339的博客 2698

模板引擎于Thymeleaf入门实操

1.模板文件在resources下的templates, eg:success.html <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>模板文件</title> </head> <body> <h2>成功<

grow_的博客 136

Thymeleaf 模板引擎入门与实践

Thymeleaf 是一个现代化的服务器端 Java 模板引擎,适用于 Web 和独立环境。它能够处理 HTML、XML、JavaScript、CSS 甚至纯文本。Thymeleaf 的主要目标是提供一种优雅且高度可维护的模板创建方式。与 JSP 等传统技术相比,Thymeleaf 的模板是纯 HTML 文件,可以直接在浏览器中打开和预览,这为前后端协作和原型设计带来了极大的便利。

2301_80954266的博客 80

Thymeleaf 模板引擎入门案例 结合 SpringBoot框架

Thymeleaf 模板引擎入门案例 结合 SpringBoot框架 开发,一般禁用thymeleaf的缓存 spring.thymeleaf.cache=false Controller : Thymeleaf默认会拼接字符串 list.html @Controller public class EmployeeController { //查询所有员工返回列表页面 public String list() { //thymeleaf默认...

zhouth94的博客 321
上一篇: Android 拍身份证(自定义相机)
下一篇: 基于大数据体系构建数据仓库——第一节——认识数据仓库
Summer_Chun
博客等级 码龄7年 3粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值