官方文档入口: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的更多相关文章

  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. vue插件

    Vue.js提供了插件机制,可以在全局添加一些功能.它们可以简单到几个方法.属性,也可以很复杂,比如一整套组件库. 注册插件需要一个公开的方法install,它的第一个参数是Vue构造器,第二个参数是 ...

  2. 解决Ubuntu19.04下网易云音乐打不开的问题

    Ubuntu19.04下打开网易云音乐的v18.04版会出现以下错误: opt/netease/netease-cloud-music/netease-cloud-music: symbol look ...

  3. java的基础语法(标识符 修饰符 关键字)

    Java 基础语法 一个 Java 程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如 ...

  4. 软件测试2019:第四次作业—— 性能测试(含JMeter实验)

    题目:性能测试练习 一.回答下述问题: 性能测试有几种类型,它们之间什么关系? 性能测试根据不同测试目的可以分为以下类: (1)性能验证测试 (2)性能基准测试 (3)性能规划测试 (4)容量测试 渗 ...

  5. VS Code引用 vue/cli

    npm i @vue/cli -g    引用cli脚手架 3.0版本 下载好后 找个空文件夹  vue create myvue 创建vue项目   myvue是自己项目名称 Your connec ...

  6. 加载Excel时,如何过滤空行空列

    巨硬为Excel提供了丰富的C#接口,基本上可以将Excel当做一个微型数据库来用,奈何前端的我们,sql也只会写两句select * from table: 目前工作中遇到了一个问题,在需要读取的E ...

  7. 最新版SEMCMS_PHP_3.5 过滤不严导致sql注入

    一.漏洞分析 在分析过程中看到网上已经有人发现semcms V2.4存在过滤不严导致sql注入的漏洞,不知道咋还没改,而且最新版过滤的关键字更少了. 首先查看首页文件index.php的代码 < ...

  8. Int和String互转的方法

    Java 中int.String的类型转换   int -> String int i=12345;String s="";第一种方法:s=i+"";第二 ...

  9. Docker切换国内镜像

    本人是Ubuntu系统 Ubuntu 18.04 安装 Docker-ce 1.更换国内软件源,推荐中国科技大学的源,稳定速度快(可选) sudo cp /etc/apt/sources.list / ...

  10. [随笔][Java][总结][java 类型系统]

    java 的类型系统大体分为两类,对象和基本类型.java使用静态类型检查来保证类型安全.每个变量在使用之前需要声明.非静态类型的语言不要求变量在使用之前进行声明. 基本数据类型 java的基本类型不 ...