Home > Enterprise >  Thymeleaf - return rendered template to String
Thymeleaf - return rendered template to String

Time:12-17

I was using this solution as an example:

Can I render Thymeleaf templates manually from a String?

but after processing template I am getting not rendered template:

my template (plans/test.html):

<!doctype html>
<html lang="pl">
<div th:text="${loki}"></div>
</html>

Java code that should renderd the template:

@RestController
public class PlansPageRestController {

   
    @Autowired
    TemplateEngine myTemplateEngine;

    @RequestMapping(value = {"/public/plans"}, method = RequestMethod.POST, produces = "application/json")
    public Map<String,String> getPlans(@RequestParam Map<String, String> requestParams) {

        Context ctx = new Context();
        ctx.setVariable("loki", "Some test value");

        String htmlTemplate = myTemplateEngine.process("plans/test.html", ctx);

        Map<String,String> result = new HashMap<>();
        result.put("html", htmlTemplate );
        result.put("result", "success" );

        return result;
    }

}

but as a result I am getting content of plans/test.html so:

<!doctype html>
<html lang="pl">
<div th:text="${loki}"></div>
</html>

I am working with spring boot 3.0.0 and regarding to pom I am using thymeleaf:

<artifactId>thymeleaf-spring6</artifactId>
<version>3.1.0.RELEASE</version>

Can anyone help me in finding what I am doing wrong?

my thymeleaf configuration:

@Configuration
public class ThymeleafConfiguration implements WebMvcConfigurer, ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Bean
    public TemplateEngine myTemplateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        engine.setDialect(new LayoutDialect());
        return engine;
    }
    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setApplicationContext(applicationContext);
        resolver.setPrefix("classpath:/templates/");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCacheable(false);
        resolver.setCacheTTLMs(0L);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
}

CodePudding user response:

The reason your standard Thymeleaf expression is not being evaluated is because you have replaced the Thymeleaf "standard" dialect with the layout dialect:

engine.setDialect(new LayoutDialect());

(I should say "the Spring dialect", given you are using Spring-Thymeleaf.)

If you need to use the layout dialect, then you can add it to the engine - and still keep the standard dialect as well:

engine.addDialect(new LayoutDialect());

Or, you may not need the layout dialect at all (you do not use it in the sample HTML file in the question, but maybe you use it elsewhere). If that is the case, you can remove this line of code.


Just to add: The default Spring Boot settings should work without you needing to define any ThymeleafConfiguration class. But, again, there may be other places where you need to use a custom configuration (e.g. the UTF-8 encoding).

  • Related