温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

BlogJava 首页 新随笔 联系 聚合 管理
  215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
velocity使用基本来说比较简单,但在加载模板时老出问题,很多初学者经常会遇到找不到模板这种异常。本文就针对目前常用的三种模板加载方式做以说明。
velocity官方帮助文档
其工程目录结构大致为
点我下载示例代码

一、velocity默认的加载方式(文件加载方式)
package com.velocity.test;

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

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

/**
 * 从文件中加载模板文件,即velocity默认的模板文件加载方式
 * 
@author welcome
 *
 
*/
public class LoaderFromFile {
    
    
public static void main(String[] args) throws Exception{
        
//初始化参数
        Properties properties=new Properties();
        
//设置velocity资源加载方式为file
        properties.setProperty("resource.loader""file");
        
//设置velocity资源加载方式为file时的处理类
        properties.setProperty("file.resource.loader.class""org.apache.velocity.runtime.resource.loader.FileResourceLoader");
        
//实例化一个VelocityEngine对象
        VelocityEngine velocityEngine=new VelocityEngine(properties);
        
        
//实例化一个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");
        
//实例化一个StringWriter
        StringWriter writer=new StringWriter();
        
//从vm目录下加载hello.vm模板,在eclipse工程中该vm目录与src目录平级
        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{
        
//初始化参数
        Properties properties=new Properties();
        
//设置velocity资源加载方式为class
        properties.setProperty("resource.loader""class");
        
//设置velocity资源加载方式为file时的处理类
        properties.setProperty("class.resource.loader.class""org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        
//实例化一个VelocityEngine对象
        VelocityEngine velocityEngine=new VelocityEngine(properties);
        
        
//实例化一个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");
        
//实例化一个StringWriter
        StringWriter writer=new StringWriter();
        
        
//从src目录下加载hello.vm模板
        
//假若在com.velocity.test包下有一个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{
        
//初始化参数
        Properties properties=new Properties();
        
//设置velocity资源加载方式为jar
        properties.setProperty("resource.loader""jar");
        
//设置velocity资源加载方式为file时的处理类
        properties.setProperty("jar.resource.loader.class""org.apache.velocity.runtime.resource.loader.JarResourceLoader");
        
//设置jar包所在的位置
        properties.setProperty("jar.resource.loader.path""jar:file:WebRoot/WEB-INF/lib/vm.jar");
        
//实例化一个VelocityEngine对象
        VelocityEngine velocityEngine=new VelocityEngine(properties);
        
        
//实例化一个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");
        
//实例化一个StringWriter
        StringWriter writer=new StringWriter();
        
//从/WebRoot/WEB-INF/lib/vm.jar中加载hello.vm模板  vm.jar的目录结构为vm/hello.vm
        velocityEngine.mergeTemplate("vm/hello.vm""gbk", context, writer);
        System.out.println(writer.toString());
    }
}

请读者朋友自行运行程序。
点我下载示例代码
posted on 2010-11-11 13:29 雪山飞鹄 阅读(12432) 评论(8)  编辑  收藏 所属分类: velocity

Feedback

# re: velocity模板加载 2010-11-30 16:30 伏笔
给你再补充一种变通的方式:
pro.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, this.getClass().getResource("/").toString().replaceAll("^file:/", "").replaceAll("WEB-INF/classes","vm"));  回复  更多评论
  

# re: velocity模板加载 2010-12-14 17:51 房子
请教个文件加载的问题,
velocity怎么也加载不到我硬盘上的模板,
后台报:
严重: ResourceManager : unable to find resource 'C:\Downloads\template\modelNameInFileName.java.vm' in any resource loader.

是我加载器的问题吗
  回复  更多评论
  

# re: velocity模板加载 2013-02-19 22:15 hell
如果是Web项目,加载的模板在WEB-INF下,又该如何获取模板呢?请教?  回复  更多评论
  

# re: velocity模板加载 2013-02-19 22:29 hell
@伏笔
如果是Web项目,加载的模板在WEB-INF下,又该如何获取模板呢?请教?@伏笔
  回复  更多评论
  

# re: velocity模板加载 2013-02-20 13:21 coo
@房子
解决了?  回复  更多评论
  

# re: velocity模板加载[未登录] 2013-05-18 21:33 frank
这个文章 写得不错,谢谢!  回复  更多评论
  

# re: velocity模板加载[未登录] 2013-11-20 16:32 你好
请问如果模板被存储到数据库中取出后是流怎么加载??能不能直击加载流???  回复  更多评论
  

# re: velocity模板加载[未登录] 2013-11-20 16:33 你好
我邮箱744946073@qq.com谢谢大神了,我急用
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: