访问本地的mustache模板文件的源码路径如下
com.twitter.finatra.http.modules.MustacheModule
从源码可以看出,是否使用cache的template是取决为local.doc.root是否为空.
为了避免这个参数干扰到使用cache缓存mustache
重新复制一份代码,命名为myMustacheModule
def provideMustacheFactory(
resolver: FileResolver,
@Flag("local.doc.root") localDocRoot: String): MustacheFactory = {
// templates are cached only if there is no local.doc.root
// val cacheMustacheTemplates = localDocRoot.isEmpty
val templatesDirectory = templatesDir()
new DefaultMustacheFactory(templatesDirectory) {
setObjectHandler(new ScalaObjectHandler)
override def compile(name: String): Mustache = {
// if (cacheMustacheTemplates) {
super.compile(name)
// } else {
// new LocalFilesystemDefaultMustacheFactory(templatesDirectory, resolver).compile(name)
// }
}
}
}
}
直接使用super.compile(name)
然后在继承HttpServer类里面声明一个mustacheModule的变量,覆盖默认的就可以
object web_test extends HttpServer {
override def configureHttp(router : HttpRouter) = {
router
.add[testController]
}
override val defaultFinatraHttpPort = ":8000"
override def mustacheModule = myMustacheModule // 这样就可以使用自定义的module里
}
这样就可以在有local.doc.root的时候,继续使用cache