--- title: Spring Boot Controller (2) Thymeleaf 複雜例子 disqus: hackmd --- ###### tags: `SpringBoot` Spring Boot Controller (2) Thymeleaf 複雜例子 === [TOC] ## 目的 有一個java的數據結構在後端產生了,可能是List或是HashMap要如何將它放進thymeleaf裡面 ## key ![](https://i.imgur.com/YpiylXu.png) ## MailHTMLContentBuilder ```java= import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; @Service public class MailHTMLContentBuilder { Logger LOG = LoggerFactory.getLogger(MailHTMLContentBuilder.class); private TemplateEngine templateEngine; @Autowired public MailHTMLContentBuilder(TemplateEngine templateEngine) { this.templateEngine = templateEngine; } public String generateMailContent(String templateFilePath, Map<String, Object> templateModelValueMap) { Context context = new Context(); context.setVariables(templateModelValueMap); return templateEngine.process(templateFilePath, context); } } ``` ## 傳送email附檔案是htmltable的信件 ```java= @Service public class DemoThymeleafTable_SERVICE { private static Logger logger = LoggerFactory.getLogger(DemoThymeleafTable_SERVICE.class); @Autowired EmailService emailService; @Autowired MailHTMLContentBuilder mailHTMLContentBuilder; // help us use thylmeleaf public void sayHI() { logger.info("HI"); System.out.println("HI"); } public List <JoinAI_RatioBean> giveMeJoinAI_RatioBeanListData( ) { List <JoinAI_RatioBean> myList = new ArrayList<JoinAI_RatioBean>() ; JoinAI_RatioBean data1= new JoinAI_RatioBean("A","AP1583","mike",0,0, null ); JoinAI_RatioBean data2= new JoinAI_RatioBean("A","AP1582","sandy",85,90, null ); JoinAI_RatioBean data3= new JoinAI_RatioBean("B","AP1587","mike",52,25, null ); JoinAI_RatioBean data4= new JoinAI_RatioBean("B","AP1587","Robin",52,25, null ); JoinAI_RatioBean data5= new JoinAI_RatioBean("B","AP1587","ELmo",52,25, null ); myList.add(data1); myList.add(data2); myList.add(data3); myList.add(data4); myList.add(data5); return myList; } public void demoThymeleafTemplateWithTable() { String[] toPrimaryString =new String[] {"mike@tradeserv.com","robin@tradeserv.com"}; String[] toCcStrings =null; String subjectString ="Demo Sending HTML"; String templateFilePathString="DemoThymeleafTemplateWithTableControl.html"; //Data List <JoinAI_RatioBean> myDataList = giveMeJoinAI_RatioBeanListData( ); for (JoinAI_RatioBean each: myDataList) { logger.info(each.toString()); } // Thymeleaf model Map<String, Object> templateModelValueMap=new HashMap<>(); templateModelValueMap.put("title", "d"); templateModelValueMap.put("mydataList", myDataList); String mailHTMLBodyString=mailHTMLContentBuilder.generateMailContent(templateFilePathString, templateModelValueMap); emailService.sendHTMLMail(subjectString,mailHTMLBodyString,toPrimaryString); } public void demoThymeleafTemplateWithTable2() { String[] toPrimaryString =new String[] {"mike@tradeserv.com","robin@tradeserv.com"}; String[] toCcStrings =null; String subjectString ="Demo Sending HTML"; String templateFilePathString="DemoThymeleafTemplateWithTableControl.html"; //Data List <JoinAI_RatioBean> myDataList = giveMeJoinAI_RatioBeanListData( ); for (JoinAI_RatioBean each: myDataList) { logger.info(each.toString()); } // Thymeleaf model Map<String, Object> templateModelValueMap=new HashMap<>(); templateModelValueMap.put("title", "d"); templateModelValueMap.put("mydataList", myDataList); String mailHTMLBodyString=mailHTMLContentBuilder.generateMailContent(templateFilePathString, templateModelValueMap); emailService.sendHTMLMail(subjectString,mailHTMLBodyString,toPrimaryString); } } ```