ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-12-27 13:56:31  阅读:409  来源: 互联网

标签:java 打印文件 freemarker new put import root 模板


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

参考

freemarker中文

freemarker官网

依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath/>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

测试代码

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<String, Object> 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<String, Object> 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);

    }
}

模板文件

<html>
<head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head>
<body>
<#if orderQuipment == 1>
    <p style='text-align:center;font-size: 48px;font-weight: bold;'>预点餐前台联</p>
<#elseif orderQuipment == 6>
    <p style='text-align:center;font-size: 48px;font-weight: bold;'>外卖前台联</p>
<#else>
    <p style='text-align:center;font-size: 48px;font-weight: bold;'>前台联</p>
</#if>

<#if tableId?? && tableId != ''>
    <p style='text-align:center;font-size: 48px;font-weight: bold;'>餐桌号:${tableId}</p>
</#if>

<#if orderStatus?? && orderStatus == 1>
<p style='Width:100%;text-align: left;font-weight: bold;'>支付时间:${successDate} </p>
    <#if orderQuipment?? && orderQuipment != 5>
<p style='Width:100%;text-align: left;font-weight: bold;'>支付状态:已支付</p>
    </#if>
</#if>
</body>
</html>

变量判空

<#if mouse??>
  Mouse found
<#else>
  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 <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
错误代码:
<#if tableId != null && tableId != ''>
    <p style='text-align:center;font-size: 48px;font-weight: bold;'>餐桌号:${tableId}</p>
</#if>

标签:java,打印文件,freemarker,new,put,import,root,模板
来源: https://www.cnblogs.com/mozq/p/12106856.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有