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()就可以了。
分享到:
相关推荐
这里我们探讨的是一个针对Windows环境的自定义PE Loader的源代码实现。 首先,PE Loader的工作流程主要包括以下几个关键步骤: 1. **读取PE头信息**:每个Windows可执行文件都包含一个Portable Executable (PE) ...
2. **加载资源**:在Loader程序的代码中,使用`FindResource`和`LoadResource`函数来定位和加载嵌入的EXE资源。这两个API函数位于Windows API中,用于处理应用程序的资源。 3. **写入磁盘**:加载资源后,需要将其...
而ActionScript则提供更底层的编程能力,允许开发者自定义组件的行为。 数据验证在Flex中通常涉及到两个主要的类:Validator和Formatters。Validator类用于检查用户输入是否符合预设的验证规则,如非空、数字范围、...
在Android开发中,自定义View是一项重要的技能,它允许开发者根据需求定制独特的用户界面和交互体验。本项目中,我们关注的是如何实现一个自定义View来展示GIF图像,并且带有音乐伴奏以及定时关闭的功能。这个自定义...
- 自定义Loader接口:`public delegate byte[] CustomLoader(ref string filepath);` - 注册自定义Loader:`public void LuaEnv.AddLoader(CustomLoader loader);` - **应用场景**:适用于Lua文件来自网络下载、...
npm i style-resources-loader -D 用法 该加载器是用于webpackCSS处理器资源加载器,它将您的样式资源(例如variables, mixins )注入到多个导入的css, sass, scss, less, stylus模块中。 它主要用于 在所有样式...
environment.getPropertySources().addLast(yamlLoader.load(filePath, loader.getResource(filePath), null)); } catch (IOException e) { logger.error("load property file failed! file: " + filePath); ...
1. `asset/resource`:发送一个单独的文件并导出URL,类似于之前的`file-loader`。 2. `asset/inline`:导出资源的数据URI,对应于之前的`url-loader`。 3. `asset/source`:导出资源的源代码,相当于`raw-loader`的...
@PropertySource(value = {"classpath:custom-config.yml"}, loader = YamlPropertySourceLoader.class) public class CustomConfig { // ... } ``` 3. **使用@ConfigurationProperties**:为了让Spring自动绑定...
当需要从特定来源(如 Resource、网络、加密文件等)加载 Lua 文件时,可以使用 XLua 的自定义 Loader。通过实现 CustomLoader 接口并注册到 LuaEnv.AddLoader,可以根据需要处理加载逻辑。例如,读取加密文件或通过...
2. **数据类型**:Informix 支持多种数据类型,包括数值型、字符型、日期/时间型、二进制型以及自定义类型等,满足不同应用场景的需求。 3. **事务处理**:Informix 提供强大的事务处理能力,确保数据的一致性和...
velocityProperties.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); velocityProperties.setProperty("velocimacro.library", ""); ...
velocityProperties.setProperty("file.resource.loader.path", "/path/to/templates"); VelocityEngine velocityEngine = new VelocityEngine(velocityProperties); ``` 上述代码中,`file.resource.loader....
5. **自定义配置**: 提供的`conf`目录包含配置文件,允许用户根据实际需求调整工具的行为,如设置最大批次大小、错误处理策略等。 6. **源代码访问**: 开源特性意味着用户可以查看`src`目录下的源代码,了解工具的...
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityEngine engine = new VelocityEngine(props); VelocityContext context = ...
spring.velocity.properties.classpath.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader spring.velocity.view.prefix=/templates/ spring.velocity.view.suffix=....
p.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); VelocityEngine ve = new VelocityEngine(); ve.init(p); Template t = ve.getTemplate...
- **Resource Loader Aware 接口**:允许 Bean 获取其上下文中的 ResourceLoader 实例。 **3. 验证、数据绑定和类型转换** - **验证**:使用 Validator 接口进行验证。 - **数据绑定**:将请求参数或数据模型...
这通常在`velocity.properties`配置文件中完成,如`resource.loader=file`和`file.resource.loader.path=WEB-INF/templates`。 6. **MVC模式的应用**: 与MVC框架(如Spring MVC)集成时,控制器(Controller)处理...
props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); return new VelocityEngine(props); } } ``` ## 3. VTL语法基础 ### 3.1 变量...