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

          雪山飛鵠

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

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            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 雪山飛鵠 閱讀(2410) 評論(1)  編輯  收藏 所屬分類: velocity

          Feedback

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

          主站蜘蛛池模板: 庆云县| 合阳县| 九江市| 巴南区| 敖汉旗| 宽城| 雷山县| 青河县| 聊城市| 庄河市| 塔河县| 阜平县| 沾益县| 白水县| 即墨市| 游戏| 南宫市| 南岸区| 晋州市| 宜昌市| 长海县| 万安县| 屏东县| 灵石县| 墨脱县| 三河市| 蒙阴县| 盐津县| 湾仔区| 潞城市| 综艺| 河津市| 玉田县| 清水河县| 阿尔山市| 凤翔县| 长沙市| 雷州市| 蒙城县| 深州市| 陇川县|