spring整合thymeleaf
官方文档入口:https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html
1、首先需要引入thymeleaf的依赖(据官网文档,thymeleaf-spring3与thymeleaf-spring4用法基本一致)
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.11.RELEASE</version>
</dependency>
2、配置thymeleaf的模板解析器、模板引擎与视图解析器
官方文档以xml作为配置方法,因为不是很方便,此处使用java方式进行配置
package com.example.demo.config;
import javax.servlet.ServletContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.context.ServletContextAware;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
@ComponentScan(basePackages="com.example.demo.controller")
public class ServletConfig implements ServletContextAware {
private ServletContext servletContext;
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
/* 加载thymeleaf模板 */
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(this.servletContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setSuffix(".html");
resolver.setTemplateMode(TemplateMode.HTML); resolver.setCharacterEncoding("UTF-8"); resolver.setCacheable(true); return resolver; }
/* 模板引擎,渲染并返回结果 */
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
/* 视图解析器 */
@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
在配置模板解析器的时候,我选择的是实现ServletContextAware接口以获取ServletContext,并以此为参数创建ServletContextTemplateResolver。此处应该还可以用另一种方式进行配置:
public class WebConfig implements ApplicationContextAware
{
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
//加载 Thymeleaf 模板
@Bean
public SpringResourceTemplateResolver templateResolver()
{
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(this.applicationContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html"); return templateResolver;
}
}
3、测试:
@RequestMapping("/test")
public String test(Model model) throws IOException {
List<User> userList = baseService.queryUsers();
model.addAttribute("userList", userList);
return "test";
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试页面</title>
</head>
<body>
<ul th:each="prop : ${userList}">
<li th:text="${prop.userName}"></li>
</ul>
</body>
</html>

4、此外,html页面常需要引入静态文件,为了饮用方便以及避免静态文件路径错误导致的异常,需要通过WebMvcConfigurer接口设置静态文件的根路径,避免路径错误导致的异常
@EnableWebMvc
public class ServletConfig implements WebMvcConfigurer,ServletContextAware {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/templates/static/");
}
}
注:需要在类上加入@EnableWebMvc注解表示启用java config,否则此方法不会生效。
spring整合thymeleaf的更多相关文章
- SpringMVC整合Thymeleaf
Thymeleaf的介绍 进行JavaWeb开发时主要用到的是JSP,传统的JSP需要在页面中加入大量的JSTL标签,这些标签只能运行在服务器中,前端开发人员维护这些页面比较困难,页面加载速度也比较慢 ...
- Thymeleaf+Spring整合
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统
一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
- Spring Boot 整合 Thymeleaf 完整 Web 案例
Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...
- Thymeleaf模板引擎+Spring整合使用方式的介绍
尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...
- Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- 极简 Spring Boot 整合 Thymeleaf 页面模板
虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...
- [Java] Spring boot2 整合 Thymeleaf 后 去除模板缓存
Spring boot2 整合 Thymeleaf 后 去除模板缓存 网上好多文章只是简单粗暴的说,在 application.properties 做如下配置即可: #Thymeleaf cach ...
随机推荐
- java 删除多层文件夹
/** * 因为不小心,写了一个死循环,在电脑里创建的了n多层空文件夹 * 并且手动最外层删除不掉. * 所以用写了本代码,从里向外的进行删除操作. * @author Singularity * @ ...
- APP测试常用工具以及框架
APP测试常用工具以及框架 1)纯白盒方式的测试,Monkey.一般是开发用的比较多,动手能力强的同学可以自己去尝试下! 2)偏白盒的robotium,这家伙号称是黑盒,但是本人不太认同- 因为使用r ...
- Qt实现 动态化遍历二叉树(前中后层次遍历)
binarytree.h 头文件 #ifndef LINKEDBINARYTREE_H #define LINKEDBINARYTREE_H #include<c++/algorithm> ...
- SQL SEVER 时间格式转换
常用:时分秒(HH:mm:ss):Select CONVERT(varchar(100), GETDATE(), 8) : 10:57:46年月日 (yyyyMMdd):Select CONVERT( ...
- ELK 起航
ELK与我 我在2017年8月份第一次听说ELK并搭建了一次,当时看到KIBANA页面超级炫酷非常激动.现在已经过去了四个月了,现在的情况不像刚开始哪有无知了.现在是要应用到实际的项目中.首先说一下整 ...
- windows下配置下burpsuite的小方法。
1.下载破解版burpsuite和正版burpsuite. 2.安装正版burpsuite(免费版) 3.打开安装路径 4.把破解版的burp拷贝到安装路径下 5.该路径下应该有个burpsuite_ ...
- centos tree 命令
ftp://mama.indstate.edu/linux/tree/ download & make
- MySQL数据库 命令行 学习笔记(一)
常用关系型数据库1 MySQL:开源免费的适用于中小型企业的免费数据库2 oracle:甲骨文公司,商业软件,收费,适用于大型电商网站3 db2:IBM公司,银行系统主要采用db24 SqlSever ...
- mybatis 中javaType和OfType 的区别
JavaType和ofType都是用来指定对象类型的,但是JavaType是用来指定pojo中属性的类型,而ofType指定的是映射到list集合属性中pojo的类型.pojo类: publiccla ...
- 《python for data analysis》第四章,numpy的基本使用
<利用python进行数据分析>第四章的程序,介绍了numpy的基本使用方法.(第三章为Ipython的基本使用) 科学计算.常用函数.数组处理.线性代数运算.随机模块…… # -*- c ...