zoukankan      html  css  js  c++  java
  • Velocity之Hello World(入门实例)

    参考:http://blog.csdn.net/mengxuwq/article/details/1871161 (非常感谢这篇文章,让我初步入门)


    自己调试完全能运行后,写在此,供新人参考,供自己温故


    1、准备模板文件

    //如下为hellosite.vm

    Hello $name! Welcome to $site world!

    2、准备Java文件  (补充说明:hellosite.vm、HelloWorld.java放在同一目录下)

    //如下为HelloWorld.java

    import java.io.StringWriter;
    import org.apache.velocity.app.VelocityEngine;
    import org.apache.velocity.Template;
    import org.apache.velocity.VelocityContext;
    
    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", "Eiffel Qiu");
    	   context.put("site", "http://www.j2medev.com");
    	   
    	   /* now render the template into a StringWriter */
    	   StringWriter writer = new StringWriter();
    	   t.merge( context, writer );
    	   
    	   /* show the World */
    	   System.out.println( writer.toString() ); 
    	}
    }

    3、配置Velocity所需要的jar包(本文的调试基于 velocity-1.6.3)

    3.1 下载jar包

       http://velocity.apache.org/download.cgi ——>Older releases ——>最终到达 http://archive.apache.org/dist/velocity/engine/1.6.3/,找到 velocity-1.6.3.zip 下载


    3.1 解压velocity-1.6.3.zip ,发现解压后的目录下存在velocity-1.6.3.jar、velocity-1.6.3-dep.jar

    3.2 配置classpath环境变量,引入velocity-1.6.3.jar、velocity-1.6.3-dep.jar


    4、编译运行上述Java文件,结果如下:

    D:聚划算技术部编程练习Velocity estVelocity>java HelloWor
    Hello Eiffel Qiu! Welcome to http://www.j2medev.com world!


    后记:

           1、调试过程中,刚开始在classpath中只设置了velocity-1.6.3.jar,调试后发现缺class,缺的class正好在velocity-1.6.3-dep.jar中,引入后问题解决


  • 相关阅读:
    修改CentOs开机启动时的timeout
    各种快捷键
    测试实例异常
    测试实例异常
    springBoot中测试类的头注解
    学习笔记9
    stat命令的实现-mystat
    反汇编测试
    学习笔记7
    openssl截图
  • 原文地址:https://www.cnblogs.com/wxdlut/p/3246938.html
Copyright © 2011-2022 走看看