如何解决“@environment”问题? Spring Boot 应用程序中 Thymeleaf 模板中的 bean
我有一个无头的Spring Boot应用程序(仅显示REST API),但是我正在使用Thymeleaf进行某些电子邮件模板。
我已经包含
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
在依赖项中。
当我在模板中加入@environment
时,我会收到错误
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'environment'
,例如,我的模板代码
<p>
<a th:href="${@environment.getProperty('system.clientUrl')} + '/verify?action=accept&verificationCode=' + ${verificationCode}">Verify</a>
</p>
是system.clienturl
在application.properties < /代码>文件。
胸腺的其他方面似乎工作正常(包括当我在资源捆绑中定义客户端URL时)。
对于模板中引用的其他对象,我将传递模型对象模型对象的set&lt; string,对象&gt;
。
Map<String, Object> templateModel = new HashMap<String, Object>();
templateModel.put("verificationCode", emailVerification.getVerificationCode());
我想,我也可以传递环境
或也可以使用clienturl,但是我假设@environment
始终可用于Thymeleaf Renderer。
更新
我的渲染代码是
Context thymeleafContext = new Context();
thymeleafContext.setVariables(templateModel);
String htmlBody = thymeleafTemplateEngine.process(templateName, thymeleafContext);
我需要传递到上下文吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论