java ant安装配置和页面引擎velocity的使用

java的ant 可以在http://ant.apache.org官网上下载最新版本,在官网首页的左边hom目录,选择download下的Binary Distributions(编译还好的ant,就是ant编译成jar供我们使用),source Distributions是ant的源代码··一般我们使用Binary Distributions下载就可以了。我下载的是1.8.2版本的zip文件格式apache-ant-1.8.2-bin.zip [PGP] [SHA1] [SHA512] [MD5],直接点击apache-ant-1.8.2-bin.zip即可,后面的是协议验证下载文件的完整性,是安全验证,这里是官网,一般就没必要啦·直接下载咯

    下载后解压,然后就是安装了,其实ant安装极为简单,他本身是一个命令行运行,只需要设置java环境变量(就是JAVA_HOME你之前配置过,这里就不用再配置了),ANT_HOM(添加一个系统环境变量,路径是你的解压ant的目录),添加%ANT_HOME%/bin到path的环境变量中,然后打开cmd,直接键入ant就可以运行了。

    如何编译本地java源代码:  使用cmd cd到你的项目中build.xml(里面是些指定编译什么java源代码,编译到何处,指定什么包的配置文件)的目录下,然后直接键入ant,就能编译你build指定的java文件了,如果你没有build.xml,没问题,新建一个test.xml 写入配置信息,然后键入:

ant –buildfile  test.xml,就可以了。

    说白了,ant的主要作用就是帮助我们把java编译成class的工具。

页面引擎velocity

    velocity可以使用自己定义的简单语言代替jsp的<%%>服务端语言,来操作显示前端页面,非常方便

     使用好像也很方便,到官方网站http://velocity.apache.org/download.cgi下载engine velocity-1.7.zip  ,主要使用到里边的bin和lib的包

使用的时候,引入就可以了,使用,我们可以参考他给的例子:

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;

/**
* This class is a simple demonstration of how the Velocity Template Engine
* can be used in a standalone application.
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
* @version $Id: Example.java 463298 2006-10-12 16:10:32Z henning $
*/

public class Example
{
public Example(String templateFile)
{
try
{
/*
* setup
*/

Velocity.init("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 = 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(args[0]);
}
}

       模板 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
编译好后,使用的时候,写一个bat 同时加载他给的包:
set VELCP=.

for %%i in (引擎bin路径\*.jar) do call appendVELCP %%i
for %%i in (引擎bin路径\lib\*.jar) do call appendVELCP %%i

echo Using classpath: %VELCP%

java -cp %VELCP% Example example.vm
在cmd调用 你写的bat 就可以看到结果:
QQ截图未命名

可能大家会问,那怎样在web方便的使用这个引擎呢,我们看一下他给出的readme.txt文档:

Finally, note that more examples of using Velocity can be found in the
Velocity-Tools subject.  Of special note is the VelocityViewServlet,
a quick and easy way to build a web application that uses Velocity.

就是说,我们可以上他的官网下载Velocity-Tools的工具,能帮我们快速方便的使用velocity

于是我下载了velocity-tools-2.0.zip ,目前正在研究这个tools当中····

查了资料:Velocity Tools 是 Velocity 模板引擎的一个子项目,用来将 Velocity 与 Web开发环境集成的工具包。例如你可以利用 VelocityTools 来集成 Velocity 和 Struts 框架,同时 VelocityTools 还提供 Velocity 的布局模板,以及很多常用的工具包。

也就是说,Velocity Tools 就是该引擎的扩展包,提供一些常用的应用和函数处理

小小使用经验,高手掠过

posted @ 2011-01-26 18:01  dodohua  阅读(1437)  评论(0)    收藏  举报