一、引入 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. Velocity.js初步

    Js越来越强大了,超乎我的想象,以前JS仅仅只能通过ajax与后台交互,后来又有了Node.js,JS可以用于服务端,然后今天我又发现了JS的动态语言.明天呢?也许不少前端的小伙伴会说,慢些吧,慢些吧 ...

  2. Python2.7-codecs

    codecs 自然语言编码转换模块 模块内的主要方法如下: codecs.encode(obj[, encoding[, errors]]):对obj用encoding编码codecs.decode( ...

  3. jpa实例

    ORM框架新的JPA ORM规范:1)JPA为POJO提供持久化标准规范.2)JPA的使用:Hibernate与TopLink以及OpenJpa都提供了JPA的实现.3)JPA主要技术:  A.ORM ...

  4. Android 写一个Activity之间来回跳转的全局工具类(主要是想实现代码的复用)

    废话不多说了,直接上代码,相信大家都能看得懂的. 一.主要工具类 package com.yw.chat.utils; import android.app.Activity; import andr ...

  5. 虚拟机VirtualBox安装MAC OS 10.12图文教程

    VirtualBox虚拟机安装Mac OS 10.12图文教程的准备 1.VirtualBox虚拟机 下载地址:https://www.virtualbox.org/ 特别提醒:推荐官方下载,安装Vi ...

  6. day45

    今日内容 1.css三种引入方式 2.三种引入方式的优先级 3.长度及颜色单位 4.常用样式 5.css选择器 CSS三种引入方式 1.1css引入方式之行间式 ​ 行间式(特点): ​ 1.标签头部 ...

  7. [Baltic 2011]Lamp BZOJ2346

    分析: 建图最短路,比较裸. 我们可以考虑,如果是‘\’那么,左上连右下边权为0,左下连右上边权为1,反之亦然. 卡裸spfa,加点优化能过,我就直接改成的堆优化Dijkstra 附上代码: #inc ...

  8. AndroidStudio怎样导入library项目开源库 - 转

    https://jingyan.baidu.com/article/1974b2898917aff4b1f77415.html

  9. 20155317 王新玮《网络对抗技术》实验5 MSF基础应用

    20155317 王新玮<网络对抗技术>实验5 MSF基础应用 1. MS08_067安全漏洞 原理:攻击者利用受害者主机默认开放的SMB服务端口445,发送特殊RPC请求,通过MSRPC ...

  10. Js读取XML文件为List结构

    习惯了C#的List集合,对于Javascript没有list 极为不舒服,在一个利用Js读取XML文件的Demo中,决定自己构建List对象,将数据存入List. 第一步,Js读取XML文件知识 X ...