2

N.B. Before anyone says that this is a duplicate from Spring Boot + Thymeleaf not finding message properties please know that I have tried to resolve my issue using the answers provided in the link but nothing has worked for me so far.

I'm running into a problem with using Thymeleaf and Spring Boot. I've followed the tutorial provided by Thymeleaf but for some reason when the template processing is finished, I end up with the following:

<!DOCTYPE html>
<html>
<body>
    <h3>??admin.greeting_en??</h3>
    <p>??admin.deposit.request.accepted_en??</p>
    <p><strong>??email.generation_en??</strong></p>
    <p>??email.regards_en??</p>
    <p>??admin.regards_en??</p>
</body>
</html>

I placed my messages_en.properties under the module which contains the Spring configuration for Thymeleaf (template resolver, message resolver etc)... The path is notifications/src/main/resources/messages_en.properties

I have also placed the properties file inside the /templates directory and it still doesn't work.

The setup class for thymeleaf looks like this.

@Configuration
public class SpringMailConfig {

    @Bean
    public MessageSource emailMessageSource() {
        final ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("messages");
        return messageSource;
    }


    @Bean
    public TemplateEngine springTemplateEngine() {
        final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        templateEngine.setTemplateEngineMessageSource(emailMessageSource());
        return templateEngine;
    }

    private ITemplateResolver htmlTemplateResolver() {
        final ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        templateResolver.setCacheable(false);
        return templateResolver;
    }
}

As an example, inside my properties (messages_en.properties) file I have:

admin.greeting = Hello, Admin!

This is what the template looks like (the HTML file)

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
    <h3 th:text="#{admin.greeting}">Dear Admin,</h3>
    <p th:text="#{admin.deposit.request.accepted(${amount}, ${user})}">The deposit request of AMOUNT euro made for user USER has been accepted.</p>
    <p><strong th:text="#{email.generation}">This is an automated email.</strong></p>
    <p th:text="#{email.regards}">Kind Regards,</p>
    <p th:text="#{admin.regards}">Robots</p>
</body>
</html>

Nothing has worked and I can't figure out why. Using variables (denoted by the ${}) inside the templates works, but the messages (using the #{}) aren't found.

This is the class which I use to invoke the template engine. The method String returnTemplateHtmlContent(String templatePath, Locale locale, Map<String, Object> map) throws TemplateInputException, NullPointerException is the method which does the actual processing. The Locale passed is set to "en", the map holds variables to be inserted into the templates (this works).

public abstract class AbstractMailHelper {

    SpringTemplateEngine springTemplateEngine;
    protected static final String ENCODING = "UTF-8";

    public AbstractMailHelper() {

    }

    public AbstractMailHelper(SpringTemplateEngine templateEngine) {
        this.springTemplateEngine = templateEngine;
    }

    //Prepares values to be injected into template
    protected Context prepareTemplateContext(Locale locale, Map<String, Object> contextMap) {
        final Context context = new Context(locale);
        context.setVariables(contextMap);
        return context;
    }

    //returns as a string the template with the custom values inserted
    protected String returnTemplateHtmlContent(String templatePath, Locale locale, Map<String, Object> map) throws TemplateInputException, NullPointerException {
        return springTemplateEngine.process(templatePath, prepareTemplateContext(locale, map));
    }
}
D. Gal
  • 329
  • 2
  • 14

0 Answers0