原文链接

关于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. Linux使用退格键时出现^H + Tab键命令补全失效/方向键失效 + ls文件夹和文件没有颜色

    删除问题 安装kalilinux使用普通用户的的时候按退格键无法实现删除功能 解决的办法有两个 一改变快捷键: 使用Ctrl+Backspace组合键可以实现删除功能 ctrl + backspace ...

  2. Eclipse中ctrl+shift+r与ctrl+shift+t的区别

    eclipse中的两个常用的快捷键可以大大提升查找文件的效率,分别是: ctrl+shift+r : open resource, 打开资源. 它可以打开当前eclipse的工作区中所有(打开的)工程 ...

  3. 移动端(微信等)使用 vConsole 调试 console

    参考链接:https://blog.csdn.net/m0_37036014/article/details/80113635

  4. 20145221高其_Web安全基础实践

    20145221高其_Web安全基础实践 目录 实践目标 WebGoat BurpSuite Injection Flaws Cross-Site Scripting (XSS) 总结 实践目标 (1 ...

  5. vector at()函数比 []运算符操作安全

    转载:https://blog.csdn.net/chenjiayi_yun/article/details/18507659 []操作符的源码 reference operator[](size_t ...

  6. tp框架中 关于数据库mysql 的一些疑点知识

    mysql创建索引, 通常是在 创建表的 同时/时候, 就创建了 即使是在win下, 用命令行操作数据库 , 也要比 图形界面的鼠标操作快得多 索引的类型有: unique, fulltext索引, ...

  7. extjs的使用笔记

    2006年jack slocum斯洛克姆 基于yui写的扩展前端框架(就是由一些前端可视化组件如表单,树, 表格,等组成的frameset或者叫做 ui engine),叫yui-ext, 后来成熟后 ...

  8. activiti 5.13流程图连线名称不显示bug修复

    使用modeler设计器,流程图连线名称是有显示的,但是运行结果却没显示.找到网上2遍文章,说是activiti框架中的一个bug,要修改 DefaultProcessDiagramGenerator ...

  9. P3605 [USACO17JAN]Promotion Counting晋升者计数

    思路 线段树合并的板子.. 和子节点合并之后在值域线段树上查询即可 代码 #include <cstdio> #include <algorithm> #include < ...

  10. P2522 [HAOI2011]Problem b

    还有三倍经验的吗(窒息) 思路 其实就是P3455套了个简单的容斥 把问题转化成f(n,m,k)-f(a-1,m,k)-f(n,b-1,k)+f(a-1,b-1,k)就可以了 和p3455几乎一样的代 ...