velocity 例子

最简单的hello world 程序。
基础搭建:从网上下载velocity.zip,把lib下的.jar文件导入到工程中。另外还要导入velocity.jar 和velocity-dep.jar.
hellosite.vm  放到工程的目录底下。

Hello $name !  Welcome to $site world !

HelloWorld.java

package  com;
import  java.io.StringWriter;
import  org.apache.velocity.Template;
import  org.apache.velocity.VelocityContext;
import  org.apache.velocity.app.VelocityEngine;
public   class  HelloWorld {
    
public   static   void  main(String[] args)  throws  Exception {
        
/*  first, get and initialize an engine  */
        VelocityEngine ve 
=   new  VelocityEngine();
        ve.init();
        
/*  next, get the Template  */
        Template t 
=  ve.getTemplate( " hellosite.vm " );
        
/*  create a context and add data  */
        VelocityContext context 
=   new  VelocityContext();
        context.put(
" name " " DingDangXiaoMa " );
        context.put(
" site " " http://www.sun.com " );
        
/*  now render the template into a StringWriter  */
        StringWriter writer 
=   new  StringWriter();
        t.merge(context, writer);
        
/*  show the World  */
        System.out.println(writer.toString());
    }
}

直接运行这个程序,就可以输入模板中的信息。
2。web程序,servlet 调用vm显示信息。
hello.vm 放在webRoot/web/下。

html >
    
< body  >         
     #set($name
= " Hello ! DingDang " )
        Hello, $name 
        woowoo
    
</ body >
</ html >

servlet 文件:

public   class  SampleServlet  extends  VelocityServlet {
    
protected  Properties loadConfiguration(ServletConfig config)
            
throws  IOException, FileNotFoundException {
        Properties p 
=   new  Properties();
        String path 
=  config.getServletContext().getRealPath( " / " );
        
if  (path  ==   null ) {
            System.out
                    .println(
"  SampleServlet.loadConfiguration() : unable to  "
                            
+   " get the current webapp root.  Using '/'. Please fix. " );
            path 
=   " / " ;
        }
        p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
        p.setProperty(
" runtime.log " , path  +   " velocity.log " );
        
return  p;
    }
    
public  Template handleRequest(HttpServletRequest request,
            HttpServletResponse response, Context ctx) {
        
/**
         * 我的一些测试输出
         
*/
        System.out.println(
" Hello  everyone " );
        System.out.println(
" sfdsfsd " );
        System.out.println(ctx);
        System.out.println(ctx.toString());
        System.out.println(request.getContextPath());
        Template template 
=   null ;

        
try  {
            
/**
             * 主要代码
             
*/
            Velocity.init();
            
/**
             * 注意相对路径
             
*/
            template 
=  Velocity.getTemplate( " /web/hello.vm " );
            VelocityContext context 
=   new  VelocityContext();
            context.put(
" name " " wowowowowowo  hello   hello  hello " );
            
//  template = Velocity.getTemplate("/web/sample.html");
        }  catch  (Exception e) {
            e.printStackTrace();
        }
        
return  template;
    }
}

以上就是两个最简单的velocity的例子。 一个是java application 一个是java web程序。
参考资料:
http://www.javaresearch.org/article/38895.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值