如何解决Java中的spring temlateInput异常?

马丁

我正在使用 spring boot、thymleaf、spring MVC、gradlebuild 工具。我想用 thymleaf 模板附件实现 spring 邮件。当我在这个方法中调用 web 服务(restful)来发送电子邮件和这个方法时,我正在调用一个 spring 模板引擎来生成一个 thymleaf 模板来发送电子邮件附件。

为了进一步说明,我附上了一个流程结构。

在此处输入图片说明

休息控制器

@RequestMapping(value= "/completeBatchUpload")
public Map<String,String>completeBatchShipmentUpload() throws Exception {

Map<String, String> resp = new HashMap<>();
 String s = emailService.sendmail();

//----some code here based on result of emailService.sendmail method.----
 return resp;    
}

电子邮件服务.java

public String sendmail(){
     try {
        MimeMessage message = javaMail.createMimeMessage();

         try {
          MimeMessageHelper helper = new MimeMessageHelper(message, true);
          helper.setFrom(new InternetAddress("[email protected]"));
          helper.setTo(new InternetAddress("[email protected]"));
          helper.setSubject("subject here");                                                  helper.setText(attachmentUtile.buildContentUsingTemplate();

                } catch (Exception e) {
                    throw new MailParseException(e);
                }

                javaMail.send(message);
            }
    }

@Service
public class AttachmentUtile {

    private SpringTemplateEngine templateEngine;

    @Autowired
    public AttachmentUtile (SpringTemplateEngine templateEngine) {
        this.templateEngine = templateEngine;
    }

    public String buildContentUsingTemplate(List<Map<String, String>>     templateContent, String recipientName) {
        final Context ctx = new Context();
        ctx.setVariable("recipientName", "ritesh");
        String s = templateEngine.process("send", ctx);
        return s;

    }
}

发送.html

使用 Thymeleaf HTML 模板示例发送电子邮件

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>



<!-- use the font -->
<style>
    body {
        font-family: 'Roboto', sans-serif;
        font-size: 48px;
    }
</style>

<table align="center" border="0" cellpadding="0" cellspacing="0" width="600" style="border-collapse: collapse;">
    <tr>
        <td align="center" bgcolor="#78ab46" style="padding: 40px 0 30px 0;">
            <p>RITESH CHOUDAHRY</p>
        </td>
    </tr>
    <tr>
        <td bgcolor="#eaeaea" style="padding: 40px 30px 40px 30px;">
            <p>Dear ${recipientName},</p>
            <p>Sending Email using Spring Boot with <b>Thymeleaf template !!!</b></p>
            <p>Thanks</p>
        </td>
    </tr>

</table>


百里香叶配置文件

@Configuration
public class ThymLeafConfig {

    @Bean
    public SpringTemplateEngine springTemplateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(htmlTemplateResolver());
        return templateEngine;
    }

    @Bean
    public ITemplateResolver  htmlTemplateResolver(){ 
        SpringResourceTemplateResolver emailTemplateResolver = new SpringResourceTemplateResolver();
        emailTemplateResolver.setPrefix("/templates/");
        emailTemplateResolver.setSuffix(".html");
        emailTemplateResolver.setTemplateMode(StandardTemplateModeHandlers.HTML5.getTemplateModeName());
        emailTemplateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
        return emailTemplateResolver;
    }

项目文件夹结构

项目文件夹结构

我得到以下异常:

在此处输入图片说明

I have seen one link in stackoverflow Error resolving template “login” spring boot, thymeleaf, But still I am not able to solve my problem. If anybosy have have idea please share. Thanks in advance. If any query required please comment.

Aleh Maksimovich

It seems that you have exactly the same problem as in the link you cite.

From your project structure one can tell that path to template is classpath:/templates/send.thml as you put your template in resources folder. Later it will land added to your jar/war.

At the same time you configure your prefix to

emailTemplateResolver.setPrefix("/templates/");

在 web 应用程序中将被视为应用程序根目录中的文件夹(不是 web 应用程序类路径所在的 /WEB-INF/classes),在 jar 可启动应用程序中,此路径将被视为相对于基本文件夹(您曾经运行过的文件夹)您的应用程序)。

您应该将该行更新为

emailTemplateResolver.setPrefix("classpath:/templates/");

classpath:前缀给人春天ResourceResolver的提示,你的模板是应用的类路径的资源(应搜索用ClassLoader.getResource())。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何解决线程“ main”中的异常java.lang.NullPointerException错误

线程“主”中的异常java.lang.NoSuchMethodError:主-如何解决该问题?

如何解决线程“ main”中的异常java.lang.NoClassDefFoundError

如何解决线程“ main”中的异常java.lang.NullPointerException

如何解决 MySQLSyntaxErrorException 异常?

如何解决此异常

如何解决Java未找到类的异常?

如何解决此Java mysql异常:通信链接失败?

如何解决C#中的系统参数异常错误?

如何解决Servelts中的非法状态异常

如何解决JavaScriptSerializer中超出maxJsonLength的异常?

如何解决Git中的“远端异常挂断”错误

如何解决C#中的OutOfMemory异常?

如何解决 PyTest 中的陈旧元素异常

GET中的StackOverflow异常错误,如何解决?

如何解决laravel中获取异常id的问题

如何解决线程主线程中的异常?怎么了?

我如何解决此错误线程“主”中的异常java.lang.NoClassDefFoundError:javax / crypto / SecretKey

如何解决cors起源异常

如何解决 GETJson 重复异常?

Odoo 13:如何解决CacheMiss异常

如何解决数字格式异常?

如何解决xcpretty未捕获的异常

如何解决“无效的密钥库异常”?

如何解决Null异常错误

如何解决找不到活动异常

尝试使用Spring Config Server时如何解决“没有可用的密码” JSch异常?

如何解决 ERROR Executor - 阶段 20.0 (TID 20) 中任务 0.0 中的异常?

如何解决异常类java.util.HashMap $ Values不能强制转换为HashMap中的类java.util.Enumeration