velocity应用

来源:互联网 发布:网络渗透入门 编辑:程序博客网 时间:2024/05/16 19:28

 

下载路径:http://velocity.apache.org/
velocity-1.6-dep.jar
/lib/log4j-1.2.12.jar
如果不使用velocity-1.6-dep.jar,也可以使用velocity-1.6.jar,但这时需要把/lib下的commons-collections-3.2.1.jar/commons-lang-2.4.jaroro-2.0.8.jar入类路径下。 velocity-1.6-dep.jar文件内部已经包含前面三个jar文件的类。
 
 
 
 
在类路径下加入velocity.properties,内容如下:
指定日志文件存放位置
runtime.log = E://spring//velocity//velocity_example.log
指定模版文件加载位置
file.resource.loader.path=E://spring//velocity
指定输入编码格式
input.encoding=UTF-8
指定velocityservlet向浏览器输出内容的编码
default.contentType=text/html; charset/=UTF-8
指定输出编码格式
output.encoding=UTF-8
 
 
 
 
try{
   Velocity.init("src/velocity.properties");
   VelocityContext context = new VelocityContext();
   context.put("hello", "HelloWorld");
   context.put("who", "黎明");
   Template template = Velocity.getTemplate("mytemplate.vm");
   StringWriter sw = new StringWriter();
   template.merge(context, sw);
   sw.flush();
   System.out.println(sw.toString());
}catch( Exception e ){
e.printStackTrace();
}
mytemplate.vm内容如下:
${who}:${hello}
 
 
 
 
原创粉丝点击