Velocity 读取字符串模板生成代码

简介: 本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/49428277 未经博主允许不得转载。 博主地址是:http://blog.csdn.net/freewebsys1,遇到问题之前使用 freeMarker 开发 cms系统,生成html。 后来页面不用jsp,开发了,换成velocity展示了

本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/49428277 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,遇到问题

之前使用 freeMarker 开发 cms系统,生成html。
后来页面不用jsp,开发了,换成velocity展示了。
想着生成页面也使用velocity。
但是发现读取文件的类库加载不进来。

2,解决

参考官方网站例子:
http://velocity.apache.org/engine/devel/developer-guide.html


import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;
import java.util.Date;

public class CreateHtml {
    public static void main(String[] args) {

        Velocity.init();

        /* lets make a Context and put data into it */

        VelocityContext context = new VelocityContext();

        context.put("name", "Velocity");
        context.put("project", "Jakarta");
        context.put("now", new Date());

        /* lets make our own string to render */

        String str = "We are using $project $name to render this. $now";
        StringWriter stringWriter = new StringWriter();
        Velocity.evaluate(context, stringWriter, "mystring", str);
        System.out.println(" string : " + stringWriter);

    }
}

读取文件从一个字符串读取模板,生成文件写到一个字符串里面。
读取文件的也不麻烦
Velocity.mergeTemplate(“testtemplate.vm”, context, w );

3,类库加入

因为日期是Date,需要对时间进行格式化。
在web里面可以使用toolbox引入,但是在main函数里面不知道咋加载进去。
找了半天,其实非常简单,直接new一个对象就行。


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

import java.io.StringWriter;
import java.util.Date;

public class CreateHtml2 {
    public static void main(String[] args) {


        VelocityEngine velocityEngine = new VelocityEngine();

        velocityEngine.init();

        Velocity.init();

        /* lets make a Context and put data into it */

        VelocityContext context = new VelocityContext();

        context.put("name", "Velocity");
        context.put("project", "Jakarta");
        context.put("now", new Date());
        context.put("dateFormatUtils", new org.apache.commons.lang.time.DateFormatUtils());

        /* lets make our own string to render */

        String str = "We are using $project $name to render this. 中文测试  $!dateFormatUtils.format($!now,'yyyy-MM-dd')";
        StringWriter stringWriter = new StringWriter();
        Velocity.evaluate(context, stringWriter, "mystring", str);
        System.out.println(" string : " + stringWriter);

    }
}

就一行:
context.put(“dateFormatUtils”, new org.apache.commons.lang.time.DateFormatUtils());

直接把新对象放入进去就可以使用格式化函数了。

$!dateFormatUtils.format($!now,'yyyy-MM-dd')
目录
相关文章
|
4天前
|
Java
Springboot 导出word,动态填充表格数据
Springboot 导出word,动态填充表格数据
|
4天前
|
JavaScript 前端开发
ES6之模板字符串及字符串新增方法
ES6之模板字符串及字符串新增方法
|
10月前
|
JavaScript 前端开发 应用服务中间件
【前端项目笔记】原生js上传文件及文件转换成base64、blob类型
项目中经常会用到上传图片上传视频等功能,由于后端nginx限制,经常要进行文件转化才能上传,大文件可能还要进行切片上传处理。
482 1
|
8月前
|
XML 数据格式
Freemarker填充数据到word模板中
Freemarker填充数据到word模板中
81 1
|
9月前
|
JavaScript
Vue页面转word格式保存
Vue页面转word格式保存
141 0
|
10月前
|
JSON 前端开发 数据格式
前端(二十一):label语句、with语句、JSON、生成器、解析赋值、历史状态管理、将页面可编辑
label语句、with语句、JSON、生成器、解析赋值、历史状态管理、将页面可编辑
110 0
|
XML 数据格式
使用freemarker模板导出word文档
使用freemarker模板导出word文档
402 0
使用freemarker模板导出word文档
|
Java 数据库
FreeMarker 模板来定义字符串模板
FreeMarker 模板来定义字符串模板
311 0
FreeMarker 模板来定义字符串模板