繁体   English   中英

Freemarker 模板无法访问图像 - Java Spring Boot

[英]Freemarker templates unable to gain access to images - Java Spring Boot

我正在尝试访问电子邮件模板上的图像。 我试过按照本指南进行操作。 Spring:引用 resources/static 文件夹和这个Spring 4 - addResourceHandlers 不解析静态资源

在此处输入图像描述

我将模板存储在这个位置

my-project\complete\src\main\resources\templates

http://localhost:8080/static/templates/forgotemail/images/bottompodmiddleb.jpg

^ 我想访问这个图像——我有一个 reactjs 构建,我不确定 reactjs 路由是否干扰了这个。

例如,我可以访问此图像。

http://localhost:8080/static/media/-bach.4f9c4b25.jpg

我创建了一个配置类 - 但我不确定是否需要调用它或什么。

import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


//@Configuration
@ComponentScan("Application")
public class Configuration extends WebMvcConfigurerAdapter {
     public void addResourceHandlers(ResourceHandlerRegistry registry) {  
        // I tried these many combinations separately.

        ResourceHandlerRegistration resourceRegistration = registry
            .addResourceHandler("resources/**");
        resourceRegistration.addResourceLocations("/resources/**");
        registry.addResourceHandler("/templates/**").addResourceLocations("/templates/**");
        //registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
        //registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:/resources/"); 
              // do the classpath works with the directory under webapp?
     }

}

这是启动电子邮件的代码。 “fmConfiguration.getTemplate("email-template.html")”似乎可以访问 html 电子邮件。

public void sendEmail(JSONObject model, JavaMailSender mailSender, Configuration fmConfiguration) throws Exception{
    System.out.println("type>>>"+model);
    System.out.println("mailSender"+ mailSender);

    if(mailSender != null){

        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            System.out.println("mimeMessage >>>"+ mimeMessage);

            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);

            mimeMessageHelper.setSubject("Hi");
            mimeMessageHelper.setFrom("test2@gmail.com");
            mimeMessageHelper.setTo("test@gmail.com");

            //Map < String, Object > model = new HashMap < String, Object > ();
            model.put("firstName", "Yashwant");
            model.put("lastName", "Chavan");
            model.put("imgPath", "resources/static/images/");

            mimeMessageHelper.setText(geContentFromTemplate(fmConfiguration, model), true);

            mailSender.send(mimeMessageHelper.getMimeMessage());
        } catch (MessagingException e) {
            System.out.println("ERROR - mimeMessage>>>");
            e.printStackTrace();
        }        
    }

}

public String geContentFromTemplate(Configuration fmConfiguration, Map < String, Object > model) {
    StringBuffer content = new StringBuffer();

    try {
        content.append(FreeMarkerTemplateUtils
            .processTemplateIntoString(fmConfiguration.getTemplate("email-template.html"), model));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return content.toString();
}

我也有同样的问题,无法将图像插入到 freemarker 模板中。

我按照这里提到的例子

现在可以使用这种方法添加图像。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM