使用freemarker模板创建文件

使用freemarker模板创建文件

  • 编写模板文件web.vue.ftl 放在resources目录下的templates目录下

    <template>
        <div>
            <el-dialog title="信息" :visible.sync="dialogFormVisible">
    
                    <el-form :model="form">
                        <#list displayList as disp>
                        <el-form-item label="${disp.china}" :label-width="formLabelWidth">
                            <el-input v-model="form.${disp.english}" autocomplete="off"></el-input>
                        </el-form-item>
                        </#list>
                    </el-form>
    
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
    
            <el-row :gutter="20">
    
                <#list displayList as disp>
                    <el-col :span="6">
                        <div class="grid-content bg-purple">
                            ${disp.china} : <el-input v-model="${disp.english}"></el-input>
                        </div>
                    </el-col>
                </#list>
                <el-col :span="6">
                    <div class="grid-content bg-purple">
                        <el-button @click="list">检索</el-button>
                    </div>
                </el-col>
            </el-row>
    
            <el-button @click="dialogFormVisible = true"> 新增</el-button> <br>
            <el-table :data="tableData" border style="width: 100%">
    
                <#list displayList as disp>
                    <el-table-column fixed prop="${disp.english}" label="${disp.china}" width="150">
                    </el-table-column>
                </#list>
    
                <el-table-column fixed="right" label="操作" width="100">
                    <template slot-scope="scope">
                        <el-button @click="handleClick(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button @click="deleteInfo(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <br>
            <el-pagination background layout="prev, pager, next" @current-change="handleCurrentChange" :total="total">
            </el-pagination>
        </div>
    </template>
    
    <script>
        export default {
            name: "${name}",
            created() {
                this.list()
            },
            components: {
    
            },
            data() {
                return {
                    tableData: [],
                    form: {},
                    dialogFormVisible: false,
                    current: 1,
                    total: 1,
                    <#list searchList as sear>
                    ${sear.english},
                    </#list>
                };
            },
            methods: {
                list() {
                    <#list searchList as sear>
                    if (this.${sear.english} == '') {
                        this.${sear.english} == null
                    }
                    </#list>
                    this.$http.get("/inter/${name}/page", {
                        params: {
                            <#list searchList as sear>
                            ${sear.english}:this.${sear.english},
                            </#list>
    
                        }
                    }).then(res => {
                        this.current = res.current
                        this.total = res.total
                        this.tableData = res.records
                    })
                },
                handleCurrentChange(val) {
                    this.current = val
                    this.list()
                },
                handleClick(row) {
                    this.dialogFormVisible = true
                    this.form = {}
                    this.form = row
                },
                add() {
                    this.$http.post("/inter/${name}/save").then(res => {
                        this.dialogFormVisible = false
                        this.form = {}
                        this.list()
                    })
                },
                deleteInfo(row) {
                    this.$http.delete("/inter/${name}/delete?id=" + row.id).then(res => {
                        this.dialogFormVisible = false
                        this.current = 1
                        this.list()
                    })
                }
            },
    
    
  • 编写生成文件的业务类(参数map传递需要写在目标文件里的变量),不管是调用controller还是测试类调用业务类就可根据模板文件生成规定文件

     public void generatorVUE(Map map) {
            Configuration configuration = new Configuration(new Version("2.3.3"));
            configuration.setDefaultEncoding("utf-8");
            String absolutePath = System.getProperty("user.dir") + File.separator+"src" +
                    File.separator + "main" + File.separator + "resources"+File.separator +"templates";
            try{
                // 加载.ftl配置文件所在路径
                configuration.setDirectoryForTemplateLoading(new File(absolutePath));
    
                // 放模板变量的值
                Map<String, Object> params = new HashMap<>();
    
                String name = map.get("name").toString();
                String searchChina = map.get("searchChina").toString();
                String searchEnglish = map.get("searchEnglish").toString();
                String displayChina = map.get("displayChina").toString();
                String displayEnglish = map.get("displayEnglish").toString();
    
                List searchList = new ArrayList();
                List displayList = new ArrayList();
    
                String[] splitSearchChina = searchChina.split(",");
                String[] splitSearchEnglish = searchEnglish.split(",");
                for (int i = 0; i < splitSearchChina.length; i++) {
                    Map map1 = new HashMap();
                    map1.put("china",splitSearchChina[i]);
                    map1.put("english",splitSearchEnglish[i]);
                    searchList.add(map1);
                }
                String[] splitDisplayChina = displayChina.split(",");
                String[] splitDisplayEnglish = displayEnglish.split(",");
                for (int i = 0; i < splitDisplayChina.length; i++) {
                    Map map1 = new HashMap();
                    map1.put("china",splitDisplayChina[i]);
                    map1.put("english",splitDisplayEnglish[i]);
                    displayList.add(map1);
                }
                params.put("name",name);
                params.put("searchList",searchList);
                params.put("displayList",displayList);
    
                //给文件赋值
                Template template = configuration.getTemplate("web.vue.ftl");
                // 文件输出路径
                FileOutputStream file = new FileOutputStream(System.getProperty("user.dir") + File.separator +"web.vue");
                OutputStreamWriter out = new OutputStreamWriter(file, "utf-8");
                template.process(params,out);
                out.close();
            }catch (IOException | TemplateException exception) {
                exception.printStackTrace();
            }
        }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值