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. EF 新增数据时提示it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element

    it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionM ...

  2. Html常用标签及全称

    <!-- 块标签 divsion --><div></div> <!--br 换行    break--> <br /> <!--分割 ...

  3. React Native组件间通信

    React Native组件间通信 React Native组件的关系有:父子关系.无直接关系.组件间通信主要针对这两类来讨论. 一.父组件和子组件之间通信 父组件向子组件传递消息.数据通过对子组件的 ...

  4. PAT甲级1016Phone Bills

    #include<iostream> #include<cstdio> #include<cstdlib> #include<vector> #incl ...

  5. CSS——样式初始化

    腾讯: body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;p ...

  6. 3星|《腾讯产业森林:AI时代的创业密码》:后半部分是较详细的创业指南,前面泛泛介绍腾讯、AI

    腾讯产业森林:AI时代的创业密码 前半部分泛泛介绍腾讯对创业者的支持,腾讯支持的创业项目的案例.AI的一些基本介绍,后半部分是比较详细的写给创业者的各阶段行动与选择的指南. 总体评价3星,有一些参考价 ...

  7. hibernate工作流程、session

    hibernate是对jdbc的封装,不建议直接使用jdbc的connection操作数据库,而是通过session操作数据库.session可以理解为操作数据库的对象. session与connec ...

  8. 关于在win7旗舰版32位上 安装 net4.0 的闪退问题研究 和安装sqlserver2008问题

    1.配置文件客户端[目标x86x64]的 可以安装 2.配置文件完全的目标x86x64的 出现闪退. 3.服务器核心的出现无法安装 安装 sqlserver 2008R2数据库 报错 \最后留下了它, ...

  9. Origin C调用NAG库

    NAG(Numerical Algorithms Group, www.nag.com)库是一个无与伦比的算法库,它提供的算法可靠.轻便.严谨,覆盖了数学与统计方方面面.最大的缺点就是:它是一个收费的 ...

  10. kvm virt-install 使用小结

    简介: virt-install 能够为KVM.Xen或其它支持libvrit API的hypervisor创建虚拟机并完成GuestOS安装. 此外,它能够基于串行控制台.VNC或SDL支持文本或图 ...