javathymeleaf

Failing to process Thymeleaf template as a String


import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.StringTemplateResolver;
import org.thymeleaf.templatemode.TemplateMode;

import java.util.HashMap;
import java.util.Map;

public class EmailService {

    public static void main(String[] args) {
        // Define the template string
        String emailTemplateString = "Hello ${greetings},\n\nHere is your report: ${subject}.\n\nBest regards,\n${team}\n\n(Email Subject: ${name})";

        // Create the context and set variables
        Context context = new Context();
        Map<String, Object> variables = new HashMap<>();
        variables.put("greetings", "Dear Frodo");
        variables.put("subject", "Sub Custody Report");
        variables.put("team", "Ring of the fellowship");
        variables.put("name", "Aragorn");
        context.setVariables(variables);

        // Set up the template engine
        TemplateEngine textTemplateEngine = new TemplateEngine();
        StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
        stringTemplateResolver.setTemplateMode(TemplateMode.TEXT);
        stringTemplateResolver.setCacheable(false);
        textTemplateEngine.setTemplateResolver(stringTemplateResolver);

        // Process the template
        String processedTemplate = textTemplateEngine.process(emailTemplateString, context);

        // Print the result
        System.out.println(processedTemplate);
    }
}

Getting response like this:

"Hello ${greetings},\n\nHere is your report: ${subject}.\n\nBest regards,\n${team}\n\n(Email Subject: ${name})" I know that template is not a .txt file but as far as I read it should be possible to make it from String and for such case need to use TemplateMode.TEXT although non of them placing values.


Solution

  • I change the syntax of the variables in your template string and turned off the TemplateMode.TEXT.

    The template string uses [[${...}]] syntax to refer to the variables. The syntax you had in your template was ${...}.

    Final code below,

    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;
    import org.thymeleaf.templateresolver.StringTemplateResolver;
    import org.thymeleaf.templatemode.TemplateMode;
    
    import java.util.HashMap;
    import java.util.Map;
    
    public class EmailService {
    
        public static void main(String[] args) {
            // Define the template string
            String emailTemplateString = "Hello [[${greetings}]],\n\nHere is your report: [[${subject}]].\n\nBest regards,\n[[${team}]]\n\n(Email Subject: [[${name}]])";
    
            // Create the context and set variables
            Context context = new Context();
            Map<String, Object> variables = new HashMap<>();
            variables.put("greetings", "Dear Frodo");
            variables.put("subject", "Sub Custody Report");
            variables.put("team", "Ring of the fellowship");
            variables.put("name", "Aragorn");
            context.setVariables(variables);
    
            // Set up the template engine
            TemplateEngine textTemplateEngine = new TemplateEngine();
            StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();
    //        stringTemplateResolver.setTemplateMode(TemplateMode.TEXT);
            stringTemplateResolver.setCacheable(false);
            textTemplateEngine.setTemplateResolver(stringTemplateResolver);
    
            // Process the template
            String processedTemplate = textTemplateEngine.process(emailTemplateString, context);
    
            // Print the result
            System.out.println(processedTemplate);
        }
    }
    

    And I see this output,

    Hello Dear Frodo,
    
    Here is your report: Sub Custody Report.
    
    Best regards,
    Ring of the fellowship
    
    (Email Subject: Aragorn)