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 <%@ ...
随机推荐
- SpringAop--系统日志简例
通过Spring的Aop我们可以声明式的配置事务管理,那么同样可以通过SpringAop来进行处理的系统日志该如何实现呢? 一.数据表和实体类的准备 我们要管理系统日志,那么数据表和实体类是必不可少的 ...
- 【转】Centos7 ftp 配置及报错处理
原文链接: https://www.cnblogs.com/GaZeon/p/5393853.html Centos7网络配置,vsftpd安装及530报错解决 今天在虚拟机安装CentOS7,准备全 ...
- 在已有spring的基础上集成hibernate
1.导入hibernate的包和spring的包 hibernate3.hibernate-jpa-2.0-api-.必须的包,log4j,log4j配置文件 1.1 导入Spring的依赖包 ...
- Zabbix 监控磁盘IO
Zabbix 监控磁盘IO 1.数据获取脚本 #!/bin/bash # resource: http://www.muck.net/19/getting-hard-disk-performance- ...
- Centos6.6 安装rsync服务端
一.介绍 在工作中经常遇到代码分发,或者是资料备份,都会用到rsync,配置不算复杂,仅做下记录,安装环境如下: 1) Centos6.6 2) rsync-3.0.6-12.el6.x86_64 3 ...
- tac
功能说明:反向显示文件内容. 参数选项: -b 在行前而非行尾加分隔标志. -r 将分隔标志视作正则表达式来解析. -s 使用指定字符串代替换行作为分隔标志. cat命令与tac命令的对比:
- 前端自动化构建工具gulp使用
1. 全局安装 gulp: $ npm install --global gulp 2. 作为项目的开发依赖(devDependencies)安装: $ npm install --save-dev ...
- CVPR 2017 Paper list
CVPR2017 paper list Machine Learning 1 Spotlight 1-1A Exclusivity-Consistency Regularized Multi-View ...
- C# 通知机制 IObserver<T> 和 IObservable<T>
class Program { public static void Main() { // Define a provider and two observers. LocationTracker ...
- 兼容ie6的mvvm框架--san
最近,由于公司项目需要,使用百度mvvm框架san开发了一个兼容ie6的小项目.san的优势是强大的兼容性,能兼容到ie6.当初我自己好奇,尝试用san做了一个小型项目,这里记录一下.如果你也想尝试一 ...