velocity简单样例整体实现须要三个步骤,详细例如以下:

1、创建一个Javaproject

2、导入须要的jar包

3、创建须要的文件

============================================

1、创建一个Javaproject

名称:JKTest,例如以下:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG92ZV9qaw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

2、导入须要的jar包

velocity-1.7.jar  velocity-1.7-dep.jar

详细下载地址:

http://yunpan.cn/QTdCXHsIrcDff  訪问password 0029

http://yunpan.cn/QTdC945Nm5PzQ  訪问password e6d2

3、创建须要的文件

Example.java

package jk003;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template; import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException; import java.io.*;
import java.util.ArrayList; public class Example
{
public Example(String templateFile)
{
try
{
/*
* setup
*/ Velocity.init("./src/jk003/velocity.properties"); /*
* Make a context object and populate with the data. This
* is where the Velocity engine gets the data to resolve the
* references (ex. $list) in the template
*/ VelocityContext context = new VelocityContext();
context.put("list", getNames()); /*
* get the Template object. This is the parsed version of your
* template input file. Note that getTemplate() can throw
* ResourceNotFoundException : if it doesn't find the template
* ParseErrorException : if there is something wrong with the VTL
* Exception : if something else goes wrong (this is generally
* indicative of as serious problem...)
*/ Template template = null; try
{
template = Velocity.getTemplate(templateFile);
}
catch( ResourceNotFoundException rnfe )
{
System.out.println("Example : error : cannot find template " + templateFile );
}
catch( ParseErrorException pee )
{
System.out.println("Example : Syntax error in template " + templateFile + ":" + pee );
} /*
* Now have the template engine process your template using the
* data placed into the context. Think of it as a 'merge'
* of the template and the data to produce the output stream.
*/ BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(System.out)); if ( template != null)
template.merge(context, writer); /*
* flush and cleanup
*/ writer.flush();
writer.close();
}
catch( Exception e )
{
System.out.println(e);
}
} public ArrayList getNames()
{
ArrayList list = new ArrayList(); list.add("ArrayList element 1");
list.add("ArrayList element 2");
list.add("ArrayList element 3");
list.add("ArrayList element 4"); return list;
} public static void main(String[] args)
{
Example t = new Example("./src/jk003/example.vm");
}
}

example.vm

#set( $this = "Velocity")

$this is great!

#foreach( $name in $list )

    $name is great!

#end

#set( $condition = true)

#if ($condition)

    The condition is true!

#else

    The condition is false!

#end

velocity.properties

runtime.log = velocity_example.log

最后执行:Example.java

控制台输出:

Velocity is great!

ArrayList element 1 is great!

    ArrayList element 2 is great!

    ArrayList element 3 is great!

    ArrayList element 4 is great!

The condition is true!

velocity简单样例的更多相关文章

  1. extern外部方法使用C#简单样例

    外部方法使用C#简单样例 1.添加引用using System.Runtime.InteropServices; 2.声明和实现的连接[DllImport("kernel32", ...

  2. spring事务详解(二)简单样例

    系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...

  3. 自己定义隐式转换和显式转换c#简单样例

    自己定义隐式转换和显式转换c#简单样例 (出自朱朱家园http://blog.csdn.net/zhgl7688) 样例:对用户user中,usernamefirst name和last name进行 ...

  4. VC6 鼠标钩子 最简单样例

    Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...

  5. gtk+3.0的环境配置及基于gtk+3.0的python简单样例

    /*********************************************************************  * Author  : Samson  * Date   ...

  6. java 使用tess4j实现OCR的最简单样例

    网上很多教程没有介绍清楚tessdata的位置,以及怎么配置,并且对中文库的描述也存在问题,这里介绍一个最简单的样例. 1.使用maven,直接引入依赖,确保你的工程JDK是1.8以上 <dep ...

  7. 使用SALT-API进入集成开发的简单样例

    测试的时候,可以CURL -K,但真正作集成的时候,却是不可以的. 必须,不可以让TOKEN满天飞吧. 现在进入这个阶段了.写个样例先: import salt import salt.auth im ...

  8. VB.net数据库编程(03):一个SQLserver连接查询的简单样例

    这个样例,因为在ADO.net入门已经专门学了,再次进行复习 一下. 主要掌握连接字串的情况. 过程就是: 1.引用System.Data.SqlClient.而Access中引用 的是System. ...

  9. Fileupload-1.2.1使用简单样例

    在測试本例至少须要在web程序的WEB-INF/lib下包括commons-fileupload- 1.2.1和commons-io-1.3.2两个类库. fileupload.jsp <%@ ...

随机推荐

  1. Java常用类库(三) : HashSet和LinkedList特点简析

    今天内容: l  浅撩HashSet集合元素不可重复的原理 l  使用LinkedList模拟栈和队列 1.浅撩HashSet集合元素不可重复的原理 我们知道HashSet是添加不了相同的元素的,其原 ...

  2. fcc 响应式框架Bootstrap 练习3

    class="container-fluid"控制宽度100% <div class="container-fluid"> <h3 class ...

  3. 【技术累积】【点】【java】【25】Orderd

    基础概念 Orderd是spring core中定义的一个接口,使用它以及相关的Comparator和@Order注解,可以实现对元素的排序. @Order 直接先说下@Order注解吧,使用场景较多 ...

  4. 微服务的一种开源实现方式——dubbo+zookeeper

    转自: http://blog.csdn.NET/zhdd99/article/details/52263609 微服务架构成了当下的技术热点,实现微服务是要付出很大成本的,但也许是因为微服务的优点太 ...

  5. Map 键值对 set get delete

  6. css3 animation 中的 steps

    steps Specifies a stepping function, described above, taking two parameters. The first parameter spe ...

  7. 学不好Linux?我们分析看看正确的学习方法是什么-马哥教育

    2018年里,Linux运维的职位数量和平均薪资水平仍然持续了去年的强劲增幅,比很多开发岗位涨的都快.从研究机构的数据来看,Linux职位数量和工资水平涨幅均在IT行业的前五之列,比去年的表现还要好一 ...

  8. 爬虫之cookie

    什么是cookie: 在网站中,http请求是无状态的.也就是说即使第一次和服务器连接后并且登录成功后,第二次请求服务器依然不能知道当前请求是哪个用户.cookie的出现就是为了解决这个问题,第一次登 ...

  9. POJ3984——迷宫问题

    迷宫问题 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 31616   Accepted: 18100 Descriptio ...

  10. 18清明校内测试T1

    消失的数字(number) Time Limit:1000ms   Memory Limit:128MB 题目描述 rsy拥有n个数,这n个数分别是a1,a2,…,an. 后来出现了一个熊孩子zhw, ...