Thymeleaf模板引擎入门

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的各种表达式得到预期的动态数据,呈现在模板页面上。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值