Spring Boot 2.x整合模版引擎(1)-Thymeleaf的XML模式,自定义方言属性

背景

前段时间有一需求,需要动态修改xml模版的内容,但是网上能收集的资料多是关于thymeleaf的HTML使用方式;于是,在【科学上网】与自己的研究下,终于成功解决了这个需求。

通过下文,将可以学到Spring Boot2.x+ThymeleafXML模式的使用,以及自定义Thymeleaf方言属性两个知识点;水平有限,若有误,欢迎各路英雄指正。

 

一、Thymeleaf的XML模式

1、创建spring boot工程

使用IDEA创建spring boot工程,具体步骤参考其他资料。

 

2、引入thymeleaf的依赖

在pom.xml中,添加thymeleaf相关的依赖,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.7.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.he</groupId>
   <artifactId>springboot-template</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboot-template</name>
   <description>Spring Boot2.x整合模板引擎技术</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

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


      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger2</artifactId>
         <version>2.8.0</version>
      </dependency>
      <dependency>
         <groupId>io.springfox</groupId>
         <artifactId>springfox-swagger-ui</artifactId>
         <version>2.8.0</version>
      </dependency>
   </dependencies>

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

3、在[application.properties]中,修改配置

在此简单起见,我全部使用spring boot默认的配置,可以看到该文件内容是为空的

 

4、新建Thymeleaf配置类,提供Bean

Thymeleaf的XML模式,不像默认的HTML,需要额外提供两个Bean才可以成功运行;

第一个,是[SpringResourceTemplateResolver],模版解析器;

第二个,是[SpringTemplateEngine],Spring的模版引擎。

  • 新建类[ThymeleafConfig.java]如下
package com.he.config;


import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;


/**
* @date 2019/10/4
* @des Thymeleaf配置
*/
@Configuration
public class ThymeleafConfig {


    @Bean
    SpringResourceTemplateResolver xmlTemplateResolver(ApplicationContext appCtx) {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();

        templateResolver.setApplicationContext(appCtx);
        templateResolver.setPrefix("classpath:/templates/");//指定模版前缀,即存放位置,默认是该地址
        templateResolver.setSuffix(".xml");//指定模版后缀
        templateResolver.setTemplateMode(TemplateMode.XML);//指定使用‘XML’模式
        templateResolver.setCharacterEncoding("UTF-8");//指定使用‘UTF-8’编码
        templateResolver.setCacheable(true);//开启缓存

        return templateResolver;
    }

    @Bean
    SpringTemplateEngine templateEngine(ApplicationContext appCtx) {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setEnableSpringELCompiler(true);
        templateEngine.setTemplateResolver(xmlTemplateResolver(appCtx));
        return templateEngine;
    }

}

 

5、添加Thymeleaf模版

在[resources-templates]中,新建我们的模版[person-test.xml],注意Thymeleaf语法的正确使用

xml模版内容如下,十分简单,我们模拟动态的修改一个人的姓名国籍信息

<?xml version="1.0" encoding="UTF-8"?>
<persons>
    <person>
        <!--tag内容,使用strings.concat()拼接示例
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值