freemarker模版引擎生成HTML,尝试使用freemarker模板引擎生成打印文件

尝试使用freemarker模板引擎生成打印文件

参考

依赖

org.springframework.boot

spring-boot-starter-parent

2.2.2.RELEASE

org.springframework.boot

spring-boot-starter-freemarker

测试代码

package com.mozq.freemarker.freemarker01.demo;

import freemarker.template.Configuration;

import freemarker.template.Template;

import freemarker.template.TemplateException;

import freemarker.template.TemplateExceptionHandler;

import java.io.File;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.HashMap;

import java.util.Map;

public class Demo_02 {

public static void main(String[] args) throws IOException, TemplateException {

// Create your Configuration instance, and specify if up to what FreeMarker

// version (here 2.3.22) do you want to apply the fixes that are not 100%

// backward-compatible. See the Configuration JavaDoc for details.

Configuration cfg = new Configuration(Configuration.VERSION_2_3_22);

// Specify the source where the template files come from. Here I set a

// plain directory for it, but non-file-system sources are possible too:

cfg.setDirectoryForTemplateLoading(new File("E:\\mozq\\demo_project\\mozq_01\\freemarker-01\\src\\main\\resources\\templates"));

// Set the preferred charset template files are stored in. UTF-8 is

// a good choice in most applications:

cfg.setDefaultEncoding("UTF-8");

// Sets how errors will appear.

// During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.

cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

// Create the root hash

Map root = new HashMap<>();

// Put string ``user'' into the root

root.put("code", "BigJoe");

root.put("orderQuipment", 6);

root.put("orderStatus", 1);

root.put("successDate", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));

root.put("tableId", "123");

// Create the hash for ``latestProduct''

Map latest = new HashMap<>();

// and put it into the root

root.put("latestProduct", latest);

// put ``url'' and ``name'' into latest

latest.put("url", "products/greenmouse.html");

latest.put("name", "green mouse");

Template temp = cfg.getTemplate("前台联.ftl");

Writer out = new OutputStreamWriter(System.out);

temp.process(root, out);

}

}

模板文件

预点餐前台联

外卖前台联

前台联

#if>

餐桌号:${tableId}

#if>

支付时间:${successDate}

支付状态:已支付

#if>

#if>

变量判空

Mouse found

No mouse found

#if>

bugs

The following has evaluated to null or missing:

==> tableId [in template "前台联.ftl" at line 12, column 6]

----

Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use when-presentwhen-missing#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??

----

错误代码:

餐桌号:${tableId}

#if>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值