1.首先下载velocity .下载地址 http://velocity.apache.org/
2.下载tomcat 6.0 (windows Service installer),安装在D:\Program Files\Apache Software Foundation\
3.在MyEclipse 里创建一个WebSite vtl。默认的website的文件夹是WebRoot.
在WebRoot里添加下面testVtlclass
package
com.cl.vtl;
import
java.io.PrintWriter;
import
java.io.IOException;
import
java.util.Properties;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
org.apache.velocity.Template;
import
org.apache.velocity.VelocityContext;
import
org.apache.velocity.app.Velocity;
import
java.io.BufferedWriter;
import
java.io.OutputStreamWriter;
public
class
testVtl
extends
HttpServlet {
public
void
doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException
{
try
{
/*
Properties props = new Properties();
props.setProperty(Velocity.INPUT_ENCODING, "GBK");
props.setProperty(Velocity.OUTPUT_ENCODING, "GBK");
props.setProperty(Velocity.RESOURCE_LOADER, "file");
props.setProperty("file.resource.loader.description", " Velocity File Resource Loader");
props.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
props.setProperty("file.resource.loader.path", "D:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/vtl");
props.setProperty("file.resource.loader.cache", "true");
props.setProperty("file.resource.loader.modificationCheckInterval", "1");
props.setProperty("directive.foreach.counter.name", "velocityCount");
props.setProperty("directive.foreach.counter.initial.value", "1");
*/
Properties p
=
new
Properties();
p.setProperty(
"
resource.loader
"
,
"
class
"
);
p.setProperty(
"
class.resource.loader.description
"
,
"
Velocity Classpath Resource Loader
"
);
//
"设置vm存放路径,这里设置到放在Classes目录。"
p.setProperty(
"
class.resource.loader.class
"
,
"
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
"
);
p.setProperty (
"
runtime.log.logsystem.class
"
,
"
org.apache.velocity.runtime.log.NullLogSystem
"
);
Velocity.init(p);
//
初始化模板设置
//
Velocity.init("velocity.properties");
Template template
=
Velocity.getTemplate(
"
/tm/index.vm
"
,
"
GBK
"
);
//
调用模板文件
VelocityContext context
=
new
VelocityContext();
//
建立上下文链接
context.put(
"
s
"
,
new
String [] {
"
redwood
"
,
"
maple
"
,
"
oak
"
,
"
pine
"
});
BufferedWriter writer
=
new
BufferedWriter(
new
OutputStreamWriter(response.getOutputStream(),
"
GBK
"
));
if
( context
!=
null
)
template.merge(context, writer);
//
输出
writer.flush();
writer.close();
}
catch
(Exception ex)
{
ex.printStackTrace();
}
}
public
void
doPost(HttpServletRequest req, HttpServletResponse resp)
throws
ServletException, IOException
{
this
.doGet(req,resp) ;
}
}
2.web.xml配置
<?
xml version="1.0" encoding="ISO-8859-1"
?>
<!
DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"
>
<
web-app
>
<
display-name
>
Welcome to Tomcat
</
display-name
>
<
description
>
Welcome to Tomcat
</
description
>
<!--
JSPC servlet mappings start
-->
<
servlet
>
<
servlet-name
>
testVtl
</
servlet-name
>
<
servlet-class
>
com.cl.vtl.testVtl
</
servlet-class
>
</
servlet
>
<
servlet-mapping
>
<
servlet-name
>
testVtl
</
servlet-name
>
<
url-pattern
>
/index
</
url-pattern
>
</
servlet-mapping
>
<!--
JSPC servlet mappings end
-->
</
web-app
>
3.classpath设置
velocity-1.6.3.jar ,velocity-1.6.3-dep.jar复制到Tomcat 6.0\lib目录。 以下是我的classpath设置
.;%JAVA_HOME%\lib\tools.jar;%JAVA_HOME%\lib\dt.jar;D:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar;D:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\velocity-1.6.3.jar;D:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\velocity-1.6.3-dep.jar;
4.在Tomcat webapps 目录里新建 vtl目录
将myeclipse 里的程序复制到Tomcat 6.0 webapps 的vtl目录。
5. 在WEB-INF 目录的Classes新建/tm/index.vm文件。这是模板文件存放路径。
<table border=1>
#foreach($n in $s)
<tr><td>
$n is a big tree!
</td></tr>
#end
</table>
6.在Src目录里新建一个comple.bat文件
写入 javac -d ..\WEB-INF\classes com\cl\vtl\*.java
可以通过这个bat文件来编译。也可以通过cmd里通过javac -d ..\WEB-INF\classes com\cl\vtl\*.java(注意此命令在vtl/src里执行)
7.输入http://loalhost:8080/vtl/index就可以看到输出内容了。
如果tomcat的浏览器里出现空白页,可以在tomcat的logs目录里查看stdout.log文件查看出错信息。
实例代码
------------------------------------------------------------------
1.velocity 设置指定文件位置读取vm文件
Properties p = new Properties();
p.setProperty(Velocity.RESOURCE_LOADER, "file");
p.setProperty("file.resource.loader.description", " Velocity File Resource Loader");
p.setProperty("file.resource.loader.path", "D:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/test");
p.setProperty ("runtime.log.logsystem.class" ,"org.apache.velocity.runtime.log.NullLogSystem");
Velocity.init(p);
2. velocity设置 在classes文件目录读取vm文件
Properties p = new Properties();
p.setProperty("resource.loader", "class");
p.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
p.setProperty ("runtime.log.logsystem.class" ,"org.apache.velocity.runtime.log.NullLogSystem");
Velocity.init(p);//初始化模板设置