[英]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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.