一、引入 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的更多相关文章

  1. SpringBoot整合Jsp和Thymeleaf (附工程)

    前言 本篇文章主要讲述SpringBoot整合Jsp以及SpringBoot整合Thymeleaf,实现一个简单的用户增删改查示例工程.事先说明,有三个项目,两个是单独整合的,一个是将它们整合在一起的 ...

  2. java框架之SpringBoot(4)-资源映射&thymeleaf

    资源映射 静态资源映射 查看 SpringMVC 的自动配置类,里面有一个配置静态资源映射的方法: @Override public void addResourceHandlers(Resource ...

  3. SpringBoot Web开发(4) Thymeleaf模板与freemaker

    SpringBoot Web开发(4) Thymeleaf模板与freemaker 一.模板引擎 常用得模板引擎有JSP.Velocity.Freemarker.Thymeleaf SpringBoo ...

  4. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 方法一 使用原生sql查询 或者 为方法名增加 ...

  5. SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一

    SpringBoot 使用JPA+MySQL+Thymeleaf 总结 一 SpringBoot 使用JPA+MySQL+Thymeleaf 总结 二 pom引用 <?xml version=& ...

  6. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

  7. Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)

    介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...

  8. 【SpringBoot】SpringBoot 国际化(七)

    本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...

  9. 【SpringBoot】SpringBoot配置与单元测试(二)

    SpringBoot项目创建参考[SpringBoot]SpringBoot快速入门(一) 本文介绍SpringBoot项目的POM文件.配置与单元测试 POM文件 1.SpringBoot的pom文 ...

  10. SpringBoot(四) -- SpringBoot与Web开发

    一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资 ...

随机推荐

  1. jqgrid 分页时,清空原表格数据加载返回的新数据

    由于,我们是动态分页,分页后的数据是在触发分页后动态加载而来.如何使jqgrid清空原数据而加载新数据? 1)调用jqgrid的 clearGridData 方法清空表格数据 2)调用jqgrid的  ...

  2. STM32驱动ILI9341控制器控制TFTLCD显示

    STM32驱动ILI9341控制器控制TFTLCD显示 一.用STM32控制TFTLCD显示的编程方法,在编程驱动TFTLCD液晶显示器之前,我们先熟悉以下概念: 1.色彩深度,这是一个与TFTLCD ...

  3. 【stylus】stylus在webstrom中的识别

    <style lang="stylus" rel="stylesheet/stylus"> @import './common/stylus/mix ...

  4. python基础2之字符串、列表、字典、集合

    内容概要: 一.python2 or 3 二.字符串拼接 三.字符串 四.列表.元祖 五.字典 六.集合 七.练习 一.python2 or python3 目前大多使用python2.7,随着时间的 ...

  5. 20155217《网络对抗》Exp07 网络欺诈防范

    20155217<网络对抗>Exp07 网络欺诈防范 实践内容 简单应用SET工具建立冒名网站 ettercap DNS spoof 结合应用两种技术,用DNS spoof引导特定访问到冒 ...

  6. 带Alpha通道的色彩叠加问题

    css3的rgba色彩模式.png/gif图片的alpha通道.canvas的rgba色彩模式.css3的阴影.css3的opacity属性等等,这些应用在网页中,有意无意间,我们的页面多了许多半透明 ...

  7. vs如何将工程配置,保存到属性表

    上次讲到新建一个opencv工程的配置过程,整个流程下来还是非常麻烦的.每次新建一个工程都要走这个流程的话就要疯了! 现在介绍一种将工程配置,保存到属性表的方法,那么下次新建工程时,只要添加这个属性表 ...

  8. 【Qt】QOpenGLWidget展示蒙版效果

    关键代码是派生QOpenGLWidget,覆写paintEvent函数 QPainter p; p.begin(this); p.drawImage(QPoint(, ), m_Img); QLine ...

  9. [HNOI2018]转盘[结论+线段树]

    题意 题目链接 分析 首先要发现一个结论:最优决策一定存在一种 先在出发点停留之后走一圈 的情况,可以考虑如下证明: 如果要停留的话一定在出发点停留,这样后面的位置更容易取到. 走超过两圈的情况都可以 ...

  10. HTML-JS 循环 函数 递归

    [循环结构的执行步骤] 1.声明循环变量 2.判断循环条件 3.执行循环体操作 4.更新循环变量 然后,循环执行2-4,直到条件不成立时,跳出循环. while循环()中的表达式,运算结果可以是各种类 ...