背景:要将2.png里第一个表格的comment和第二个表格里的Approver Comments里的内容格式和1.png保持一致
上代码:
Map<String, Object> datasourceBody = new HashMap<>();
datasourceBody.put"COMMENTS", StringUtils.isNotEmpty(commons) ? commons.replace("\\n", "<br/>") : commons);
TemplateEmail templateEmail = new TemplateEmail();
templateEmail.setDatasourceBody(new ObjectMapper().valueToTree(datasourceBody));
// 这个转换完的效果就是2.png
// 这个ObjectMapper.valueToTree转换之后会将本来有格式的文本转换成string字符串
我们的邮件模板其实是存在数据库里的,可以看到如下json,body里的{{COMMENTS}}和{{APPROVER_COMMENTS}}在左右两边加上了pre标签,问题迎刃而解。
{
"to": "{{to}}",
"cc": "{{cc}}",
"bcc": "{{bcc}}",
"subject": "Multi Country Quote {{QUOTE_NO}} ({{{QUOTE_DESCRIPTION}}}) has been Approved for {{MC_ACCOUNT_HIERARCHY}}",
"body": "Account Name: {{MC_ACCOUNT_HIERARCHY}}<br/><br/>Quote: {{QUOTE_NO}}<br/>Fulfillment Method: {{FULFILLMENT_METHOD}}<br/>Description: {{DESCRIPTION}}<br/>Lead Country: {{LEAD_COUNTRY_NAME}}<br/>Total Revenue: {{TOTAL_REVENUE}}<br/>Total Margin: {{TOTAL_MARGIN}}<br/><br/><table border=\"1\"><tr><th>product</th><th>Country</th><th>List Price</th><th>Approved Price</th><th>quantity</th><th>Description</th><th>FYI</th><th>REJECTED</th></tr>{{#each QUOTE_LINE_LIST}}<tr><td>{{PRODUCT}}</td><td>{{LINE_COUNTRY}}</td><td>{{LIST_PRICE}}</td><td>{{APPROVED_PRICE}}</td><td>{{QUANTITY}}</td><td>{{DESCRIPTION}}</td><td>{{FYI}}</td><td>{{REJECTED}}</td></tr>{{/each}}<tr><td>Comment</td><td colspan=\"7\"><pre>{{COMMENTS}}</pre></td></tr></table><br/><br/><table border=\"1\"></tr><tr><th>Step Name</th><th>Approval Status</th><th>Date</th><th>APPROVER_ROLE</th><th>Assigned To</th><th>Actual Approver</th><th>Approver Comments</th></tr></p>{{#each WORKFLOW}}<tr><td>Cart Price Approval</td><td>{{APPROVAL_STATUS}}</td><td>{{DATE}}</td><td>{{APPROVER_ROLE}}</td><td>{{ASSIGNED_TO}}</td><td>{{ACTUAL_APPROVER}}</td><td><pre>{{APPROVER_COMMENTS}}</pre></td></tr>{{/each}}</table><br/><br/>Click on the link below to view the quote:<br/><a href={{URL}}>{{QUOTE_NO}}</a>"
}