zoukankan      html  css  js  c++  java
  • Spring Boot整合 Thymeleaf 模板引擎

    什么是Thymeleaf

    Thymeleaf是一款用于渲染XML、XHTML、HTML5内容的模板引擎。类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框架进行集成作为Web应用的模板引擎。

    Thymeleaf也是Spring Boot首要支持的模板引擎,并且在最新的Spring Boot版本中已经不再支持Velocity了。

    官网:http://www.thymeleaf.org/

    引入依赖

    需要引入Spring Boot的Thymeleaf启动器依赖。

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    引入该依赖后会自动引入web依赖,不需要再单独引入web依赖。

    自动配置说明

    下面是Thymeleaf的自动配置相关类。

    Thymeleaf的自动配置类:

    org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration

    Thymeleaf的自动配置参数类:

    org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties

    查看参数源码:

    private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
    
    private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
    
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    
    public static final String DEFAULT_SUFFIX = ".html";
    

    默认的编码是:UTF-8

    默认的类型是:text/html

    默认的模板文件目录是:classpath:/templates/

    默认的模板文件后缀是:.html

    这些参数都可以通过在application配置文件中指定spring.thymeleaf.xx进行更改,更多可参考该参数类。

    实战

    知道了自动配置的原理,所以我们可以知道怎么做了。

    一、在resources目录下创建templates目录。

    二、在templates目录下创建.html模板文件。

    三、使用模板:

    1、模板文件头部使用<html xmlns:th="http://www.thymeleaf.org">定义。

    2、html标签上使用th:开头标识作为前缀。

    3、通过@{}引入web静态文件。

    <link rel="stylesheet" th:href="@{/css/jquery.min.css}"/>
    

    4、访问数据

    访问springmvc中的model数据:${user.name},访问更多不同对象的数据请点击参考官方定义。

    推荐:Spring Boot & Cloud 最强技术教程

  • 相关阅读:
    怎样创建动态扩容数组?
    怎样终止一个线程?
    模式识别
    基于Linux根据僵尸网络病毒平台《比尔盖茨》
    web.xml在&lt;init-param&gt;一个错误
    DTD学习笔记
    Java排序算法(四):Shell排序
    VC6迁移到VS2008几个问题——良好的代码,从我做起,从现在开始。
    Maven 建 Struts2 基本实现 CURD Controller
    Andy&#39;s First Dictionary
  • 原文地址:https://www.cnblogs.com/javastack/p/9153936.html
Copyright © 2011-2022 走看看