velocity模板加载
http://hi.baidu.com/ly_dayu/item/828b09c5c3c5e547a8ba9409
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());
- }
- }
velocity模板路径又一解http://www.blogjava.net/patterns/archive/2006/11/28/velocity_template_path_another_method.html
研究hibernatesynchronizer的源码,看到他将velocity模板和编译的类一起打包在jar包中,在获得模板时使用
- Xobject.class.getClassLoader().getResourceAsStream("/templates/xx.vm")
获得流,然后再将转变成字符串
- public static String getStringFromStream(InputStream is) throws IOException {
- if (null == is)
- return null;
- try {
- InputStreamReader reader = new InputStreamReader(is);
- char[] buffer = new char[1024];
- StringWriter writer = new StringWriter();
- int bytes_read;
- while ((bytes_read = reader.read(buffer)) != -1) {
- writer.write(buffer, 0, bytes_read);
- }
- return (writer.toString());
- } finally {
- if (null != is)
- is.close();
- }
- }
最后调用velocity的方法
- Velocity.evaluate(Context context, java.io.Writer out, java.lang.String logTag, java.lang.String instring)
从而生成文件。居然不知道velocity有这样的方法,挺无知的,为了路径焦头烂额,终于得解了。总结一下技巧:
1、Xobject.class.getClassLoader().getResourceAsStream("/templates/xx.vm")相对路径获得流;
2、Velocity.evaluate(...)方法使用;
velocity模板路径: http://zhyt710.iteye.com/blog/235250
遇到的velocity加载模板时的路径问题。
于是查阅资料解决。最后综合velocity自己带的例子的example1和example2,改写了一个例子。怎样解决的在例子的注释中已经说的很明确。对于初学velocity的同志来说,这个例子可以是你参照学习的良好实例
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- import java.io.BufferedWriter;
- import java.io.OutputStreamWriter;
- import java.io.StringWriter;
- import java.util.ArrayList;
- import java.util.Properties;
- 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;
- /**
- * This class is a simple demonstration of how the Velocity Template Engine
- * can be used in a standalone application using the Velocity utility class.
- *
- * It demonstrates two of the 'helper' methods found in the org.apache.velocity.util.Velocity
- * class, mergeTemplate() and evaluate().
- *
- *
- * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
- * @version $Id: Example2.java 463298 2006-10-12 16:10:32Z henning $
- */
- public class Example2
- {
- public static ArrayList getNames()
- {
- ArrayList list = new ArrayList();
- list.add("ArrayList element 1");
- list.add("ArrayList element 2");
- list.add("ArrayList element 3");
- list.add("ArrayList element 4");
- return list;
- }
- public static void main( String args[] )
- {
- /* first, we init the runtime engine. Defaults are fine. */
- Properties p = new Properties();
- //设置输入输出编码类型。和这次说的解决的问题无关
- p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
- p.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
- //这里加载类路径里的模板而不是文件系统路径里的模板
- p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
- //也可以用下面方法指定一个绝对路径,不过这样要求你所有的模板都放在该路径下,是有局限的
- //p.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "模板路径");
- try
- {
- Velocity.init(p);
- }
- catch(Exception e)
- {
- System.out.println("Problem initializing Velocity : " + e );
- return;
- }
- /* lets make a Context and put data into it */
- VelocityContext context = new VelocityContext();
- context.put("name", "Velocity");
- context.put("project", "阿帕奇");
- context.put("list", getNames());
- /* lets render a template */
- StringWriter w = new StringWriter();
- try
- {
- Velocity.mergeTemplate("example2.vm", "UTF-8", context, w );
- }
- catch (Exception e )
- {
- System.out.println("Problem merging template : " + e );
- }
- System.out.println(" template : " + w );
- /*
- * lets dynamically 'create' our template
- * and use the evaluate() method to render it
- */
- //这个例子也同时告诉我们可以先从文件系统读取一个文件到字符串,然后进行我们想要的操作
- String s = "We are using $project $name to render this.";
- w = new StringWriter();
- try
- {
- Velocity.evaluate( context, w, "mystring", s );
- }
- catch( ParseErrorException pee )
- {
- /*
- * thrown if something is wrong with the
- * syntax of our template string
- */
- System.out.println("ParseErrorException : " + pee );
- }
- catch( MethodInvocationException mee )
- {
- /*
- * thrown if a method of a reference
- * called by the template
- * throws an exception. That won't happen here
- * as we aren't calling any methods in this
- * example, but we have to catch them anyway
- */
- System.out.println("MethodInvocationException : " + mee );
- }
- catch( Exception e )
- {
- System.out.println("Exception : " + e );
- }
- System.out.println(" string : " + w );
- ///////////////////////////////////////////////////////
- //其他方法: 1分别指定路径,此方法可以设定不同的路径 (也可是相对的。在eclipse下是工程目录)
- try {
- VelocityEngine velocityEngine = new VelocityEngine();
- Properties properties = new Properties();
- //也可以在这里指定绝对路径。当指定相对路径时, 在不同的环境下是有区别的。
- //比如把程序部署到tomcat以后,相对路径相对到哪里是个很恶心的事情。
- String basePath = "vm";
- //可设置绝对路径
- //String basePath = "F:/";
- properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, basePath);
- velocityEngine.init(properties);
- Template template = velocityEngine.getTemplate("example2.vm");
- BufferedWriter writer = new BufferedWriter(
- new OutputStreamWriter(System.out));
- template.merge(context, writer);
- writer.flush();
- writer.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
velocity模板加载的更多相关文章
- Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式: a. 加载classpath目录下的vm文件 /** * 初始化Velocity引擎 * --VelocityEngine是单例模式,线程安全 * @th ...
- angular模板加载 ----ng-template
Angularjs作为mvc(或者说mvvm)框架,同样具备模板这一基本概念. NG加载模板的顺序为 内存加载---AJAX加载. 如果排版乱掉,请查阅https://www.zybuluo.com/ ...
- 转 Velocity中加载vm文件的三种方式
Velocity中加载vm文件的三种方式 velocitypropertiespath Velocity中加载vm文件的三种方式: 方式一:加载classpath目录下的vm文件 Prope ...
- freemarker模板加载TemplateLoader常见方式
使用过freemarker的肯定其见过如下情况: java.io.FileNotFoundException: Template xxx.ftl not found. 模板找不到.可能你会认为我明明指 ...
- wordpress模板加载顺序汇总
我们要创建一个新的wordpress模板需要先了解有哪些页面模板,这些页面模板的文件是什么?它们是怎么工作的?下面ytkah汇总了一些常用的wordpress模板结构方便大家查找 首页 首先WordP ...
- Jquery使用ajax以及angularjs 动态模板加载并进行渲染
1. 源码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <tit ...
- nodejs模板加载的问题
JADE模板:http://jumplink.github.io/jade2html2jade/ HTML转JADE,跟imooc上一步步来就搞定了 直接加载HTML静态资源: var express ...
- Django之--模板加载图片
在使用Django加载图片时遇到了一些问题,在模板html文件中无论使用绝对路径还是当前相对路径都无法找到图片,一直报403和404的错误,后来结合官网和网上的其他资料总算是成功了,这里记下来. 参考 ...
- CI模板加载css和js
1.需求 ci无法加载css和js文件. 2.解决 删除..htaccess文件. 在config目录下配置base_url,并传给页面 $base_url = $this->config-&g ...
随机推荐
- Java程序如何生成Jar 执行文件(2)
一.用Eclipse生产Jar文件 注意:此方法可以打包含有第三方jar包的项目 1. 首先,右键你的Java工程,选择Export,在Java文件夹下选择Runnable JAR file,如下图所 ...
- idea中创建web项目搭建Hibernate框架连接oracle数据库
hibernate框架 hibernate是数据化持久工具,也是一个开源代码的ORM解决方案.hibernate内部封装了通过jdbc访问数据库的操作,向商场应用提供面向对象的数据访问api. hib ...
- js-scroll判断页面是向上滚动还是向下滚动
原理:那当前的scrollTop和之前的scrollTop对比 如果变大了,表示向下滚动(scrollTop值变大): 如果变小了,表示向上滚动(scrollTop值变小). 方法一:js代码: $( ...
- python-映射·字典
1.创建字典:字典由键值对组成,每个键值对就是字典的一个元素,键值对之间用分号(:)隔开,元素之间用逗号(,)隔开.字典中的键必须是唯一 且不可变得(不可以是列表或者字典).字典中的元素是无序的. d ...
- py函数初识
一. 什么是函数 1. 我们到目前为止, 已经可以完成一些软件的基础功能了. 那么我们来完成这样一个功能: 约x print("拿出手机") print("打开陌&quo ...
- 怎么修复网站漏洞 骑士cms的漏洞修复方案
骑士CMS是国内公司开发的一套开源人才网站系统,使用PHP语言开发以及mysql数据库的架构,2019年1月份被某安全组织检测出漏洞,目前最新版本4.2存在高危网站漏洞,通杀SQL注入漏洞,利用该网站 ...
- MySQL server has gone away报错原因分析及解决办法
原因1. MySQL 服务宕了 判断是否属于这个原因的方法很简单,执行以下命令,查看mysql的运行时长 $ mysql -uroot -p -e "show global status l ...
- 8-C++远征之继承篇-学习笔记
C++远征之继承篇 开篇介绍 整个C++远征计划: 起航->离港->封装->继承 为什么要用继承? 为什么要有继承? 如何来定义基类 <----> 派生类? 基类到派生类 ...
- 嵌入式框架Zorb Framework搭建六:定时器的实现
我是卓波,我是一名嵌入式工程师,我万万没想到我会在这里跟大家吹牛皮. 嵌入式框架Zorb Framework搭建过程 嵌入式框架Zorb Framework搭建一:嵌入式环境搭建.调试输出和建立时间系 ...
- 洛谷P4016 负载平衡问题
题目描述 G 公司有 n 个沿铁路运输线环形排列的仓库,每个仓库存储的货物数量不等.如何用最少搬运量可以使 n 个仓库的库存数量相同.搬运货物时,只能在相邻的仓库之间搬运. 输入输出格式 输入格式: ...