【问题标题】:How to resollve spring temlateInput Exception in Java?如何解决 Java 中的 spring 模板输入异常?
【发布时间】:2018-04-02 08:00:23
【问题描述】:

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

为了更清楚,我附上了一个流程结构。

RestController

@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;    
}

EmailService.java

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

         try {
          MimeMessageHelper helper = new MimeMessageHelper(message, true);
          helper.setFrom(new InternetAddress("abc@gmail.com"));
          helper.setTo(new InternetAddress("xyz@gmail.com"));
          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;

    }
}

send.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>


ThymleafConfiuration 文件

@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;
    }

项目文件夹结构

我遇到了以下异常:

我在 stackoverflow Error resolving template “login” spring boot, thymeleaf 中看到了一个链接,但我仍然无法解决我的问题。如果任何人有想法,请分享。提前致谢。如果需要任何查询,请发表评论。

【问题讨论】:

    标签: java spring rest spring-mvc thymeleaf


    【解决方案1】:

    您的问题似乎与您引用的link 完全相同。

    从您的项目结构中可以看出模板的路径是classpath:/templates/send.thml,因为您将模板放在资源文件夹中。稍后它将添加到您的 jar/war 中。

    同时你配置你的前缀为

    emailTemplateResolver.setPrefix("/templates/");
    

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

    您应该将行更新为

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

    classpath: 前缀向 Spring ResourceResolver 提示您的模板是应用程序类路径资源(应使用 ClassLoader.getResource() 搜索)。

    【讨论】:

    • 非常感谢。现在我的问题已经从你的代码中解决了。请为这个问题投票以帮助其他人。
    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 2016-09-25
    • 2016-10-20
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    相关资源
    最近更新 更多