简体   繁体   中英

Java Spring Boot - getting images paths for emails to work in Freemarker

I am new to Java Spring boot. -

I've got access to the Freemarker demo -- but how do I get hold of the image path for "\\src\\main\\resources\\static\\images"

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

Spring already provides this out of the box, where you can map such paths to static URIs.

What you will need to do is, define a Spring Configuration class, extend it with WebMvcConfigurerAdapter

This will give you access to addResourceHandlers method, override that method and provide the path to your static resources along with what URI you want to map it with. Am providing link to another SO answer which does the same thing.

Spring 4 loading static resources

Spring 4 - addResourceHandlers not resolving the static resources

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