Skip to content

Instantly share code, notes, and snippets.

@djkeh
Last active January 3, 2024 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djkeh/6e1d557ce8c466135b1541d342b1c25c to your computer and use it in GitHub Desktop.
Save djkeh/6e1d557ce8c466135b1541d342b1c25c to your computer and use it in GitHub Desktop.
Thymeleaf Decoupled Logic configuration for Spring Boot + Thymeleaf 3
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
@Configuration
public class ThymeleafConfig {
@Bean
public SpringResourceTemplateResolver thymeleafTemplateResolver(
SpringResourceTemplateResolver defaultTemplateResolver,
Thymeleaf3Properties thymeleaf3Properties
) {
defaultTemplateResolver.setUseDecoupledLogic(thymeleaf3Properties.decoupledLogic());
return defaultTemplateResolver;
}
@ConfigurationProperties("spring.thymeleaf3")
public record Thymeleaf3Properties(boolean decoupledLogic) {}
}
@djkeh
Copy link
Author

djkeh commented Jun 11, 2022

This lets you use a Spring Boot user-custom property called spring.thymeleaf3.decoupled-logic to turn on/off the decoupled logic feature for Thymeleaf 3.
Use this property inside application.properties or application.yaml to activate Thymeleaf 3 decoupled logic.
I personally feel weird that Spring Boot common application properties for Thymeleaf isn't taking care of this option by these days as it's not a difficult thing to provide at all,
even though this feature seems not so popular to Spring Boot and Thymeleaf users.

이 스프링 부트 설정은 사용자 정의 프로퍼티 spring.thymeleaf3.decoupled-logic 을 사용하여 타임리프 3의 템플릿 로직 분리(decoupled logic) 기능을 이용할 수 있게 해줍니다.
application.properties 혹은 application.yaml 에서 사용하여 템플릿 로직 분리 옵션을 제어해 보세요.
저는 개인적으로 이 옵션이 스프링 부트 애플리케이션 프로퍼티에서 설정할 수 있도록 제공되지 않는다는 사실이 이상하게 느껴집니다. 세팅이 어려운 부분이 아니기 때문에요.
비록 스프링 부트와 타임리프 유저들에게 그렇게 인기있는 기능은 아니라 하더라도요.

I made the prefix of the property to spring.thymeleaf3, it might have been smoother to use spring.thymealeaf instead, but I chose not to do that way to avoid confusion.
This is as far as I know the simplest and the most graceful way to use decoupled logic in Spring Boot.
Also, I suggest you use this Spring Boot configuration with the dependency spring-boot-configuration-processor for better IDE support and experience.

프로퍼티 앞글자는 spring.thymeleaf3로 지었습니다. spring.thymealeaf로 지었으면 더 좋았겠지만, 혼란을 피하기 위해 구분을 두었습니다.
제가 아는 한 이것이 스프링 부트에서 타임리프 로직 분리기능을 가장 쉽고 우아하게 사용할 수 있는 방법입니다.
또한, 이 프로퍼티를 spring-boot-configuration-processor 스프링 부트 디펜던시와 함께 사용하여 IDE 의 지원과 나은 경험을 느껴보세요.

Tested on Spring Boot 2.7.0 + thymeleaf 3.0.15

Reference

@bcchoi518
Copy link

👍

@bluesyy
Copy link

bluesyy commented Mar 29, 2023

👍

@hojin7769
Copy link

👍

@matrixpower1004
Copy link

👍

@congsole
Copy link

congsole commented Jan 3, 2024

👍

@djkeh
Copy link
Author

djkeh commented Jan 3, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment