|
CommonTemplate 配置指南 |
|
一. Properties 配置
配置示例:
# 继承标准web配置
@extends=org/commontemplate/tools/web/commontemplate.properties
# 非调试模式将隐藏部分不重要的异常信息
debugMode=false
# 国际化信息基名,规则同java.util.ResourceBundle,
# 如:假设基名为xxx/yyy,用户地区为zh_CN,
# 则在ClassPath中先查找xxx/yyy_zh_CN.properties,
# 不存在则查找xxx/yyy_zh.properties, 否则查找xxx/yyy.properties
messagesBasename=xxx/yyy
# null值输出时的默认替换字符串
defaultNullValue=
# 数组输出时的各元素间的分隔符
defaultArraySeparator=,
# 日期输出时的默认格式
defaultDateFormat=yyyy-MM-dd HH:mm:ss
# 数字输出时的默认格式
defaultNumberFormat=###,##0.###
# 加载模板文件时的默认字符集编码,默认为UTF-8
# 若设置defaultEncoding=null将取当前系统的默认文件编码
defaultEncoding=UTF-8
# 模板文件虚拟目录,模板加载时将以此目录作为根目录
virtualDirectory=WEB-INF/views/
# 文件修改检测时间间隔,用于热加载,单位:毫秒(mm)
# 默认为30秒(30000mm),调试时可设成1秒(1000mm)
modificationCheckInterval=30000
# 模板运行日志输出端
logger=org.commontemplate.standard.log.SimpleLogger()
#logger=org.commontemplate.standard.log.CommonsLogging()
# 磁盘缓存目录,用于缓存模板编译结果
diskCache.directory=WEB-INF/cache/
# 缓存文件前缀
diskCache.filePrefix=
# 缓存文件后缀
diskCache.fileSuffix=.template
# 文件路径分割符(也就是"/"和"\")的替换符,用于将模板全路径名转成文件名
diskCache.pathSeparatorReplacer=_-
# 内存缓存策略,OSCache, EHCache需要另外的配置
memoryCache=org.commontemplate.standard.cache.LruCache()
#memoryCache=org.commontemplate.standard.cache.MruCache()
#memoryCache=org.commontemplate.standard.cache.FifoCache()
#memoryCache=org.commontemplate.standard.cache.NoneCache()
#memoryCache=org.commontemplate.standard.cache.WeakCache()
#memoryCache=org.commontemplate.standard.cache.SoftCache()
#memoryCache=org.commontemplate.standard.cache.StrongCache()
#memoryCache=org.commontemplate.ext.cache.OSCache()
#memoryCache=org.commontemplate.ext.cache.EHCache()
# 内存缓存容量,用于:LruCache, MruCache, FifoCache
memoryCache.maxSize=1000
# 响应内容类型, 不设置默认为text/html
response.contentType=text/html
特殊配置说明:
(1) 非web应用,如代码生成器等,可以设置:
@extends=org/commontemplate/tools/commontemplate.properties
(2) 如果需去掉磁盘缓存,通常用于已在OSCache/EHCache中配置磁盘缓存,可以设置:
templateCache=$memoryCache
(3) 如果需将缓存放在非web应用目录,可以设置:
diskCache.rootDirectory=C:/xxx/
(3) 默认是禁止函数调用的,若需要开启,可以设置:
functionAvailable=true
Web配置查找顺序:
1.首先查找web.xml中的context-param配置:commontemplate-config参数所指的路径。如:
<context-param>
<param-name>commontemplate-config</param-name>
<param-value>/WEB-INF/commontemplate.properties</param-value>
</context-param>
(注:如果配置路径以 / 开头则表示在web应用目录下,否则在ClassPath下查找)
2.如果未配置,则查找默认WEB-INF路径:/WEB-INF/commontemplate.properties
3.如果WEB-INF中没有,则查找ClassPath根目录:commontemplate.properties
4.如果ClassPath根目录也没有,则使用标准配置:org/commontemplate/tools/web/commontemplate.properties
配置规则:
整个standard包的所有配置类全部留有setter注入接口,任何支持setter方式的IoC容器均可以完成配置。
默认采用内置的IoC容器初始化配置,详细请参考:org.commontemplate.util.PropertiesBeanFactory。
使用properties文件作为配置,所需遵循java.util.Properties的所有规则,如:# ! = :等符号需转义等。
(1) 基本类型:(与Java相似)
null, true, false为关键字
以数字开头的为Number,识别后缀L,F,D,S
以单引号括起的为Character
以双引号括起的为String,如果非特殊串,双引号可省
(2) 引用:
以$开头表示引用配置项
(3) 类和对象:
以.class结尾表示相应Class类元
以()结尾表示创建Instance,并以Instance的key加点号作为前缀,查找并注入其属性
以.static结尾表示通过静态字段获取Instance
以().static结尾表示通过静态工厂方法获取Instance,并以Instance的key加点号作为前缀,查找并注入其属性
(4) 集合:
以<>结尾表示Set,并以<>前的名称查找Set的项
以[]结尾表示List,并以[]前的名称查找List的项
以{}结尾表示Map,并以{}前的名称查找Map的项
(5) 配置继承:
@extends=parent1.properties,parent2.properties
使用方法:
PropertiesConfigurationLoader.loadConfiguration("xxx.properties"); // 在Classpath中查找
PropertiesConfigurationLoader.loadConfiguration("xxx.properties", resourceLoader); // 指定资源加载器
标准配置参考:
org/commontemplate/tools/commontemplate.properties
org/commontemplate/tools/context.properties
org/commontemplate/tools/template.properties
org/commontemplate/tools/directive.properties
org/commontemplate/tools/expression.properties
二. Spring 配置
Spring的使用请参见:http://www.springframework.org
commontemplate-spring.xml:
|
|