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 集合系列07之 Stack详细介绍(源码解析)和使用示例

    概要 学完Vector了之后,接下来我们开始学习Stack.Stack很简单,它继承于Vector.学习方式还是和之前一样,先对Stack有个整体认识,然后再学习它的源码:最后再通过实例来学会使用它. ...

  2. Java—将文件压缩为zip文件

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  3. CSS——行内元素的margin与padding

    行内元素: 1.margin:0 20px:只可以定义左右. 2.pading:20px 20px 20px 20px:上下左右都有效 例如span: <!DOCTYPE html> &l ...

  4. 转:selenium自动化脚本错误总结

    https://blog.csdn.net/zxy987872674/article/details/53141118

  5. Android本地消息推送

    项目介绍:cocos2dx跨平台游戏 项目需求:实现本地消息推送,需求①:定点推送:需求②:根据游戏内逻辑实现推送(比如玩家体力满时,需要计算后到点推送):需求③:清理后台程序或重启后依然能够实现本地 ...

  6. PowerDesigner16逆向工程生成PDM列注释(My Sql5.0模版)

    一.编辑当前DataBase 选择DataBase——>edit Current DBMS...弹出如下对话框:  如上图,先解释一下: 根据红颜色框从上往下解释一下. 第一个红框是对应的修改的 ...

  7. C# null

    var t0est = Convert.ToString(""+null);//结果"" var t1est = ("" + null).T ...

  8. windows 小知识---windows下生成公钥和私钥

    首先Windows操作系统需要安装git. 安装完成后,再到任意的文件夹内,点击右键.选择git bash here 打开之后,输入ssh-keygen,一路按enter键. 全部结束后,再到C:\U ...

  9. SpringBoot启动报jdbc连接池错误

    如图,启动报连接池错误 项目中没有使用任何连接池,以为没用连接池的原因,所以配置了druid,一开始可以正常启动,但后来重启项目时仍旧报同样的错.网上找了资料,url中加useSSL=false,显式 ...

  10. PAT_A1133#Splitting A Linked List

    Source: PAT A1133 Splitting A Linked List (25 分) Description: Given a singly linked list, you are su ...