velocity的开发指南 (十一)

2014-11-24 11:27:38 · 作者: · 浏览: 50
在Servlet类型应用常见的一种设置。支持Servlet 2.2 (或更新)规范的容器Tomcat就是这样一个例子. 这种装载方式很有效, 因此你必须将你的模板打成jar包放到你的web应用的WEB-INF/lib 目录下.就不再存在绝对、相对路径的问题了,与以上两个装载器相比 ClasspathResourceLoader不仅在servlet container中用也,几乎所有应用的上下文(context)都有用.
DataSourceResourceLoader : 这个loader可以从数据库载入资源. 这个loader不是标准j2EE的一部分,因此需要取得J2EE 发行库,将j2ee.jar加入到build/lib目录下,然后编译新的Velocity.jar设置ant target为jar-j2ee,更细说明请见文档中对类 org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader的介绍.
2.Configuration Examples
己配置的loader,可以参看 resource configuration section, for further reference.
第一就是要配置loader的名字. 参数resource.loader的值可以是你喜欢的用来关联指定loader的名字.
resource.loader = file
下一步就是设置这个名字对应的class了,这是最重要的一步 :
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader

这个例子中,我们告诉Velocity我们设置的loader名字叫file,指定的类是org.apache.velocity.runtime.resource.loader.FileResourceLoader.下一步就是设置这个loader的一些重要参数.
file.resource.loader.path = /opt/templates
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 2

这里,我们设置了查找模板的路径是 /opt/templates. 然后打开caching,最后,设置检测周期为2秒,以便Velocity检测新的或己更改过的模板来load.
上示是一些基本配置,随后,还会再有一些示例.
Do-nothing Default Configuration : 你也可以什么都不改动,就用默认的配置. 这是默认的loader配置:
resource.loader = file

file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = .
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 0

Multiple Template Path Configuration :多模板路径配置如下所示,只要用逗号分开就是 :
resource.loader = file

file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = /opt/directory1, /opt/directory2
file.resource.loader.cache = true
file.resource.loader.modificationCheckInterval = 10

Multiple Loader Configuration : 多个loader配置,嗯,也很简单,不说了,看例子就是.
#
# specify three resource loaders to use
#
resource.loader = file, class, jar

#
# for the loader we call 'file', set the FileResourceLoader as the
# class to use, turn off caching, and use 3 directories for templates
#
file.resource.loader.description = Velocity File Resource Loader
file.resource.loader.class = org.apache.velocity.runtime.resource.loader.FileResourceLoader
file.resource.loader.path = templatedirectory1, anotherdirectory, foo/bar
file.resource.loader.cache = false
file.resource.loader.modificationCheckInterval = 0

#
# for the loader we call 'class', use the ClasspathResourceLoader
#
class.resource.loader.description = Velocity Classpath Resource Loader
class.resource.loader.class = org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader

#
# and finally, for the loader we call 'jar', use the JarResourceLoader
# and specify two jars to load from
#
jar.resource.loader.description = Velocity Jar Resource Loader
jar.resource.loader.class = org.apache.velocity.runtime.resource.loader.JarResourceLoader
jar.resource.loader.path = jar:file:/myjarplace/myjar.jar, jar:file:/myjarplace/myjar2.jar

只是注意: 'file', 'class', and 'jar' 这三个名字不是固定是,可以根据你的喜好来设定. 但只要保持上面的对应关系就是.
3.插入定制资源管理器和Cache实现