模板文件样例功能以实现:1、按指定长度自动填充空格 2、按照列表循环生成文件
1个文件已修改
2个文件已添加
92 ■■■■■ 已修改文件
hangzhoumesParent/moduleService/howllowGlassModule/src/main/java/com/mes/hollow/controller/HollowBigStorageCageController.java 57 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/moduleService/howllowGlassModule/src/main/java/com/mes/utils/Blank.java 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/moduleService/howllowGlassModule/src/main/resources/templates/hollowGlass.ftl 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/moduleService/howllowGlassModule/src/main/java/com/mes/hollow/controller/HollowBigStorageCageController.java
@@ -2,17 +2,28 @@
import com.mes.hollow.entity.dto.HollowBigStorageAndDetailsDTO;
import com.mes.hollow.entity.request.HollowTaskRequest;
import com.mes.hollow.entity.vo.HollowBigStorageDetailsQueryVO;
import com.mes.hollow.service.HollowBigStorageCageService;
import com.mes.utils.Blank;
import com.mes.utils.Result;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * (HollowBigStorageCage)表控制层
@@ -20,6 +31,7 @@
 * @author makejava
 * @since 2024-11-21 09:22:39
 */
@Api(tags = "中空理片笼详情")
@RestController
@RequestMapping("hollowBigStorageCage")
public class HollowBigStorageCageController {
@@ -31,5 +43,42 @@
    public Result<List<HollowBigStorageAndDetailsDTO>> queryHollowBigStorageCageDetail(@RequestBody HollowBigStorageDetailsQueryVO query) {
        return Result.build(200, "查询成功", hollowBigStorageCageService.queryHollowBigStorageCageDetail(query));
    }
    @GetMapping("/generate")
    public ResponseEntity<FileSystemResource> generate() throws IOException, TemplateException {
// 创建和配置Freemarker配置实例
        Configuration cfg = new Configuration(new Version("2.3.29"));
        cfg.setClassForTemplateLoading(HollowBigStorageCageController.class, "/templates/");
        // 创建Calculator实例
        Blank blank = new Blank();
        // 创建数据模型
        Map<String, Object> root = new HashMap<>();
        root.put("blank", blank);
        HollowTaskRequest request = new HollowTaskRequest();
        request.setCell(1);
        request.setFlowCardId("1111");
        List<HollowTaskRequest> list = new ArrayList<>();
        list.add(request);
        list.add(request);
        list.add(request);
        root.put("request", request);
        root.put("list1", list);
        // 获取模板
        Template temp = cfg.getTemplate("hollowGlass.ftl");
        // 合并数据模型和模板
        StringWriter out = new StringWriter();
        try {
            temp.process(root, out);
        } catch (TemplateException | IOException e) {
            e.printStackTrace();
        }
        // 输出结果
        System.out.println(out.toString());
        return null;
    }
}
hangzhoumesParent/moduleService/howllowGlassModule/src/main/java/com/mes/utils/Blank.java
New file
@@ -0,0 +1,20 @@
package com.mes.utils;
/**
 * @Author : zhoush
 * @Date: 2024/12/26 11:08
 * @Description:
 */
public class Blank {
    public String computeBlank(Object original, int length) {
        String tempStr = "";
        if (null != original) {
            tempStr = original + "";
        }
        // 格式化字符串
        return String.format("%1$" + length + "s", tempStr).replace(' ', ' ');
    }
}
hangzhoumesParent/moduleService/howllowGlassModule/src/main/resources/templates/hollowGlass.ftl
New file
@@ -0,0 +1,15 @@
<#-- 假设你有一个变量名为"exampleText" -->
${blank.computeBlank(request.cell,15)}|
${blank.computeBlank(request.flowCardId,30)}|
${blank.computeBlank(request.totalPairQuantity,15)}|
${blank.computeBlank(request.formulaId,15)}|
<h1>List</h1>
<ul>
    <#list list1 as req>
        ${blank.computeBlank(req.cell,15)}|
        ${blank.computeBlank(req.flowCardId,30)}|
        ${blank.computeBlank(req.totalPairQuantity,15)}|
        ${blank.computeBlank(req.formulaId,15)}|
    </#list>
</ul>