简体   繁体   中英

Freemarker templates unable to gain access to images - Java Spring Boot

I am trying to gain access for images on an email template. I've tried following this guide. Spring: refering the resources/static folder and this Spring 4 - addResourceHandlers not resolving the static resources

在此处输入图像描述

I am storing the templates in this location

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

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

^ I want to gain access to this image -- I have a reactjs build and I am unsure if the reactjs routing is interfering with this.

I have access to this image for example.

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

I create a configuration class -but I am unsure if I need to invoke it or what.

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

}

here is the code that launches the email. "fmConfiguration.getTemplate("email-template.html")" appears to get access to the html email.

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

I also had same kind of issue of not able to insert the images into the freemarker template.

I followed the example mentioned here

Now able to add the images with this approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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