Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.thymeleaf.templatemode.StandardTemplateModeHandlers can't be resolved

In my Spring boot API, I need to send an email using Thymeleaf.Therefore, I chose this tutorial.However, when adding ThymeleafConfig.java, STS throws the following error.

The import org.thymeleaf.templatemode.StandardTemplateModeHandlers cannot be resolved

As stated in this answer, I changed the dependencies as follows.But it didn't solve the problem.

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>

<dependency>
  <groupId>nz.net.ultraq.thymeleaf</groupId>
  <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

ThymeleafConfig.java

import java.nio.charset.StandardCharsets;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//following import is not resolved
import org.thymeleaf.templatemode.StandardTemplateModeHandlers;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;


@Configuration
public class ThymeleafConfig {

  @Bean
  public ClassLoaderTemplateResolver htmlTemplateResolver(){
    ClassLoaderTemplateResolver emailTemplateResolver = new ClassLoaderTemplateResolver();
    emailTemplateResolver.setPrefix("/templates/");
    emailTemplateResolver.setSuffix(".html");
      emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
    emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
    return emailTemplateResolver;
 }
}
like image 735
Pavindu Avatar asked May 30 '19 12:05

Pavindu


People also ask

What is Thymeleaf in HTML?

What is Thymeleaf? The Thymeleaf is an open-source Java library that is licensed under the Apache License 2.0. It is a HTML5/XHTML/XML template engine. It is a server-side Java template engine for both web (servlet-based) and non-web (offline) environments. It is perfect for modern-day HTML5 JVM web development.

What is Thymeleaf used for?

Thymeleaf is a modern server-side Java template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. The main goal of Thymeleaf is to provide an elegant and highly-maintainable way of creating templates.

What is Thymeleaf template engine?

4. Thymeleaf. Thymeleaf is a Java template engine which can process HTML, XML, text, JavaScript or CSS files. Unlike other template engines, Thymeleaf allows using templates as prototypes, meaning they can be viewed as static files.

Is Thymeleaf a frontend?

Thymeleaf is server rendered, it's not front end.


1 Answers

Use TemplateMode instead of StandardTemplateModeHandlers

import org.thymeleaf.templatemode.TemplateMode;

    @Bean
    public SpringResourceTemplateResolver htmlTemplateResolver(){
        SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
        emailTemplateResolver.setPrefix("classpath:/templates/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode(TemplateMode.HTML);
        emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        return emailTemplateResolver;
    }

In HTML template, use th:text

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
 <p th:text="${name}">
</body>
</html>

When you are processing your template , use thymeleaf's TemplateEngine

 import org.thymeleaf.TemplateEngine; 

   @Autowired
    private TemplateEngine templateEngine;

       Context context = new Context();
            context.setVariables(mail.getModel());
            String html = templateEngine.process("email-template", context);
like image 82
Akalya Avatar answered Oct 22 '22 23:10

Akalya