简体   繁体   English

Java Spring Boot-获取电子邮件的图像路径以在Freemarker中工作

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

I am new to Java Spring boot. 我是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" 我可以访问Freemarker演示-但是如何获得“ \\ 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. Spring已经提供了开箱即用的功能,您可以在其中将这些路径映射到静态URI。

What you will need to do is, define a Spring Configuration class, extend it with WebMvcConfigurerAdapter 您需要做的是,定义一个Spring Configuration类,并使用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. 这将使您可以访问addResourceHandlers方法,覆盖该方法,并提供静态资源的路径以及要使用其映射的URI。 Am providing link to another SO answer which does the same thing. 正在提供指向执行相同操作的另一个SO答案的链接。

Spring 4 loading static resources Spring 4加载静态资源

Spring 4 - addResourceHandlers not resolving the static resources 春季4-addResourceHandlers无法解析静态资源

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

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