【开发心得】freemarker生成各类工单的思路

前言:之前和一个网友在评论里聊到生成工单的思路。笔者编写了一个xmlUtils,参考我的另一篇博文

https://blog.csdn.net/qq_26834611/article/details/108856515

基于他提到使用freemarker 去实现,我今天得空的时候去研究了一下(我们已经前后端分离了,而且我只学过thymeleaf)

注意:

1).使用的freemarker jar包版本 :2.3.28 空构造的 Configuration 在2.3.x已经被废弃了,需要传递Version,可以通过它的全局配置类给到,或者直接字符串

2).Springboot jar包部署,类路径获取要用ClassPathResource

    Resource resource = new ClassPathResource("c2xml");
    configuration.setDirectoryForTemplateLoading(resource.getFile());

1.基于ftl模板,写出工单。

ftl(业务上讲code啥的不允许为空,这里只是演示):

<Object ElementType="${programType?default("")}" ID="${id?default("")}" Action="${action?default("")}" Code="${code?default("")}">
    <Property Name="Name">${name?default("")}</Property>
    <Property Name="OrgAirDate">${orgairedate?default("")}</Property>
    <Property Name="LicensingWindowStart">${licensingwindowstart?default("")}</Property>
    <Property Name="LicensingWindowEnd">${licensingwindowend?default("")}</Property>
    <Property Name="SeriesFlag">${seriesflag?default("")}</Property>
    <Property Name="SourceType">${sourcetype?default("")}</Property>
    <Property Name="Status">${status?default("")}</Property>
    <Property Name="VSPCode">${vspcode?default("")}</Property>
    <Property Name="SearchName">${searchname?default("")}</Property>
    <Property Name="Kpeople">${kpeople?default("")}</Property>
    <Property Name="Director">${director?default("")}</Property>
    <Property Name="Description">${description?default("")}</Property>
    <Property Name="SortName">${sortname?default("")}</Property>
    <Property Name="OriginalName">${originalname?default("")}</Property>
    <Property Name="ReleaseYear">${releaseyear?default("")}</Property>
    <Property Name="ContentProvider">${contentprovider?default("")}</Property>
    <Property Name="CopyRight">${copyright?default("")}</Property>
    <Property Name="Type1">${type1?default("")}</Property>
</Object>

demo:

public static void main(String[] args) {
        try {
            Configuration configuration = new Configuration(new Version("2.3.28"));
            configuration.setDefaultEncoding("utf-8");
            Resource resource = new ClassPathResource("c2xml");
            configuration.setDirectoryForTemplateLoading(resource.getFile());
            Template template = configuration.getTemplate("ProgramObject.ftl");
            Map<String,String> map = new HashMap<String,String>();
            map.put("name", "战狼9");
            map.put("director", "吴京");
            File file = new File("E:\\test\\Frout.xml");
            BufferedWriter out = new BufferedWriter(new FileWriter(file));
            template.process(map, out);
            out.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

and,发现freemarker很多可用的方案,比如生成word模板啥的,甚至是偷懒写java和xml代码。后续研究。

猜你喜欢

转载自blog.csdn.net/qq_26834611/article/details/109185985
今日推荐