本文针对velocity模板加载的三种方式进行说明
一:velocity默认加载方式(文件加载) (下面是针对文件加载的目录结构)
![](https://i-blog.csdnimg.cn/blog_migrate/93e75a37e014e34a6eb7b8bcfddca16f.png)
public class VelocityTemplate {
public static String getVelocityTemplate(String basePath) throws Exception {
String sysRoot = VelocityTemplate.class.getResource("").getPath();
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");
properties.put("input.encoding", "UTF-8");
properties.put("output.encoding", "UTF-8");
//实例化一个VelocityEngine对象
VelocityEngine velocityEngine = new VelocityEngine(properties);
//实例化一个VelocityContext
VelocityContext velocityContext = new VelocityContext();
//向VelocityContext中放入键值
context.put("username", "张三");
context.put("password", "123456789");
context.put("age", "20");
//实例化一个StringWriter
StringWriter writer=new StringWriter();
Template template = velocityEngine.getTemplate(sysRoot+basePath, "UTF-8");
template.merge(velocityContext, stringWriter);
return stringWriter.toString();
}
}
具体实现请看:https://git.oschina.net/ldbangel/quicksurepcprovider/blob/master/src/main/java/com/quicksure/insurance/velocity/VelocityTemplate.java
二:从类路径加载模板文件
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 {
</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> main(String[] args) <span style="color: #0000ff">throws</span><span style="color: #000000"> Exception{
</span><span style="color: #008000">//</span><span style="color: #008000">初始化参数</span>
Properties properties=<span style="color: #0000ff">new</span><span style="color: #000000"> Properties();
</span><span style="color: #008000">//</span><span style="color: #008000">设置velocity资源加载方式为class</span>
properties.setProperty("resource.loader", "class"<span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">设置velocity资源加载方式为file时的处理类</span>
properties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"<span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">实例化一个VelocityEngine对象</span>
VelocityEngine velocityEngine=<span style="color: #0000ff">new</span><span style="color: #000000"> VelocityEngine(properties);
</span><span style="color: #008000">//</span><span style="color: #008000">实例化一个VelocityContext</span>
VelocityContext context=<span style="color: #0000ff">new</span><span style="color: #000000"> VelocityContext();
</span><span style="color: #008000">//</span><span style="color: #008000">向VelocityContext中放入键值</span>
context.put("username", "张三"<span style="color: #000000">);
context.put(</span>"password", "123456789"<span style="color: #000000">);
context.put(</span>"age", "20"<span style="color: #000000">);
context.put(</span>"address", "陕西西安"<span style="color: #000000">);
context.put(</span>"blog", "http://blogjava.net/sxyx2008"<span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">实例化一个StringWriter</span>
StringWriter writer=<span style="color: #0000ff">new</span><span style="color: #000000"> StringWriter();
</span><span style="color: #008000">//</span><span style="color: #008000">从src目录下加载hello.vm模板
</span><span style="color: #008000">//</span><span style="color: #008000">假若在com.velocity.test包下有一个hello.vm文件,那么加载路径为com/velocity/test/hello.vm</span>
velocityEngine.mergeTemplate("com/velocity/test/hello.vm", "gbk"<span style="color: #000000">, context, writer);
</span><span style="color: #008000">//</span><span style="color: #008000">velocityEngine.mergeTemplate("hello.vm", "gbk", context, writer);</span>
System.out.println(writer.toString());
}
}
三:从jar文件中加载模板文件
public class VelocityTemplate {
public static String getVelocityTemplate(String basePath) throws Exception {
String sysRoot = VelocityTemplate.class.getResource("").getPath();
//初始化参数
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:/F:/quicksure_Server_Provider.jar");
properties.put("input.encoding", "UTF-8");
properties.put("output.encoding", "UTF-8");
//实例化一个VelocityEngine对象
VelocityEngine velocityEngine = new VelocityEngine(properties);
//实例化一个VelocityContext
VelocityContext velocityContext = new VelocityContext();
//向VelocityContext中放入键值
context.put("username", "张三");
context.put("password", "123456789");
context.put("age", "20");
//实例化一个StringWriter
StringWriter writer=new StringWriter();
Template template = velocityEngine.getTemplate("com/quicksure/insurance/velocity/template/"+basePath, "UTF-8");
template.merge(velocityContext, stringWriter);
return stringWriter.toString();
}
}
实例展示:https://git.oschina.net/ldbangel/quicksurepcprovider/blob/master/src/main/java/com/quicksure/insurance/velocity/VelocityTemplate.java
主要应该就是用的file文件加载和jar包文件加载
</div>
<div class="postDesc">posted @ <span id="post-date">2017-05-19 09:21</span> <a href="http://www.cnblogs.com/ldbangel/">编码龟</a> 阅读(<span id="post_view_count">2832</span>) 评论(<span id="post_comment_count">0</span>) <a href="https://i.cnblogs.com/EditPosts.aspx?postid=6876661" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(6876661);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=317206,cb_entryId=6876661,cb_blogApp=currentBlogApp,cb_blogUserGuid='758b68ad-76f5-e511-9fc1-ac853d9f53cc',cb_entryCreatedDate='2017/5/19 9:21:00';loadViewCount(cb_entryId);var cb_postType=1;</script>
</div><!--end: forFlow -->
</div>