velocity在1.5中提供了6种资源加载器,已经足够满足大多数的需求,当然,如果你觉得这些都不适用,你也可以自己实现一个。
实现一个资源加载器,实际上很简单,只要继承ResourceLoader,实现它的几个方法就可以了。
下面是一个简单的例子,它展示了如何实现一个最简单的ResourceLoader。
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://sivasoft.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=package%20org.sivasoft.viework.velocity.resource.loader%3B%0A%0Aimport%20java.io.ByteArrayInputStream%3B%0Aimport%20java.io.InputStream%3B%0Aimport%20java.io.UnsupportedEncodingException%3B%0A%0Aimport%20org.apache.commons.collections.ExtendedProperties%3B%0Aimport%20org.apache.commons.logging.Log%3B%0Aimport%20org.apache.commons.logging.LogFactory%3B%0Aimport%20org.apache.velocity.exception.ResourceNotFoundException%3B%0Aimport%20org.apache.velocity.runtime.resource.Resource%3B%0Aimport%20org.apache.velocity.runtime.resource.loader.ResourceLoader%3B%0A%0Apublic%20class%20TestResourceLoader%20extends%20ResourceLoader%20%7B%0A%0A%09private%20static%20Log%20log%20%3D%20LogFactory.getLog(TestResourceLoader.class)%3B%0A%0A%09%2F**%0A%09%20*%20%E5%BE%97%E5%88%B0%E8%B5%84%E6%BA%90%E6%B5%81%E7%9A%84%E6%9C%80%E5%90%8E%E4%BF%AE%E6%94%B9%E6%97%B6%E9%97%B4%0A%09%20*%20%0A%09%20*%20%40param%20resource%0A%09%20*%20%40return%20Time%20in%20millis%20when%20the%20resource%20has%20been%20modified.%0A%09%20*%2F%0A%09%40Override%0A%09public%20long%20getLastModified(Resource%20arg0)%20%7B%0A%09%09return%200%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E5%BE%97%E5%88%B0%E4%B8%80%E4%B8%AA%E8%B5%84%E6%BA%90%E8%BE%93%E5%85%A5%E6%B5%81%E3%80%82%0A%09%20*%20%0A%09%20*%20%40param%20source%0A%09%20*%20%40return%20The%20input%20stream%20for%20the%20requested%20resource.%0A%09%20*%20%40throws%20ResourceNotFoundException%0A%09%20*%2F%0A%09%40Override%0A%09public%20InputStream%20getResourceStream(String%20name)%0A%09%09%09throws%20ResourceNotFoundException%20%7B%0A%09%09if%20(name%20%3D%3D%20null%20%7C%7C%20name.length()%20%3D%3D%200)%20%7B%0A%09%09%09throw%20new%20ResourceNotFoundException(%0A%09%09%09%09%09%22Need%20to%20specify%20a%20template%20name!%22)%3B%0A%09%09%7D%0A%0A%09%09%2F%2F%20%E5%BE%97%E5%88%B0%E4%B8%80%E4%B8%AA%E8%B5%84%E6%BA%90%E6%B5%81%EF%BC%8C%E8%BF%99%E9%87%8C%E4%B8%BA%E4%BA%86%E7%AE%80%E5%8D%95%EF%BC%8C%E4%BD%BF%E7%94%A8%E4%BA%86%E4%B8%80%E4%B8%AA%E5%9B%BA%E5%AE%9A%E7%9A%84String%EF%BC%8C%E5%AE%9E%E9%99%85%E4%B8%8A%E5%B9%B6%E4%B8%8D%E4%BC%9A%E5%A6%82%E6%AD%A4%E4%BD%BF%E7%94%A8%E3%80%82%0A%09%09String%20str%20%3D%20%22Hello%20%24name%22%3B%0A%09%09try%20%7B%0A%09%09%09return%20new%20ByteArrayInputStream(str.getBytes(%22UTF-8%22))%3B%0A%09%09%7D%20catch%20(UnsupportedEncodingException%20e)%20%7B%2F%2F%20%E8%BF%99%E4%B8%AA%E5%BC%82%E5%B8%B8%E5%BA%94%E8%AF%A5%E4%B8%8D%E4%BC%9A%E5%8F%91%E7%94%9F%E3%80%82%0A%09%09%09log.error(e)%3B%0A%09%09%09throw%20new%20RuntimeException(e)%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E5%88%9D%E5%A7%8B%E5%8C%96%E8%B5%84%E6%BA%90%E5%8A%A0%E8%BD%BD%E5%99%A8%20a%20resources%20class.%0A%09%20*%20%0A%09%20*%20%40param%20configuration%0A%09%20*%2F%0A%09%40Override%0A%09public%20void%20init(ExtendedProperties%20arg0)%20%7B%0A%09%09if%20(log.isTraceEnabled())%20%7B%0A%09%09%09log.trace(%22TestResourceLoader%20%3A%20initialization%20complete.%22)%3B%0A%09%09%7D%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E6%A3%80%E6%9F%A5%E8%B5%84%E6%BA%90%E6%98%AF%E5%90%A6%E8%A2%AB%E4%BF%AE%E6%94%B9%E3%80%82%0A%09%20*%20%0A%09%20*%20%40param%20resource%0A%09%20*%20%40return%20True%20if%20the%20resource%20has%20been%20modified.%0A%09%20*%2F%0A%09%40Override%0A%09public%20boolean%20isSourceModified(Resource%20arg0)%20%7B%0A%09%09return%20false%3B%0A%09%7D%0A%0A%7D%0A"></embed>
- package org.sivasoft.viework.velocity.resource.loader;
-
- import java.io.ByteArrayInputStream;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
-
- import org.apache.commons.collections.ExtendedProperties;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.apache.velocity.exception.ResourceNotFoundException;
- import org.apache.velocity.runtime.resource.Resource;
- import org.apache.velocity.runtime.resource.loader.ResourceLoader;
-
- public class TestResourceLoader extends ResourceLoader {
-
- private static Log log = LogFactory.getLog(TestResourceLoader.class);
-
-
-
-
-
-
-
- @Override
- public long getLastModified(Resource arg0) {
- return 0;
- }
-
-
-
-
-
-
-
-
- @Override
- public InputStream getResourceStream(String name)
- throws ResourceNotFoundException {
- if (name == null || name.length() == 0) {
- throw new ResourceNotFoundException(
- "Need to specify a template name!");
- }
-
-
- String str = "Hello $name";
- try {
- return new ByteArrayInputStream(str.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e) {
- log.error(e);
- throw new RuntimeException(e);
- }
- }
-
-
-
-
-
-
- @Override
- public void init(ExtendedProperties arg0) {
- if (log.isTraceEnabled()) {
- log.trace("TestResourceLoader : initialization complete.");
- }
- }
-
-
-
-
-
-
-
- @Override
- public boolean isSourceModified(Resource arg0) {
- return false;
- }
-
- }
很简单,只要做一点小小的开发,你就可以自己管理资源加载。
再来看看如何使用这个加载器:
Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://sivasoft.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="high" flashvars="clipboard=%2F%2F%E4%BB%A3%E7%A0%81%E7%9C%81%E7%95%A5%E6%8E%89%E4%BA%86%E6%89%80%E6%9C%89try%20catch%0AProperties%20p%20%3D%20new%20Properties()%3B%0Ap.setProperty(%22resource.loader%22%2C%20%22test%22)%3B%2F%2F%E7%BB%99%E4%BD%A0%E7%9A%84%E5%8A%A0%E8%BD%BD%E5%99%A8%E9%9A%8F%E4%BE%BF%E5%8F%96%E4%B8%AA%E5%90%8D%E5%AD%97%0Ap.setProperty(%22test.resource.loader.class%22%2C%22org.sivasoft.viework.velocity.resource.loader.TestResourceLoader%22)%3B%2F%2F%E9%85%8D%E7%BD%AE%E4%B8%80%E4%B8%8B%E4%BD%A0%E7%9A%84%E5%8A%A0%E8%BD%BD%E5%99%A8%E5%AE%9E%E7%8E%B0%E7%B1%BB%0Ap.setProperty(%22input.encoding%22%2C%20%22UTF-8%22)%3B%0Ap.setProperty(%22output.encoding%22%2C%20%22UTF-8%22)%3B%0A%0AVelocityEngine%20ve%20%3D%20new%20VelocityEngine()%3B%0Ave.init(p)%3B%0AString%20velocityTemplate%20%3D%20vm%3B%0ATemplate%20template%20%3D%20null%3B%0Atemplate%20%3D%20ve.getTemplate(%22%E9%9A%8F%E4%BE%BF%E4%B8%80%E4%B8%AAString%22%2C%20%22UTF-8%22)%3B%0A%0AVelocityContext%20context%20%3D%20new%20VelocityContext()%3B%0Acontext.put(%22name%22%2C%22Siva%22)%3B%0A%0AFileOutputStream%20fos%20%3D%20new%20FileOutputStream(%22d%3A%5Ctest.txt%22)%3B%0ABufferedWriter%20writer%20%3D%20new%20BufferedWriter(new%20OutputStreamWriter(fos%2C%20%22UTF-8%22))%3B%0Atemplate.merge(context%2C%20writer)%3B%0Awriter.close()%3B%0Afos.close()%3B%0A"></embed>
-
- Properties p = new Properties();
- p.setProperty("resource.loader", "test");
- p.setProperty("test.resource.loader.class","org.sivasoft.viework.velocity.resource.loader.TestResourceLoader");
- p.setProperty("input.encoding", "UTF-8");
- p.setProperty("output.encoding", "UTF-8");
-
- VelocityEngine ve = new VelocityEngine();
- ve.init(p);
- String velocityTemplate = vm;
- Template template = null;
- template = ve.getTemplate("随便一个String", "UTF-8");
-
- VelocityContext context = new VelocityContext();
- context.put("name","Siva");
-
- FileOutputStream fos = new FileOutputStream("d:\test.txt");
- BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
- template.merge(context, writer);
- writer.close();
- fos.close();
运行代码(需要给它加上异常处理),可以看到D盘根目录下生成了一个文件,打开看看,是不是Hello Siva。
可以看到,实现一个最简单的资源加载器,我们只要继承ResourceLoader并实现getResourceStream()就可以了。