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;
-
- public class LoaderFromFile {
-
- public static void main(String[] args) throws Exception{
-
- Properties properties=new Properties();
-
- properties.setProperty("resource.loader", "file");
-
- properties.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
-
- VelocityEngine velocityEngine=new VelocityEngine(properties);
-
-
- VelocityContext context=new VelocityContext();
-
- context.put("username", "张三");
- context.put("password", "123456789");
- context.put("age", "20");
- context.put("address", "陕西西安");
- context.put("blog", "http://blogjava.net/sxyx2008");
-
- StringWriter writer=new StringWriter();
-
- 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;
-
- public class LoaderFromClass {
-
- public static void main(String[] args) throws Exception{
-
- Properties properties=new Properties();
-
- properties.setProperty("resource.loader", "class");
-
- properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
-
- VelocityEngine velocityEngine=new VelocityEngine(properties);
-
-
- VelocityContext context=new VelocityContext();
-
- context.put("username", "张三");
- context.put("password", "123456789");
- context.put("age", "20");
- context.put("address", "陕西西安");
- context.put("blog", "http://blogjava.net/sxyx2008");
-
- StringWriter writer=new StringWriter();
-
-
-
- velocityEngine.mergeTemplate("com/velocity/test/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;
-
- public class LoaderFromJar {
-
- public static void main(String[] args) throws Exception{
-
- Properties properties=new Properties();
-
- properties.setProperty("resource.loader", "jar");
-
- properties.setProperty("jar.resource.loader.class", "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
-
- properties.setProperty("jar.resource.loader.path", "jar:file:WebRoot/WEB-INF/lib/vm.jar");
-
- VelocityEngine velocityEngine=new VelocityEngine(properties);
-
-
- VelocityContext context=new VelocityContext();
-
- context.put("username", "张三");
- context.put("password", "123456789");
- context.put("age", "20");
- context.put("address", "陕西西安");
- context.put("blog", "http://blogjava.net/sxyx2008");
-
- StringWriter writer=new StringWriter();
-
- 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的同志来说,这个例子可以是你参照学习的良好实例
-
- 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;
-
-
- 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[] )
- {
-
-
- 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");
-
-
- try
- {
- Velocity.init(p);
- }
- catch(Exception e)
- {
- System.out.println("Problem initializing Velocity : " + e );
- return;
- }
-
-
-
- VelocityContext context = new VelocityContext();
-
- context.put("name", "Velocity");
- context.put("project", "阿帕奇");
-
- context.put("list", getNames());
-
-
-
- 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 );
-
-
-
-
- String s = "We are using $project $name to render this.";
- w = new StringWriter();
-
- try
- {
- Velocity.evaluate( context, w, "mystring", s );
- }
- catch( ParseErrorException pee )
- {
-
- System.out.println("ParseErrorException : " + pee );
- }
- catch( MethodInvocationException mee )
- {
-
- System.out.println("MethodInvocationException : " + mee );
- }
- catch( Exception e )
- {
- System.out.println("Exception : " + e );
- }
-
- System.out.println(" string : " + w );
-
-
-
- try {
- VelocityEngine velocityEngine = new VelocityEngine();
- Properties properties = new Properties();
-
-
- String basePath = "vm";
-
-
- 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模板加载
原文:http://www.cnblogs.com/justuntil/p/7152283.html