简体   繁体   English

Apache Velocity 2.0 如何编写自定义资源加载器?

[英]Apache Velocity 2.0 how to write a custom resource loader?

There is a ResourceLoader class in the API documentation: API文档中有一个ResourceLoader类:

https://velocity.apache.org/engine/2.0/apidocs/org/apache/velocity/runtime/resource/loader/ResourceLoader.html https://velocity.apache.org/engine/2.0/apidocs/org/apache/velocity/runtime/resource/loader/ResourceLoader.html

I would like to implement my own loader, because I need to load templates from a database, but in a context sensitive way (in other words: DataSourceResourceLoader cannot be used, I need to write custom code to select the "right" template from the database).我想实现自己的加载器,因为我需要从数据库加载模板,但是以上下文相关的方式(换句话说:不能使用 DataSourceResourceLoader,我需要编写自定义代码以从数据库)。

It seems that ResourceLoader has some abstract methods, and it also seems that I would be able to write a custom loader by implementing these abstract methods.似乎ResourceLoader有一些抽象方法,而且我似乎也可以通过实现这些抽象方法来编写自定义加载器。 But I don't see any way to add a new loader to the engine.但是我看不出有什么方法可以向引擎添加新的加载器。 There is no "addResourceLoader" method.没有“addResourceLoader”方法。 The documentation only shows how to configure the loaders that are built into Velocity:该文档仅展示了如何配置内置于 Velocity 中的加载器:

https://velocity.apache.org/engine/2.0/developer-guide.html#resource-loaders https://velocity.apache.org/engine/2.0/developer-guide.html#resource-loaders

The main question: how to I add a custom resource loader to VelocityEngine (or VelocityContext?)主要问题:如何将自定义资源加载器添加到 VelocityEngine(或 VelocityContext?)

Another side question: I would like to turn off all built-in loaders.另一个问题:我想关闭所有内置加载程序。 Especially WebappResourceLoader which is active by default, and represents a security risk in my particular application.特别是默认情况下处于活动状态的WebappResourceLoader ,并且在我的特定应用程序中存在安全风险。 How to do that?怎么做?

I can't answer about how to implement your own resource loader, but the second part is quite easy:我无法回答如何实现您自己的资源加载器,但第二部分非常简单:

When you create the VelocityEngine, you can pass Properties that defined the "class path" of the resource loaders创建 VelocityEngine 时,可以传递定义资源加载器“类路径”的属性

The different classloaders are identified by a prefix for the property keys.不同的类加载器由属性键的前缀标识。 So something like:所以像:

Properties props = new Properties();

// Add a default class path resource loader - just an example
props.setProperty("cp.resource.loader.class", ClasspathResourceLoader.class.getName());
props.setProperty("cp.resource.loader.cache", "true);

// Add your own resource loader
props.setProperty("db.resource.loader.class", MyDBResourceLoader.class.getName());
props.setProperty("db.resource.loader.cache", "false");

// Define the "class path" for the loaders
// in this case first the "db" loader is asked for resources, if nothing is found the "cp" loader
props.setProperty(RuntimeConstants.RESOURCE_LOADER, "db,cp");

// Now create the engine
VelocityEngine engine = new VelocityEngine(props);

The above only defines two resource loaders for that engine, no other loaders will be used by that engine instance.上面只为该引擎定义了两个资源加载器,该引擎实例不会使用其他加载器。

You must first implement the ResourceLoader interface (or subclass an existing resource loader), then declare your resource loader in velocity.properties :您必须首先实现ResourceLoader接口(或现有资源加载器的子类),然后在velocity.properties中声明您的资源加载器:

resource.loader = .... , my_loader, ...
my_loader.resource.loader.class = com.foo.MyResourceLoader
my_loader.resource.loader.some_property = some_value
... other properties...

Please note that the configuration properties syntax changed a bit in version 2.1.请注意,配置属性语法在 2.1 版中发生了一些变化。 While the old syntax still works, if you want to avoid deprecation warnings in the log, it's rather:虽然旧语法仍然有效,但如果您想避免日志中的弃用警告,则更确切地说:

resource.loaders = .... , my_loader, ...
resource.loader.my_loader.class = com.foo.MyResourceLoader
resource.loader.m_loader.some_property = some_value
... other properties...

The has ResourceLoader interface has four abstract methods that you need to provide: has ResourceLoader 接口有四个需要提供的抽象方法:

  • void init(ExtProperties configuration) where the configuration object contains some_property -> some_value properties pair void init(ExtProperties configuration)其中configuration对象包含some_property -> some_value属性对
  • long getLastModified(Resource resource) which returns the number of seconds since a resource has been modified long getLastModified(Resource resource)返回资源被修改后的秒数
  • Reader getResourceReader(String source, String encoding) to get the actual content of thre resource Reader getResourceReader(String source, String encoding)获取thre resource的实际内容
  • boolean isSourceModified(Resource resource)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误[org.apache.velocity] ResourceManager:无法在任何资源加载器中找到资源“ layout.vm” - ERROR [org.apache.velocity] ResourceManager : unable to find resource 'layout.vm' in any resource loader 错误org.apache.velocity:ResourceManager:无法在任何资源加载器中找到资源'xxx.html.vm' - ERROR org.apache.velocity : ResourceManager : unable to find resource 'xxx.html.vm' in any resource loader 速度模板文件资源加载器不起作用 - Velocity Template File Resource Loader not working Velocity 2.0无法在jar中找到模板资源 - Velocity 2.0 unable to find template resource in jar 如何在自定义Velocity工具中加载/访问bean(@Resource) - How to load/access a bean (@Resource) in a custom Velocity tool Apache Velocity 2.0-日志绑定问题? - Apache Velocity 2.0 - log binding issue? Velocity 2.0:NoClassDefFoundError:org / apache / velocity / runtime / log / CommonsLogLogChute - Velocity 2.0: NoClassDefFoundError: org/apache/velocity/runtime/log/CommonsLogLogChute Apache Android 中的速度(“无法找到资源”) - Apache Velocity in Android("unable to find resource") Apache Velocity禁用模板和资源缓存 - Apache Velocity disable template & resource caching 在任何资源加载器中获取速度错误“无法找到资源'error.vm'。” - Getting velocity error “unable to find resource 'error.vm' in any resource loader.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM