java velocity 模板引擎

Maven 依赖

        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.2</version>
        </dependency>

工具类

/**
 * 模板引擎渲染
 *
 * @author: enncy
 */
public class TemplateEngine {

    /**
     *  渲染后返回字符串结果
     *
     * @param context 渲染信息
     * @param templatePath 模板文件资源路径
     * @return: java.lang.String
     */
    public static String  render(VelocityContext context,String templatePath){
        VelocityEngine velocity = new VelocityEngine();
        //模板文件所在的路径
        String sourcePath = templatePath.substring(0, templatePath.lastIndexOf("/"));
        String fileName = templatePath.substring(templatePath.lastIndexOf("/"));
        String path = Objects.requireNonNull(TemplateEngine.class.getClassLoader().getResource(sourcePath)).getPath();
        //设置参数
        velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
        //处理中文问题
        velocity.setProperty(Velocity.INPUT_ENCODING, "GBK");
        //初始化模板
        velocity.init();

        StringWriter stringWriter = new StringWriter();
        velocity.mergeTemplate(fileName, "utf-8", context,stringWriter);
        return stringWriter.toString();
    }


    /**
     *  渲染成一个文件
     *
     * @param context   渲染信息
     * @param templatePath  模板文件资源路径
     * @param outputPath    输出路径
     * @return: void
     */
    public  static void renderFile(VelocityContext context,String templatePath,String outputPath) throws IOException {
        String render = TemplateEngine.render(context, templatePath);

        File file = new File(outputPath);
        if(!file.exists()){
            URL resource = TemplateEngine.class.getClassLoader().getResource(outputPath);
            if(resource!=null){
                String path = resource.getPath();
                file = new File(path);
            }else{
                throw new NullPointerException("资源文件不存在!");
            }

        }
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(render);
        fileWriter.close();
    }


}

用法

class MyTest {

    @Test
    public void test() {

        VelocityContext context = new VelocityContext();
        context.put("name","enncy");

        // 渲染成字符串
        String render = TemplateEngine.render(context,"templates/index.html");
        System.out.println(render);

        // 渲染到指定文件
        TemplateEngine.renderFile(context,"templates/index.html","templates/index.html");


    }

}

输出

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    enncy
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值