原文链接

关于Thymeleaf的优点,我只说一条:它就是html页面啊,直接可以用浏览器打开。受够了JSP的同学可以尝试一下。

1.在pom.xml文件中添加依赖:

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

spring-boot-starter-thymeleaf下面已经包括了spring-boot-starter-web,所以可以把spring-boot-starter-web的依赖去掉。

2.配置属性。其实完全可以使用不用配置,但是Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行吧:

spring.thymeleaf.cache=false

3.编写Controller类。

package com.yws710.springboot.demo1.controller;

import com.yws710.springboot.demo1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; @Controller
@RequestMapping("/user")
public class UserController { @Autowired
UserService userService; @RequestMapping("/list2")
public String userList2(Model model) throws Exception {
model.addAttribute("hello","Hello, Spring Boot!");
model.addAttribute("userList", userService.userList());
return "/user/list2";
}
}

4.创建Thymeleaf模板文件。在classpath:resources下创建一个名为templates的文件夹,在templates的子文件夹user中创建一个名为list2.html的文件。

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>用户列表</title>
<link href="/css/main.css" rel="stylesheet" />
</head>
<body>
<h1 th:text="${hello}">Hello, Spring Boot!</h1>
<table>
<tr>
<th>ID</th>
<th>姓名</th>
<th>生日</th>
<th>薪资</th>
</tr>
<tr th:each="user : ${userList}">
<td th:text="${user.id}">0</td>
<td th:text="${user.name}">zhansan</td>
<td th:text="${user.birthday}">1988-06-01</td>
<td th:text="${user.salary}">12345</td>
</tr>
</table> <select>
<option th:each="user:${userList}" th:value="${user.id}" th:text="${user.name}">我是默认值</option>
</select> </body>
</html>

需要注意的是,模板文件中的所有标签都需要闭合哦。meta标签需要这么写:<meta charset="UTF-8" />或者<meta charset="UTF-8"></meta>。但是,难免有疏忽的时候,那怎么办?使用3版本吧。只需要在pom.xml文件中增加如下代码:

    <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- set thymeleaf version -->
<thymeleaf.version>3.0.0.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>

这样就不用担心单标签关闭的问题了,更符合书写习惯。

5.启动项目,查看运行结果:

作为对比,直接在浏览器中打开list2.html看看会怎样?

很遗憾,样式文件没加载到。模板文件中是这样写的:

<link href="/css/main.css" rel="stylesheet" />
但是样式文件是在classes/static/css中呢。淡淡的忧伤。
使用如下方式即可很好地解决上述问题。
<link href="../../static/css/main.css" th:href="@{/css/main.css}" rel="stylesheet" />

Spring Boot入门第四天:使用Thymeleaf模板引擎的更多相关文章

  1. Spring Boot入门第五天:使用JSP

    原文链接 1.在pom.xml文件中添加依赖: <!-- Servlet依赖 --> <dependency> <groupId>javax.servlet< ...

  2. Spring Boot入门第三天:配置日志系统和Druid数据库连接池。

    原文链接 一.日志管理 1.在application.properties文件中加入如下内容: logging.level.root=WARN logging.level.org.springfram ...

  3. Spring Boot2(五):使用Spring Boot结合Thymeleaf模板引擎使用总结

    一.Thymeleaf概述 一般来说,常用的模板引擎有JSP.Velocity.Freemarker.Thymeleaf . SpringBoot推荐的 Thymeleaf – 语法更简单,功能更强大 ...

  4. spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法

    spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...

  5. Spring Boot 启动(四) EnvironmentPostProcessor

    Spring Boot 启动(四) EnvironmentPostProcessor Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698. ...

  6. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  7. spring boot: thymeleaf模板引擎使用

    spring boot: thymeleaf模板引擎使用 在pom.xml加入thymeleaf模板依赖 <!-- 添加thymeleaf的依赖 --> <dependency> ...

  8. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  9. Spring Boot 2.X(四):Spring Boot 自定义 Web MVC 配置

    0.准备 Spring Boot 不仅提供了相当简单使用的自动配置功能,而且开放了非常自由灵活的配置类.Spring MVC 为我们提供了 WebMvcConfigurationSupport 类和一 ...

随机推荐

  1. LVS+Keepalived+Mysql+主主数据库架构[2台]

    架构图 安装步骤省略. 158.140 keepalived.conf ! Configuration File for keepalived global_defs { #全局标识模块 notifi ...

  2. LINUX学习 - 磁盘分区 + 开机自动挂载 + 性能测试

    在看鸟哥linux私房菜,发现不少有趣的东西,真是相见恨晚. 情境:建立一个新的filesystem挂在/srv/myproject目录下,并让其开机自动挂载到目录,该目录给project群组共享,其 ...

  3. 如何利用好github的问题

    github对我来说真的是一个超好的平台,不过之前只是把它仓库来使用, 后来在大佬告诉我应该怎么使用github,今天就来总结下如何利用好github,让它发挥最大的威力. 1.把github当做百科 ...

  4. bzoj 2936 [Poi 1999] 降水 - 并查集

    题目传送门 需要root权限的传送门 题目大意 有一个$n\times m$的网格图,每一格都有一个高度.一次降雨过后问最多能积多少水. 考虑算每一高度能储存的水的量. 如果小于等于这个高度的格子和边 ...

  5. Error: php71w-common conflicts with php-common-5.4.16-46.el7.x86_64

    Centos7.3 阿里云主机 安装zabbix报错 问题: [root@lvhanzhi code]# yum install -y zabbix-web-mysql zabbix-agent ma ...

  6. mysql 的delete from 和update子查询限制

    最经做项目时发现的问题,好像在update时也有... 网上查到的资料如下: 1.使用mysql进行delete from操作时,若子查询的 FROM 字句和更新/删除对象使用同一张表,会出现错误. ...

  7. 从0开始安装fedora23的笔记-- 以及使用fedora的常规问题

    关于安装过程中的笔记本 触摸板 和鼠标的问题? 在安装过程中, 系统只认从usb接口插入的鼠标和触摸板上的 "左键和右键"按钮. 触摸板上的鼠标只支持"移动"的 ...

  8. SpringBoot 使用validation数据校验

    后端对数据进行验证 添加包 hibernate-validator <!-- https://mvnrepository.com/artifact/org.hibernate.validator ...

  9. SQL优化参考

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  10. 深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods)

    深度学习课程笔记(十三)深度强化学习 --- 策略梯度方法(Policy Gradient Methods) 2018-07-17 16:50:12 Reference:https://www.you ...