官方文档入口:https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html

1、首先需要引入thymeleaf的依赖(据官网文档,thymeleaf-spring3与thymeleaf-spring4用法基本一致)

  1. <dependency>
  2. <groupId>org.thymeleaf</groupId>
  3. <artifactId>thymeleaf-spring4</artifactId>
  4. <version>3.0.11.RELEASE</version>
  5. </dependency>

2、配置thymeleaf的模板解析器、模板引擎与视图解析器

官方文档以xml作为配置方法,因为不是很方便,此处使用java方式进行配置

  1. package com.example.demo.config;
  2.  
  3. import javax.servlet.ServletContext;
  4.  
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.ComponentScan;
  7. import org.springframework.web.context.ServletContextAware;
  8. import org.thymeleaf.spring4.SpringTemplateEngine;
  9. import org.thymeleaf.spring4.view.ThymeleafViewResolver;
  10. import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
  11.  
  12. @ComponentScan(basePackages="com.example.demo.controller")
  13. public class ServletConfig implements ServletContextAware {
  14.  
  15. private ServletContext servletContext;
  16.  
  17. @Override
  18. public void setServletContext(ServletContext servletContext) {
  19. this.servletContext = servletContext;
  20. }
  21.  
  22. /* 加载thymeleaf模板 */
  23. @Bean
  24. public ServletContextTemplateResolver templateResolver() {
  25. ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(this.servletContext);
  26. resolver.setPrefix("/WEB-INF/templates/");
  27. resolver.setSuffix(".html");
  28. resolver.setTemplateMode(TemplateMode.HTML); resolver.setCharacterEncoding("UTF-8"); resolver.setCacheable(true); return resolver; }
  1. /* 模板引擎,渲染并返回结果 */
  2. @Bean
  3. public SpringTemplateEngine templateEngine() {
  4. SpringTemplateEngine templateEngine = new SpringTemplateEngine();
  5. templateEngine.setTemplateResolver(templateResolver());
  6. templateEngine.setEnableSpringELCompiler(true);
  7. return templateEngine;
  8. }
  9. /* 视图解析器 */
  10. @Bean
  11. public ThymeleafViewResolver viewResolver() {
  12. ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
  13. viewResolver.setTemplateEngine(templateEngine());
  14. viewResolver.setCharacterEncoding("UTF-8");
  15. return viewResolver;
  16. }
  17. }

在配置模板解析器的时候,我选择的是实现ServletContextAware接口以获取ServletContext,并以此为参数创建ServletContextTemplateResolver。此处应该还可以用另一种方式进行配置:

  1. public class WebConfig implements ApplicationContextAware
  2. {
  3. private ApplicationContext applicationContext;
  4.  
  5. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
  6. {
  7. this.applicationContext = applicationContext;
  8. }
  9.  
  10. //加载 Thymeleaf 模板
  11. @Bean
  12. public SpringResourceTemplateResolver templateResolver()
  13. {
  14. SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
  15. templateResolver.setApplicationContext(this.applicationContext);
  16. templateResolver.setPrefix("/WEB-INF/templates/");
  17. templateResolver.setSuffix(".html"); return templateResolver;
  18. }
  19. }

3、测试:

  1. @RequestMapping("/test")
  2. public String test(Model model) throws IOException {
  3. List<User> userList = baseService.queryUsers();
  4. model.addAttribute("userList", userList);
  5. return "test";
  6. }
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>测试页面</title>
  6. </head>
  7. <body>
  8. <ul th:each="prop : ${userList}">
  9. <li th:text="${prop.userName}"></li>
  10. </ul>
  11. </body>
  12. </html>

4、此外,html页面常需要引入静态文件,为了饮用方便以及避免静态文件路径错误导致的异常,需要通过WebMvcConfigurer接口设置静态文件的根路径,避免路径错误导致的异常

  1. @EnableWebMvc
  2. public class ServletConfig implements WebMvcConfigurer,ServletContextAware {
  3. public void addResourceHandlers(ResourceHandlerRegistry registry) {
  4. registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/templates/static/");
  5. }
  6. }

注:需要在类上加入@EnableWebMvc注解表示启用java config,否则此方法不会生效。

spring整合thymeleaf的更多相关文章

  1. SpringMVC整合Thymeleaf

    Thymeleaf的介绍 进行JavaWeb开发时主要用到的是JSP,传统的JSP需要在页面中加入大量的JSTL标签,这些标签只能运行在服务器中,前端开发人员维护这些页面比较困难,页面加载速度也比较慢 ...

  2. Thymeleaf+Spring整合

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  3. Spring Boot 2.x 综合示例-整合thymeleaf、mybatis、shiro、logging、cache开发一个文章发布管理系统

    一.概述 经过HelloWorld示例(Spring Boot 2.x 快速入门(上)HelloWorld示例)( Spring Boot 2.x 快速入门(下)HelloWorld示例详解)两篇的学 ...

  4. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  5. Spring Boot 整合 Thymeleaf 完整 Web 案例

    Thymeleaf 是一种模板语言.那模板语言或模板引擎是什么?常见的模板语言都包含以下几个概念:数据(Data).模板(Template).模板引擎(Template Engine)和结果文档(Re ...

  6. Thymeleaf模板引擎+Spring整合使用方式的介绍

    尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...

  7. Spring Boot2 系列教程(九)Spring Boot 整合 Thymeleaf

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  8. 极简 Spring Boot 整合 Thymeleaf 页面模板

    虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在做前后端不分的开发,而在前后端不分的开发中,我们就会需要后端页面模板(实际上,即使前后端分离,也会在一些场景下需要使用页面模板, ...

  9. [Java] Spring boot2 整合 Thymeleaf 后 去除模板缓存

    Spring boot2 整合 Thymeleaf 后 去除模板缓存 网上好多文章只是简单粗暴的说,在 application.properties  做如下配置即可: #Thymeleaf cach ...

随机推荐

  1. EmptyBeanUtil

    package com.rscode.credits.util; import java.util.List; /** * * 判断实体是否为空 * @author tn * */ public cl ...

  2. Codeforces1056E.Check Transcription(枚举+Hash)

    题目链接:传送门 题目: E. Check Transcription time limit per test seconds memory limit per test megabytes inpu ...

  3. 在java中,异常抛出点后程序的执行情况

    1.在throw语句,即自定义的抛出异常语句后面的代码并不会执行,会提示错误,编译器并不可以正常编译. 2.若在一个条件语句中抛出一个异常,程序可以编译,但不会运行(dead code). 3.若在一 ...

  4. PTA——近似求PI

    PTA 7-50 近似求PI 网友代码: include <stdio.h> int main(){ , i, temp=; scanf("%le", &eps ...

  5. freebsd 记录点

    问题一: FreeBSD修改python的默认版本 在/usr/local/bin目录下, mv python python.old ln -s pythonX.X  python in X.X wr ...

  6. 为什么要用docker

    一:更高效的利用系统资源            由于容器不需要进行硬件虚拟以及运行完整操作系统等额外开销,Docker 对系统资源的利用率更高.无论是应用执行速度.内存损耗或者文件存储速度,都要比传统 ...

  7. java设计模式概述

    java的设计模式大体上分为三大类: 创建型模式(5种):工厂方法模式,抽象工厂模式,单例模式,建造者模式,原型模式. 结构型模式(7种):适配器模式,装饰器模式,代理模式,外观模式,桥接模式,组合模 ...

  8. c# 实现 HSV 调色板

    界面相关核心代码如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } private vo ...

  9. golang-http-post

    func httpPost() { resp, err := http.Post("https://www.abcd123.top/api/v1/login", "app ...

  10. 使用python调用其他脚本

    cmd = '<command line string>' print(cmd) p = subprocess.Popen(args=cmd, shell=True, stdout=sub ...