溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫此文的意義!!!煩請您在轉(zhuǎn)載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          Java code

          package com.velocity.test;
          import java.io.IOException;
          import java.io.StringWriter;

          import org.apache.velocity.Template;
          import org.apache.velocity.VelocityContext;
          import org.apache.velocity.app.Velocity;
          import org.apache.velocity.app.VelocityEngine;
          import org.apache.velocity.exception.MethodInvocationException;
          import org.apache.velocity.exception.ParseErrorException;
          import org.apache.velocity.exception.ResourceNotFoundException;


          public class VelocityTest {

           public static void main(String[] args) {
            
            
            try {
             Velocity.init();
            } catch (Exception e) {
             e.printStackTrace();
            }
            
            VelocityEngine engine=new VelocityEngine();
            engine.setProperty(Velocity.RESOURCE_LOADER, "class");
            engine.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
            
            VelocityContext context= new VelocityContext();
            context.put("name", "林心如");
            context.put("velocity", "Velocity");
            
            Template template = null;
            try {
             template = engine.getTemplate("velocity.vm","gbk");
            } catch (ResourceNotFoundException e) {
             e.printStackTrace();
            } catch (ParseErrorException e) {
             e.printStackTrace();
            } catch (Exception e) {
             e.printStackTrace();
            }
            
            StringWriter writer=new StringWriter();
            
            try {
             template.merge(context, writer);
             System.out.println(writer.toString());
            } catch (ResourceNotFoundException e) {
             e.printStackTrace();
            } catch (ParseErrorException e) {
             e.printStackTrace();
            } catch (MethodInvocationException e) {
             e.printStackTrace();
            } catch (IOException e) {
             e.printStackTrace();
            }
           }

          }


          Velocity 模版
          Hello $name ,
           this is my first $velocity !!!
          #set($sex="女")
          *******************************************
          $sex
          #**
           #set($a=true)
           
           #if($a)
            true
           #else
            false
           #end
           
           #set()
          *#

          Jar包支持
          avalon-logkit-2.1.jar
          commons-collections-3.2.1.jar
          commons-lang-2.4.jar
          oro-2.0.8.jar
          velocity-1.6.3.jar
          posted on 2010-02-23 10:36 雪山飛鵠 閱讀(2418) 評論(1)  編輯  收藏 所屬分類: velocity

          Feedback

          # re: First Velocity Demo 2014-09-15 22:10 zuidaima
          velocity demo教程源代碼下載:http://zuidaima.com/share/kvelocity-p1-s1.htm  回復  更多評論
            

          主站蜘蛛池模板: 威远县| 措美县| 江都市| 龙陵县| 皮山县| 洛阳市| 襄汾县| 磐石市| 青阳县| 高青县| 綦江县| 西昌市| 三亚市| 岚皋县| 中西区| 紫云| 枣强县| 临潭县| 雅江县| 远安县| 宣城市| 于田县| 临湘市| 伊春市| 南开区| 措美县| 白城市| 工布江达县| 福州市| 灵寿县| 尤溪县| 饶平县| 平乡县| 寿宁县| 外汇| 龙川县| 新和县| 新河县| 论坛| 霍山县| 常山县|