SpringBoot 7.SpringBoot 结合 Thymeleaf
一、引入 Thymeleaf 依赖
<!-- Spring boot - thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 引入此依赖,再设置 spring.thymeleaf.mode=LEGACYHTML5,可以解决 thymeleaf 严格模式问题 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
二、在 application.properties 中对 Thymeleaf 进行配置
# Thymeleaf
spring.thymeleaf.prefix=/WEB-INF/views/
spring.thymeleaf.suffix=.html
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
设置后台返回视图url的前缀、后缀、以及 contentType
设置thymeleaf是否缓存以及其模式(HTML 和 LEGACYHTML5)。
当我们使用 HTML 模式的时候,你在 thymeleaf 模板的 html 中如果使用了没有闭合的 dom 标签,那么他就会报错,因此需要加入上述所说的 nekohtml 依赖,以及设置对应的 mode为 LEGACYHTML5。
三、创建 index.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="ChengRuan" /> <title>title</title>
</head>
<body>
<h1>Hello RCDDUP!!!</h1>
</body>
</html>
四、创建 PageController.java
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="ChengRuan" /> <title>title</title>
</head>
<body>
<h1 th:text="'Hello RCDDUP!!!'"></h1>
</body>
</html>
五、启动 App.java
由于我将 server.port设置为9090,所以启动的url为:http://localhost:9090/index

好了,我们成功使用了 Thymeleaf。
六、解决静态资源(css、js等)问题
在 CustomWebMvcConfigurer.java 中重写 addResourceHandlers() 方法。
可以简写为:registry.addResourceHandler("/images/**","/css/**","/js/**").addResourceLocations("/images/","/css/","/js/");
package org.rcddup.app.config; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration
@MapperScan(basePackages = { "org.rcddup.app.dao" })
public class CustomWebMvcConfigurer extends WebMvcConfigurerAdapter {
// 静态资源处理
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/images/**").addResourceLocations("/images/");
registry.addResourceHandler("/css/**").addResourceLocations("/css/");
registry.addResourceHandler("/js/**").addResourceLocations("/js/");
super.addResourceHandlers(registry);
} }
1.创建 main.css

然后我们访问:http://localhost:9090/css/main.css

在 index.html 中引入 main.css
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="ChengRuan" /> <link href="/css/main.css" rel="stylesheet"> <title>title</title>
</head>
<body>
<h1 th:text="'Hello RCDDUP!!!'"></h1>
</body>
</html>
然后我们访问 index 页面,http://localhost:9090/index

这样我们就解决了静态资源的问题,可以很好的在 html 中引入你的静态资源了。
SpringBoot 7.SpringBoot 结合 Thymeleaf的更多相关文章
- SpringBoot整合Jsp和Thymeleaf (附工程)
前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...
- java框架之SpringBoot(4)-资源映射&thymeleaf
资源映射 静态资源映射 查看 SpringMVC 的自动配置类,里面有一个配置静态资源映射的方法: @Override public void addResourceHandlers(Resource ...
- SpringBoot Web开发(4) Thymeleaf模板与freemaker
SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...
- SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二
SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 方法一 使用原生sql查询 或者 为方法名增加 ...
- SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一
SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...
- springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据
springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...
- Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)
介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...
- 【SpringBoot】SpringBoot 国际化(七)
本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...
- 【SpringBoot】SpringBoot配置与单元测试(二)
SpringBoot项目创建参考[SpringBoot]SpringBoot快速入门(一) 本文介绍SpringBoot项目的POM文件.配置与单元测试 POM文件 1.SpringBoot的pom文 ...
- SpringBoot(四) -- SpringBoot与Web开发
一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资 ...
随机推荐
- 简单的表格json控件
简单的表格json控件 由于最近做的项目一直有表格的形式展示数据,所以想写个简单的关于表格方面的控件出来,想用JSON数据直接渲染出来,因为开发给到我们前端的字段可能会叫不同的名字,所以我们前端渲染页 ...
- PAT B1022 D进制的A+B (20 分)
输入两个非负 10 进制整数 A 和 B (≤),输出 A+B 的 D (1)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+B 的 D 进制数. 输入 ...
- sparse linear regression with beta process priors
虽然翻译水平有限,但是看原文虽然看得懂,但是在词汇的问题上,会导致看了后面忘了前面,所以先蹩脚的翻译成中文,然后在仔细思考论文的思想(当然不能翻译成中文就不看英文原本了,得两者一起看,这样不会丢失前面 ...
- 跟我学Android NDK开发(一)
Android NDK 开发跟其它开发一样,首先需要配置好开发环境,本文以 Ubuntu系统为例介绍如何进行 Android NDK 开发环境的配置. 1. 简介 什么是 Android NDK 呢? ...
- POJ 2965&&1753
最近由于复习备考(然而考得还是很炸),很久没打题目了.现在开始刷寒假作业,不得不搞POJ 话说没有中文真的好烦啊! 先看1753 题目大意是说在一个4*4的格子中有黑白两色的棋子,你可以翻动其中的棋子 ...
- arm学习——有关位操作的总结
在学习arm的过程中,感觉寄存器,基本不会提供位操作,而是整体的操作, 整体操作的就是要注意在对某位赋值的时候不要影响到其他位,看上去不简单, 其实,整体操作有技巧, 那么就来总结一下: 1.首先要理 ...
- 51nod 小朋友的笑话
链接 分析: 每次操作把以前没有出现这个数的设为1,有这个数的设为0.首先将当前区间设为1,考虑有set维护这个颜色出现的区间,然后把所有与当前区间相交的拿出来,修改为0. 复杂度?每次操作的线段只会 ...
- mybatis 异常 too many connections 解决方案 mysql
参考: https://blog.csdn.net/u011628250/article/details/54017481 https://www.cnblogs.com/baby123/p/5710 ...
- REST-framework快速构建API--频率
前面已经了解了API的认证和授权.认证,是对资源访问者的第一道门,必须有钥匙,你才能进来拿我的资源:授权,是对资源访问者的第二道门,虽然你进来了,但是你可以拿走什么资源,还是我说了算,就是授权. 当然 ...
- SonarQube 平台搭建代码审查平台步骤
SonarQube 平台1.下载包,安装启动2.在sonar.properties 配置mysql数据库的sonar.jdbc.username=sonarsonar.jdbc.password=so ...