前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用velocity

使用velocity

作者头像
allsmallpig
发布2021-02-25 10:28:59
5220
发布2021-02-25 10:28:59
举报
文章被收录于专栏:allsmallpi博客

使用maven引入jar

代码语言:javascript
复制
    org.apache.velocity
    velocity
    1.7


    org.apache.velocity
    velocity-tools
    2.0
    
        
            org.apache.struts
            struts-tiles
        
        
            org.apache.struts
            struts-taglib
        
        
            org.apache.struts
            struts-core
        
        
            sslext
            sslext
        
        
            oro
            oro
        
    


  
            commons-configuration
            commons-configuration
            1.10
        
        
            commons-net
            commons-net
            3.3

创建TemplateUtil工具类

代码语言:javascript
复制
package com.os.core.util.web;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;


public class TemplateUtil {
    public static void main(String[] args) {
        test();
    }

    /**
     * 测试方法
     */
    public static void test() {
        try {
            String vmContent = "${dataMap.value1}中文
${dataMap.value2}";
            String vmFileUrl = "";//CommonConstants.templateFileDir;
            //生成模板文件
            String fileName = createTemplateFile(vmContent, vmFileUrl);
            //模板上下文
            Map dataMap = new HashMap();
            dataMap.put("value1", "ssssssssssssssssssssss");
            dataMap.put("value2", "中文");
            VelocityContext context = new VelocityContext();
            context.put("dataMap", dataMap);

            //获取模板生成器
            VelocityEngine engine = initEngine("");//CommonConstants.templateFileDir
            //html文件生成全路径
            File file = new File("", "UTF-8");//CommonConstants.htmlFileDir+"/index.html"
            boolean createSuccess = createHtmlFile(engine, fileName, file, context);
            if (createSuccess) {
                System.out.println("文件生成完成");
            } else {
                System.out.println("文件生成失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成静态的HTML文件
     *
     * @param engine       模板生成器
     * @param tempFileName 模板文件
     * @param file         HTML生成的全路径
     * @param context      模板上下文
     * @return true生成成功 false生成失败
     * @throws Exception
     */
    public static boolean createHtmlFile(VelocityEngine engine, String tempFileName, File file, VelocityContext context) {
        try {
            if (file.getParentFile().exists() == false) {
                file.getParentFile().mkdirs();
            }
            Template temp = engine.getTemplate(tempFileName, "UTF-8");
            FileOutputStream tempFos = new FileOutputStream(file);
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(tempFos, "UTF-8"));
            temp.setEncoding("UTF-8");
            temp.merge(context, writer);
            writer.close();
            tempFos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 初始化模板生成器
     *
     * @return VelocityEngine
     * @throws Exception
     */
    public static VelocityEngine initEngine(String genDir) throws Exception {
        VelocityEngine engine = new VelocityEngine();
        Properties properties = new Properties();
        properties.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, genDir);
        properties.setProperty(VelocityEngine.INPUT_ENCODING, "UTF-8");
        properties.setProperty(VelocityEngine.OUTPUT_ENCODING, "UTF-8");
        engine.init(properties);
        return engine;
    }

    /***
     * 创建模板文件
     *
     * @param fileContext  模板内容
     * @param createDirUrl 模板保存目录
     */
    public static String createTemplateFile(String fileContext, String createDirUrl) {
        BufferedWriter bw = null;
        try {
            String fileDirUrl = createDirUrl + ".vm";
            File file = new File(fileDirUrl);
            if (file.getParentFile().exists() == false) {
                file.getParentFile().mkdirs();
            }
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
            bw.write(fileContext);
            return fileDirUrl;
        } catch (Exception e) {
            System.out.println("生成模板文件出错");
            e.printStackTrace();
            return null;
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018/01/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档