springBoot multiple template resolver config

공부는 혼자하는 거·2023년 4월 7일
0

Spring Tip

목록 보기
36/52

필자는 이메일 발송 할 때 템플릿 엔진으로서 타임리프를 사용하고 있다. 이메일 양식은 resource/templates 폴더 내에 위치하고 있는데, 이메일 양식이 많아짐으로서, 폴더쩨로 구분해야 될 필요가 생겼다. 필자는 SpringTemplateEngine에 넘겨줄 template 이름을 대충 아래와 같은 Enum으로 관리하고 있는데

enum class EmailTemplate(
    val templateTarget: String,
    val subject: String,
    var username:String,
    val desc: String,
    var languageCode:String,
    val group: TemplateGroup,
) {

    /**
     * todo applesoicalsigninService refactoring
     *
     */

    VERIFICATION("verification", "domain verification", "", "인증 코드", Const.DEFAULT_LANGUAGE, TemplateGroup.EMAIL_VERIFICATION),
}

문제는 templates 아래의 하위 디렉토리의 템플릿을 넘겨줄 때


    VERIFICATION("mails/verification", "domain verification", "", "인증 코드", Const.DEFAULT_LANGUAGE, TemplateGroup.EMAIL_VERIFICATION)
    

위와 같이는 안 된다. 따라서 타임리프 기본설정을 재구성 해야 할 필요가 생겼다.

@Configuration
class ThymeleafConfig(
    //private val servletContext: ServletContext
) : ApplicationContextAware {

    private lateinit var applicationContext: ApplicationContext

    private val log = KotlinLogging.logger {  }

    //https://stackoverflow.com/questions/40420963/how-to-add-support-for-more-than-one-thymeleaf-template-modes

    override fun setApplicationContext(@Autowired applicationContext: ApplicationContext) {
        this.applicationContext = applicationContext
    }


    @Bean
    fun templateResolver(): ITemplateResolver {
        val emailTemplateResolver = ClassLoaderTemplateResolver()
        //templateResolver.setApplicationContext(applicationContext)
        emailTemplateResolver.prefix = "templates/mails/"
        emailTemplateResolver.suffix = ".html"
        emailTemplateResolver.templateMode = TemplateMode.HTML
        emailTemplateResolver.characterEncoding = "UTF-8"
        emailTemplateResolver.order = 0
        emailTemplateResolver.checkExistence = true /* FYI: necessary to chain TemplateResolvers */
        emailTemplateResolver.isCacheable = false /* FYI: during development -> false, so that we can see changes we make */

        return emailTemplateResolver
    }

    @Bean
    fun templateResolver2(): ITemplateResolver {

        val templateResolver = ClassLoaderTemplateResolver()

        templateResolver.prefix = "templates/"
        templateResolver.suffix = ".html"
        templateResolver.templateMode = TemplateMode.HTML
        templateResolver.characterEncoding = "UTF-8"
        templateResolver.order = 1
        templateResolver.checkExistence = true /* FYI: necessary to chain TemplateResolvers */
        templateResolver.isCacheable = false /* FYI: during development -> false, so that we can see changes we make */
        return templateResolver
    }

    @Bean
    fun templateEngine(): SpringTemplateEngine {
        /* SpringTemplateEngine automatically applies SpringStandardDialect and
           enables Spring's own MessageSource message resolution mechanisms. */
        val templateEngine = SpringTemplateEngine()
        templateEngine.addTemplateResolver(templateResolver())
        templateEngine.addTemplateResolver(templateResolver2())
        templateEngine.enableSpringELCompiler = true
        return templateEngine
    }

    @Bean
    fun viewResolver(): ViewResolver {
        val viewResolver = ThymeleafViewResolver()
        viewResolver.templateEngine = templateEngine()
        viewResolver.characterEncoding = "UTF-8"
        viewResolver.isCache = false /* FYI: during development -> false */
        viewResolver.order = 0
        return viewResolver
    }


}

이제 두 가지 폴더 모두에서 템플릿 이름으로 접근할 수 있다.

profile
시간대비효율

0개의 댓글