How do I resolve the '@environment' bean in a Thymeleaf template in a Spring Boot app

Viewed 142

I have a Spring boot application that is headless (only presenting REST APIs), but I am using Thymeleaf for some email templates.

I have included

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

in my dependencies.

When I include @environment in my template, I get the error

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'environment'

My template code is, for example,

<p>
  <a th:href="${@environment.getProperty('system.clientUrl')} + '/verify?action=accept&verificationCode=' + ${verificationCode}">Verify</a>
</p>

where system.clientUrl is defined in the application.properties file.

Other aspects of Thymeleaf seem to work just fine (including when I defined the client URL in a resources bundle).

For other objects referenced in the template, I am passing a Set<String,Object> of template model objects.

Map<String, Object> templateModel = new HashMap<String, Object>();
    templateModel.put("verificationCode", emailVerification.getVerificationCode());

I suppose, I could pass the Environment or just the clientUrl in too, but I assumed that @environment is always available to the Thymeleaf renderer.

Update

My rendering code is

Context thymeleafContext = new Context();
thymeleafContext.setVariables(templateModel);
String htmlBody = thymeleafTemplateEngine.process(templateName, thymeleafContext);

Is there something I need to pass to the context?

0 Answers
Related