Thymeleaf不会将所有图标附加到模板

时间:2018-10-04 17:29:46

标签: java html5 spring-boot thymeleaf

当我尝试在模板上生成带有图标的电子邮件时,我遇到了 Thymeleaf 的问题。我按照文档中的建议See part 4.3进行了操作,但电子邮件中仅显示了3个图标中的2个。

首先,我加载图标:

Map<String, Map<String, Double>> result = new LinkedHashMap<>();
data.forEach(entry -> {
    String name = (String) entry.get("name");
    Map<String, Double> map = result.computeIfAbsent(name, k -> new HashMap<>());
    map.merge("old", (Double) entry.get("old"), Double::sum);
    map.merge("new", (Double) entry.get("new"), Double::sum);
});

然后将图标添加到private byte[] loadImage(String path, String extension) { byte[] image = new byte[0]; try { URL file = getClass().getClassLoader().getResource(path); BufferedImage buffer = ImageIO.read(file); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(buffer, extension, outputStream); image = outputStream.toByteArray(); } catch (IOException exception) { log.debug(exception.getLocalizedMessage()); }

MimeMessageHelper

位置:

private void attachIcons(MimeMessageHelper message) {
    try {
        byte[] logoIconInBytes = loadImage(LOGO_ICON_PATH, "png");
        InputStreamSource logoImageSource = new ByteArrayResource(logoIconInBytes);
        message.addInline(LOGO_IMAGE_RESOURCE, logoImageSource, IMAGE_CONTENT_TYPE);
        byte[] phoneIconInBytes = loadImage(PHONE_ICON_PATH, "png");
        InputStreamSource phoneImageSource = new ByteArrayResource(phoneIconInBytes);
        message.addInline(PHONE_IMAGE_RESOURCE, phoneImageSource, IMAGE_CONTENT_TYPE);
        byte[] emailIconInBytes = loadImage(EMAIL_ICON_PATH, "png");
        InputStreamSource emailImageSource = new ByteArrayResource(emailIconInBytes);
        message.addInline(EMAIL_IMAGE_RESOURCE, emailImageSource, IMAGE_CONTENT_TYPE);
    } catch (MessagingException exception) {
        log.debug(exception.getLocalizedMessage());
    }
}

然后,在发送电子邮件之前,我在private static final String LOGO_ICON_PATH = "data" + File.separator + "logo_example.png"; private static final String PHONE_ICON_PATH = "data" + File.separator + "phone.png"; private static final String EMAIL_ICON_PATH = "data" + File.separator + "email.png"; private static final String IMAGE_CONTENT_TYPE = "image/png"; 上设置了变量:

context

在模板上,我分别在变量...... context.setVariable(LOGO_IMAGE_RESOURCE, LOGO_IMAGE_RESOURCE); context.setVariable(PHONE_IMAGE_RESOURCE, PHONE_IMAGE_RESOURCE); context.setVariable(EMAIL_IMAGE_RESOURCE, EMAIL_IMAGE_RESOURCE); String content = templateEngine.process(templateName, context); ...... 上调用变量:

<img>

但是没有显示电子邮件图标,使用Firefox开发工具检查 DOM 我发现了这一点:

Doom inspection

您会看到已附加 Logo Phone 图标,但未附加 Email 图标。

我改变了图标的想法,以为文件已损坏但没有任何更改。

出什么问题了?

致谢。

1 个答案:

答案 0 :(得分:0)

好吧,我偶然发现了问题所在。我不知道这是一个错误还是因为我不知道该API的使用方式,但这就是发生的奇怪事情:

当我添加图标时:

byte[] logoIconInBytes = loadImage(LOGO_ICON_PATH, "png");
InputStreamSource logoImageSource = new ByteArrayResource(logoIconInBytes);
message.addInline(LOGO_IMAGE_RESOURCE, logoImageSource, IMAGE_CONTENT_TYPE);
byte[] phoneIconInBytes = loadImage(PHONE_ICON_PATH, "png");
InputStreamSource phoneImageSource = new ByteArrayResource(phoneIconInBytes);
message.addInline(PHONE_IMAGE_RESOURCE, phoneImageSource, IMAGE_CONTENT_TYPE);
byte[] emailIconInBytes = loadImage(EMAIL_ICON_PATH, "png");
InputStreamSource emailImageSource = new ByteArrayResource(emailIconInBytes);
message.addInline(EMAIL_IMAGE_RESOURCE, emailImageSource, IMAGE_CONTENT_TYPE);

我删除了电子邮件代码行,以防万一出错。

byte[] logoIconInBytes = loadImage(LOGO_ICON_PATH, "png");
InputStreamSource logoImageSource = new ByteArrayResource(logoIconInBytes);
message.addInline(LOGO_IMAGE_RESOURCE, logoImageSource, IMAGE_CONTENT_TYPE);
byte[] phoneIconInBytes = loadImage(PHONE_ICON_PATH, "png");
InputStreamSource phoneImageSource = new ByteArrayResource(phoneIconInBytes);
message.addInline(PHONE_IMAGE_RESOURCE, phoneImageSource, IMAGE_CONTENT_TYPE);

由于偶然的原因,我运行了该应用程序并生成了电子邮件,然后发现没有显示电话图标。我删除了电话图标并仅加载徽标,这次电子邮件中没有显示任何图标。

因此,解决方案是在加载所有图标以使其显示之后添加虚拟内容。

byte[] logoIconInBytes = loadImage(LOGO_ICON_PATH, "png");
InputStreamSource logoImageSource = new ByteArrayResource(logoIconInBytes);
message.addInline(LOGO_IMAGE_RESOURCE, logoImageSource, IMAGE_CONTENT_TYPE);
byte[] phoneIconInBytes = loadImage(PHONE_ICON_PATH, "png");
InputStreamSource phoneImageSource = new ByteArrayResource(phoneIconInBytes);
message.addInline(PHONE_IMAGE_RESOURCE, phoneImageSource, IMAGE_CONTENT_TYPE);
byte[] emailIconInBytes = loadImage(EMAIL_ICON_PATH, "png");
InputStreamSource emailImageSource = new ByteArrayResource(emailIconInBytes);
message.addInline(EMAIL_IMAGE_RESOURCE, emailImageSource, IMAGE_CONTENT_TYPE);
// Dummy content that won't be displayed to make all previous icons visible on the email
message.addInline("emptyIcon", new ByteArrayResource(new byte[0]), IMAGE_CONTENT_TYPE);