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.编写业务代码 二.静态资 ...
随机推荐
- opencv——对象计数
思路: 1.通过形态学操作.阈值处理.距离变换等方法,使得各个轮廓分开 2.计算轮廓数量 #include <opencv2/opencv.hpp> #include <iostr ...
- ceph存储osd启动异常处理和正常启停操作
机器角色:cloudstack虚拟机的宿主机:ceph存储机器. 事件:ceph存储的物理机器由于内存异常,需要停机更换,仅仅是把该物理机上面的虚拟机迁移走,同时启动了停机维护,然后就直接关机.结果造 ...
- Windows控制程序网站带宽及Qos(TOS或DSCP)
[基于策略的 Qos]位置:gpedit.msc->本地计算机策略->用户配置->Windows 设置->基于策略的 Qos
- Windows10安装vmware和centos7
1. 下载vmware安装包 地址(vmware版本:vmware workstation 14):http://xzc.197746.com/vmware-workstation-full1413. ...
- jQuery对底部导航进行跳转并高亮显示
这两天弄一个mui的底部菜单,有点费时了,尝试了用vue写,纯js写,还有根据mui的写,还是有些问题和麻烦.直到看了网上的一些例子,才想明白,之前一直是一种点击触发事件才高亮的思维去做,这个虽然可以 ...
- 没事做的Delphi版的俄罗斯方块游戏Demo
源代码下载
- C++中冒号的作用
一次偶然机会看见了一种以前没见过的写法,记录下来以后参阅学习. class NoName{ public: NoName():pstring(new std::string), i(0), d(0){ ...
- EZ 2018 05 06 NOIP2018 慈溪中学集训队互测(五)
享受爆零的快感 老叶本来是让初三的打的,然后我SB的去凑热闹了 TM的T2写炸了(去你妹的优化),T1连-1的分都忘记判了,T3理所当然的不会 光荣革命啊! T1 思维图论题,CHJ dalao给出了 ...
- 【php增删改查实例】第五节 - easyUI的基本使用
1. 列表组件 datagrid 1.1 创建一个grid.html <html> <head> <meta charset="utf-8" /> ...
- 汇编 MOVSX与MOVZX 指令
知识点: MOVSX符号扩展传送 MOVZX零扩展传送 一.MOVSX与MOVZX格式 MOVSX 操作数A ,操作数B MOVZX 操作数A ,操作数B 相同点:操作数B 空间必须小于 操作 ...