html文件生成在线连接,使用Themleaf 模板引擎手动生成html文件

1.为什么要写这一篇呢?

在做一个邮件发送功能的时候,需要发送html邮件,javaMail 发送html 的时候需要有已经生成的html正文,所以需要提前将要发送的内容生成,所以就需要模板引擎来动态填充数据。

public voidsendHtmlEmail(String to, String object, String content) {

MimeMessage message= mailSender.createMimeMessage();//创建一个MINE消息

try{//true表示需要创建一个multipart message

MimeMessageHelper helper = new MimeMessageHelper(message, true);

helper.setFrom(from);

helper.setTo(to);

helper.setSubject(object);

helper.setText(content,true);

mailSender.send(message);

log.info("html邮件发送成功");

}catch(MessagingException e) {

log.error("发送html邮件时发生异常!", e);

}

}

2.引入依赖

org.springframework.boot

spring-boot-starter-thymeleaf

ognl 的jar包可能并不需要,在生成过程中报classNotFound ,应该是和我的项目结构有关系,这个包根据实际情况来使用

ognl

ognl

3.2

3.代码

(1)首先准备一个html模板,需要替换的内容使用themleaf的th表达式来占位。我的工程是springboot 项目,文件位置放在resources/templates,也就是classpath:templates/

Title

td{width:80px;height:25px;align-content:center;

}

亲爱的,您月份的工资如下:

2.配置模板引擎

@Configurationpublic classMailConfig {

@Bean("myTemplateEngine")publicTemplateEngine templateEngine(){

ClassLoaderTemplateResolver resolver= newClassLoaderTemplateResolver();

resolver.setPrefix("templates/");

resolver.setSuffix(".html");

TemplateEngine engine= newTemplateEngine();

engine.setTemplateResolver(resolver);returnengine;

}

}

为手动生成html文件单独定义一个模板引擎,其他的使用默认配置。其中制定了模板文件的位置及后缀名,这个配置和application.properties 中配置是一样的

3.生成html

@PostMapping("/sendEmail")

@ResponseBodypublic AjaxResult sendEmail(Integer id) throwsIOException {

List componentVos =salaryCompnentService.selectComponentSum(id);

List th =Lists.newArrayList();

List tb =Lists.newArrayList();for (int i = 0; i < componentVos.size(); i++) {

ComponentVo vo=componentVos.get(i);

th.add(i, vo.getName());

tb.add(i, vo.getAmount());

}

SalaryCalculateVo salaryCalculate=salaryService.selectSalaryById(id);

Context context= newContext();

context.setVariable("name", salaryCalculate.getEmpName());

context.setVariable("month", salaryCalculate.getWorkMonth());

context.setVariable("th", th);

context.setVariable("tb", tb);

String res= engine.process("mailTeamlate", context);

salaryService.sendEmail(id,res, salaryCalculate.getWorkMonth()+ "工资条", salaryCalculate.getEmail());return toAjax(1);

}

由于是测试功能,没有在代码结构上下功夫,随便写一下。主要的代码是王模板上下文Context中填充参数,engine.process()有很多重载的方法,主要有两类,一类是直接输出内容,一类是将文件输出到指定文件里。String template 这个参数指的是模板的名称。比如我的叫mailTeamlate,解析的时候模板引擎会找classpath:templates/mailTeamlate.html 这个文件。

public finalString process(String template, IContext context) {return this.process(new TemplateSpec(template, (Set)null, (TemplateMode)null, (String)null, (Map)null), context);

}

public final voidprocess(String template, IContext context, Writer writer) {this.process(new TemplateSpec(template, (Set)null, (TemplateMode)null, (String)null, (Map)null), context, writer);

}

4.效果

具体的文件内容就不说了,直接看我放到邮件的里面正文里的html样式

019dbe41c60f26ae8cb23a7349fe64a8.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值