简体   繁体   English

在我的servlet中使用thymeleaf,找不到html模板文件

[英]Using thymeleaf in my servlet, html template file not found

I am trying to use thymeleaf to read an html template, remplace a variable in it and then send it as an email. 我正在尝试使用百里香读取html模板,在其中重新放置变量,然后将其作为电子邮件发送。 However I am stuck at the part of reading the html template file. 但是,我被困在阅读html模板文件的部分。

This is the error i get: 这是我得到的错误:

The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>org.thymeleaf.exceptions.TemplateInputException: Error resolving template &quot;invitationemail&quot;, template might not exist or might not be accessible by any of the configured Template Resolvers
    org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:247)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1192)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1148)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1095)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1008)
    org.thymeleaf.TemplateEngine.process(TemplateEngine.java:982)
    yesterscape.ses.SESManagement.sendInvitationEmail(SESManagement.java:69)
    yesterscape.users.InviteUserServlet.doPost(InviteUserServlet.java:68)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

So I am assumig it is because I am not properly setting my invitationemail.html file. 之所以这样,是因为我没有正确设置我的Invitationemail.html文件。

This is the code to read the email and change the value: 这是读取电子邮件并更改值的代码:

ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode("HTML5");
resolver.setSuffix(".html");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);
final Context context = new Context(Locale.US);
context.setVariable("name", nameFrom);
final String htmlVer = templateEngine.process("invitationemail", context);

As you can see, I am setting the suffix as .html and in the name of the file to process i am just typing "invitationemail". 如您所见,我将后缀设置为.html,并在要处理的文件名中输入“ invitationemail”。 But still it cannot be found. 但是仍然找不到。

In what folder of my servlet architecture should I include the invitationemail.html file? 我应该在servlet体系结构的哪个文件夹中包含Invitationemail.html文件? I thought it should be in the WEB-INF folder, and this is where it currently is but I still have no luck. 我以为它应该在WEB-INF文件夹中,这是当前位置,但是我仍然没有运气。

Thanks in advice for any assistance you can provide. 感谢您提供任何帮助的建议。

Instead of ClassLoaderTemplateResolver use another ITemplateResolver implementation as follows 代替ClassLoaderTemplateResolver使用另一个ITemplateResolver实现,如下所示

ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/template/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
// ... etc

And put you email template into /WEB-INF/template folder.. 并将您的电子邮件模板放入/WEB-INF/template文件夹。

Problem fixed. 问题已解决。 When using the ClassLoaderTemplateResolver (which is the resolver needed for email content) the html files should be placed in the following directory: 使用ClassLoaderTemplateResolver(电子邮件内容所需的解析器)时,html文件应放在以下目录中:

WEB-INF/classes WEB-INF / classes目录

Answer based on this post: http://forum.thymeleaf.org/springmail-sample-td3945553.html 根据这篇文章的答案: http : //forum.thymeleaf.org/springmail-sample-td3945553.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Thymeleaf模板无法访问,这是我的配置文件吗? - Thymeleaf Template Not accessable, is it my config file? 为什么我的数据库 (mysql) 中的信息没有显示在我的 html 模板上? 这是一个使用 thymeleaf 的 springboot 应用程序 - Why is the information from my database (mysql) not showing on my html template? This is a springboot application using thymeleaf Spring 启动 / Docker:Thymeleaf 模板未找到 ZFC35FDC70D5FC69D2539883A822C 邮件 - Spring Boot / Docker: Thymeleaf template not found for html mail 将 html 注入 thymeleaf 模板 - Inject html into thymeleaf template 在基于 Web 的 Spring Scopes 中使用 Thymeleaf 处理 HTML 文件并将处理后的模板存储为字符串 - Process HTML file using Thymeleaf in Web based Scopes of Spring and store the processed template as String 使用 Thymeleaf 处理 HTML 模板的模板解析期间发生错误 - An error happened during template parsing using Thymeleaf to process a HTML template 我想使用 thymeleaf 将其包含到 html 文件中 - I want to include into a html file using thymeleaf 使用 Thymeleaf 与控制器中的 html 文件交互 - Interacting with html file from controller using Thymeleaf 如何在 Spring Boot 中使用 Thymeleaf 将值传递给 HTML 模板的变量 - How to pass values to variables of a HTML template using Thymeleaf in Spring Boot 在servlet中包含html模板 - Including html template in servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM