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')
目录
相关文章
|
SQL Java 数据库连接
欲善事先利器——IDEA 插件篇
欲善事先利器——IDEA 插件篇
751 0
|
10月前
|
机器学习/深度学习 人工智能 数据处理
混元开源又+1:视频音效可以自动生成了
AI生成的视频音效,已经可以用于视频制作了。
607 32
混元开源又+1:视频音效可以自动生成了
|
JSON 数据格式
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
解决POSTMAN传参报错,JSON parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OB
|
新能源 API 开发者
车辆限行查询API的实战指南:让限行管理从此 “有码可循”
随着全国机动车保有量突破4.53亿辆,交通拥堵与污染问题日益严峻,各城市陆续实施限行政策。探数API推出的车辆限行查询服务覆盖200+城市,提供实时限行数据,包括本地/外地燃油车及新能源车的限行规则、区域和时间等信息。其功能涵盖单个城市限行政策查询与支持城市的全面列表,助力用户精准规划出行。通过HTTP POST请求即可轻松接入,适用于导航平台和个人开发者。在“双碳”目标下,该API推动绿色出行与智能交通发展,为个人、企业和城市治理提供高效解决方案。
860 5
|
存储 安全 Java
Spring Boot中的文件下载实现
Spring Boot中的文件下载实现
|
缓存 关系型数据库 MySQL
业务中的字典表的MySQL实现方案
业务中的字典表的MySQL实现方案
799 0
业务中的字典表的MySQL实现方案
|
安全 前端开发 Java
微服务技术系列教程(38)- SpringBoot -整合SpringSecurity
微服务技术系列教程(38)- SpringBoot -整合SpringSecurity
418 0
|
Java API Spring
Java实现异步编程的几种方式
通过本文的介绍,我们了解了在Java中实现异步编程的几种常用方式。每种方法都有其优点和适用场景,具体选择哪种方式应根据实际需求和场景决定。如果任务较简单,可以使用 `Thread`或 `ExecutorService`;如果需要处理复杂的异步流程,可以考虑使用 `CompletableFuture`或Reactive编程框架。希望本文对您理解和实现Java异步编程有所帮助。
694 1
|
API Docker 容器
超实用工具分享!Gotenberg,让你的文档转换PDF无缝对接
超实用工具分享!Gotenberg,让你的文档转换PDF无缝对接
1757 4
|
自然语言处理 Java 索引
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
ElasticSearch 实现分词全文检索 - Java SpringBoot ES 文档操作
721 0