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

时间:2017-09-09 08:55:11

标签: java spring spring-mvc freemarker

我正在尝试访问电子邮件模板上的图像。我试过按照本指南。 Spring: refering the resources/static folder和此Spring 4 - addResourceHandlers not resolving the static resources

enter image description here

我将模板存储在此位置

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

^我想获得对此图像的访问权限 - 我有一个reactjs构建,我不确定reactjs路由是否干扰了这一点。

我可以访问此图片。

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?
     }

}

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

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();
}

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

{{1}}

0 个答案:

没有答案