velocity简单样例
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简单样例的更多相关文章
- extern外部方法使用C#简单样例
外部方法使用C#简单样例 1.添加引用using System.Runtime.InteropServices; 2.声明和实现的连接[DllImport("kernel32", ...
- spring事务详解(二)简单样例
系列目录 spring事务详解(一)初探事务 spring事务详解(二)简单样例 spring事务详解(三)源码详解 spring事务详解(四)测试验证 spring事务详解(五)总结提高 一.引子 ...
- 自己定义隐式转换和显式转换c#简单样例
自己定义隐式转换和显式转换c#简单样例 (出自朱朱家园http://blog.csdn.net/zhgl7688) 样例:对用户user中,usernamefirst name和last name进行 ...
- VC6 鼠标钩子 最简单样例
Windows系统是建立在事件驱动的机制上的,说穿了就是整个系统都是通过消息的传递来实现的.而钩子是Windows系统中非常重要的系统接口,用它能够截获并处理送给其它应用程序的消息,来完毕普通应用程序 ...
- gtk+3.0的环境配置及基于gtk+3.0的python简单样例
/********************************************************************* * Author : Samson * Date ...
- java 使用tess4j实现OCR的最简单样例
网上很多教程没有介绍清楚tessdata的位置,以及怎么配置,并且对中文库的描述也存在问题,这里介绍一个最简单的样例. 1.使用maven,直接引入依赖,确保你的工程JDK是1.8以上 <dep ...
- 使用SALT-API进入集成开发的简单样例
测试的时候,可以CURL -K,但真正作集成的时候,却是不可以的. 必须,不可以让TOKEN满天飞吧. 现在进入这个阶段了.写个样例先: import salt import salt.auth im ...
- VB.net数据库编程(03):一个SQLserver连接查询的简单样例
这个样例,因为在ADO.net入门已经专门学了,再次进行复习 一下. 主要掌握连接字串的情况. 过程就是: 1.引用System.Data.SqlClient.而Access中引用 的是System. ...
- Fileupload-1.2.1使用简单样例
在測试本例至少须要在web程序的WEB-INF/lib下包括commons-fileupload- 1.2.1和commons-io-1.3.2两个类库. fileupload.jsp <%@ ...
随机推荐
- 如何下载JDK和JRE历史版本
首先进入网址http://www.oracle.com/technetwork/java/javase/downloads/index.html 然后页面滑到最下面,选择[Java Archive]后 ...
- VS2015环境配置: VS2015 未能正确加载“ResourceManagerPackage”包的问题
启动vs2015专业版时,出现类似于这样的提示框,有好几个,点击是或否,但下次打开还是会出现.笔者也寻找了网上的一些解决办法,例如用vs命令窗口或其他,但都无疾而终,下面提供的这个办法,顺利解决此问题 ...
- nodejs全局安装路径的位置
一般nodejs安装在默认的C盘,如果不知道安装在哪里,可以打开控制面板-系统和安全-系统-高级配置中找到 所谓全局安装: 是指安装在node中node_module的根目录里,可以在电脑的任何位置调 ...
- 在mac上快捷找到nodejs的安装路径
打开控制台输入 which node ,得到的输出结果就是node安装路径
- CentOS下使用yum安装配置和使用svn
安装说明 系统环境:CentOS-6.3安装方式:yum install (源码安装容易产生版本兼容的问题)安装软件:系统自动下载SVN软件 检查已安装版本 ? 1 2 3 4 5 6 7 8 9 1 ...
- httpd-vhosts.conf
## VirtualHost example:# Almost any Apache directive may go into a VirtualHost container.# The first ...
- linq 升序排序 空值放后面并根据另一个字段进行多重排序
List<PickingInfo> res = GetDatas(); var _d = (from e in res select new { aa = e.pickingLibrary ...
- 【Linq】
" }; var l1 = strs.ToLookup(a => "a"); //Key=a elements=1,3 var l2 = strs.ToLookup ...
- [CodeForces] CF226D The table
Harry Potter has a difficult homework. Given a rectangular table, consisting of n × m cells. Each ce ...
- 8.1.1 Connection 对象
Connect是sqllite3模块中最基本的也是最重要的一个类,其主要方法如下表所示: 方法 说明 execute(sql[,parameters]) 执行一条SQL语句 executemany(s ...