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

          雪山飛鵠

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

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          velocity使用基本來說比較簡單,但在加載模板時(shí)老出問題,很多初學(xué)者經(jīng)常會(huì)遇到找不到模板這種異常。本文就針對目前常用的三種模板加載方式做以說明。
          velocity官方幫助文檔
          其工程目錄結(jié)構(gòu)大致為
          點(diǎn)我下載示例代碼

          一、velocity默認(rèn)的加載方式(文件加載方式)
          package com.velocity.test;

          import java.io.StringWriter;
          import java.util.Properties;

          import org.apache.velocity.VelocityContext;
          import org.apache.velocity.app.VelocityEngine;

          /**
           * 從文件中加載模板文件,即velocity默認(rèn)的模板文件加載方式
           * 
          @author welcome
           *
           
          */
          public class LoaderFromFile {
              
              
          public static void main(String[] args) throws Exception{
                  
          //初始化參數(shù)
                  Properties properties=new Properties();
                  
          //設(shè)置velocity資源加載方式為file
                  properties.setProperty("resource.loader""file");
                  
          //設(shè)置velocity資源加載方式為file時(shí)的處理類
                  properties.setProperty("file.resource.loader.class""org.apache.velocity.runtime.resource.loader.FileResourceLoader");
                  
          //實(shí)例化一個(gè)VelocityEngine對象
                  VelocityEngine velocityEngine=new VelocityEngine(properties);
                  
                  
          //實(shí)例化一個(gè)VelocityContext
                  VelocityContext context=new VelocityContext();
                  
          //向VelocityContext中放入鍵值
                  context.put("username""張三");
                  context.put(
          "password""123456789");
                  context.put(
          "age""20");
                  context.put(
          "address""陜西西安"); 
                  context.put(
          "blog""http://blogjava.net/sxyx2008");
                  
          //實(shí)例化一個(gè)StringWriter
                  StringWriter writer=new StringWriter();
                  
          //從vm目錄下加載hello.vm模板,在eclipse工程中該vm目錄與src目錄平級(jí)
                  velocityEngine.mergeTemplate("vm/hello.vm""gbk", context, writer);
                  System.out.println(writer.toString());
                  
              }
          }
          二、從類路徑加載模板文件
          package com.velocity.test;

          import java.io.StringWriter;
          import java.util.Properties;

          import org.apache.velocity.VelocityContext;
          import org.apache.velocity.app.VelocityEngine;

          /**
           * 從class(類路徑)中加載模板文件
           * 
          @author welcome
           *
           
          */
          public class LoaderFromClass {
              
              
          public static void main(String[] args) throws Exception{
                  
          //初始化參數(shù)
                  Properties properties=new Properties();
                  
          //設(shè)置velocity資源加載方式為class
                  properties.setProperty("resource.loader""class");
                  
          //設(shè)置velocity資源加載方式為file時(shí)的處理類
                  properties.setProperty("class.resource.loader.class""org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
                  
          //實(shí)例化一個(gè)VelocityEngine對象
                  VelocityEngine velocityEngine=new VelocityEngine(properties);
                  
                  
          //實(shí)例化一個(gè)VelocityContext
                  VelocityContext context=new VelocityContext();
                  
          //向VelocityContext中放入鍵值
                  context.put("username""張三");
                  context.put(
          "password""123456789");
                  context.put(
          "age""20");
                  context.put(
          "address""陜西西安"); 
                  context.put(
          "blog""http://blogjava.net/sxyx2008");
                  
          //實(shí)例化一個(gè)StringWriter
                  StringWriter writer=new StringWriter();
                  
                  
          //從src目錄下加載hello.vm模板
                  
          //假若在com.velocity.test包下有一個(gè)hello.vm文件,那么加載路徑為com/velocity/test/hello.vm
                  velocityEngine.mergeTemplate("com/velocity/test/hello.vm""gbk", context, writer);
                  
                  
          //velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer);
                  System.out.println(writer.toString());
              }
          }
          三、從jar文件中加載模板文件
          package com.velocity.test;

          import java.io.StringWriter;
          import java.util.Properties;

          import org.apache.velocity.VelocityContext;
          import org.apache.velocity.app.VelocityEngine;

          /**
           * 從jar文件中加載模板文件
           * 
          @author welcome
           *
           
          */
          public class LoaderFromJar {
              
              
          public static void main(String[] args) throws Exception{
                  
          //初始化參數(shù)
                  Properties properties=new Properties();
                  
          //設(shè)置velocity資源加載方式為jar
                  properties.setProperty("resource.loader""jar");
                  
          //設(shè)置velocity資源加載方式為file時(shí)的處理類
                  properties.setProperty("jar.resource.loader.class""org.apache.velocity.runtime.resource.loader.JarResourceLoader");
                  
          //設(shè)置jar包所在的位置
                  properties.setProperty("jar.resource.loader.path""jar:file:WebRoot/WEB-INF/lib/vm.jar");
                  
          //實(shí)例化一個(gè)VelocityEngine對象
                  VelocityEngine velocityEngine=new VelocityEngine(properties);
                  
                  
          //實(shí)例化一個(gè)VelocityContext
                  VelocityContext context=new VelocityContext();
                  
          //向VelocityContext中放入鍵值
                  context.put("username""張三");
                  context.put(
          "password""123456789");
                  context.put(
          "age""20");
                  context.put(
          "address""陜西西安"); 
                  context.put(
          "blog""http://blogjava.net/sxyx2008");
                  
          //實(shí)例化一個(gè)StringWriter
                  StringWriter writer=new StringWriter();
                  
          //從/WebRoot/WEB-INF/lib/vm.jar中加載hello.vm模板  vm.jar的目錄結(jié)構(gòu)為vm/hello.vm
                  velocityEngine.mergeTemplate("vm/hello.vm""gbk", context, writer);
                  System.out.println(writer.toString());
              }
          }

          請讀者朋友自行運(yùn)行程序。
          點(diǎn)我下載示例代碼
          posted on 2010-11-11 13:29 雪山飛鵠 閱讀(12479) 評(píng)論(8)  編輯  收藏 所屬分類: velocity

          Feedback

          # re: velocity模板加載 2010-11-30 16:30 伏筆
          給你再補(bǔ)充一種變通的方式:
          pro.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, this.getClass().getResource("/").toString().replaceAll("^file:/", "").replaceAll("WEB-INF/classes","vm"));  回復(fù)  更多評(píng)論
            

          # re: velocity模板加載 2010-12-14 17:51 房子
          請教個(gè)文件加載的問題,
          velocity怎么也加載不到我硬盤上的模板,
          后臺(tái)報(bào):
          嚴(yán)重: ResourceManager : unable to find resource 'C:\Downloads\template\modelNameInFileName.java.vm' in any resource loader.

          是我加載器的問題嗎
            回復(fù)  更多評(píng)論
            

          # re: velocity模板加載 2013-02-19 22:15 hell
          如果是Web項(xiàng)目,加載的模板在WEB-INF下,又該如何獲取模板呢?請教?  回復(fù)  更多評(píng)論
            

          # re: velocity模板加載 2013-02-19 22:29 hell
          @伏筆
          如果是Web項(xiàng)目,加載的模板在WEB-INF下,又該如何獲取模板呢?請教?@伏筆
            回復(fù)  更多評(píng)論
            

          # re: velocity模板加載 2013-02-20 13:21 coo
          @房子
          解決了?  回復(fù)  更多評(píng)論
            

          # re: velocity模板加載[未登錄] 2013-05-18 21:33 frank
          這個(gè)文章 寫得不錯(cuò),謝謝!  回復(fù)  更多評(píng)論
            

          # re: velocity模板加載[未登錄] 2013-11-20 16:32 你好
          請問如果模板被存儲(chǔ)到數(shù)據(jù)庫中取出后是流怎么加載??能不能直擊加載流???  回復(fù)  更多評(píng)論
            

          # re: velocity模板加載[未登錄] 2013-11-20 16:33 你好
          我郵箱744946073@qq.com謝謝大神了,我急用
            回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 大田县| 芦山县| 奉节县| 南华县| 祁阳县| 兴安盟| 班玛县| 湖南省| 沿河| 夏津县| 宜宾市| 阿图什市| 临沂市| 泸西县| 南漳县| 云龙县| 陇西县| 色达县| 东源县| 石狮市| 黎城县| 乌拉特前旗| 加查县| 普定县| 枣庄市| 合水县| 莱州市| 城固县| 襄樊市| 西吉县| 盱眙县| 隆子县| 温州市| 恩平市| 新和县| 张家口市| 京山县| 德庆县| 正安县| 惠来县| 宣汉县|