今天在学习使用Velocity的VelocityLayoutServlet布局的过程中遇到这样一个问题,请求的模板文件能够正常地加载显示,但是设置的默认的布局模板总是加载不了。
velocity.properties中我是这样配置的:
velocimacro.library=macros.vm
webapp.resource.loader.path=/WEB-INF/templates/
velocimacro.permissions.allow.inline = true
velocimacro.permissions.allow.inline.to.replace.global = false
velocimacro.permissions.allow.inline.local.scope = false
velocimacro.context.localscope = false
input.encoding=utf-8
output.encoding=utf-8
# Filepath for error template,
# relative to web application root directory
tools.view.servlet.error.template = /WEB-INF/templates/layout/Error.vm
# Directory for layout templates,
# relative to web application root directory
tools.view.servlet.layout.directory =/WEB-INF/templates/layout
# Filepath of the default layout template
# relative to the layout directory
# NOT relative to the root directory of the webapp!
tools.view.servlet.layout.default.template =mainTemplate.vm
看了半天源码我才突然发现,是官方文档中的配置文件实例中的这句注释彻底误导了我:
# Directory for layout templates,
# relative to web application root directory
tools.view.servlet.layout.directory =/WEB-INF/templates/layout
我真的因为是相对于网站的根目录了,原来是相对于:
webapp.resource.loader.path
果断把:
tools.view.servlet.layout.directory =/WEB-INF/templates/layout
改为:
tools.view.servlet.layout.directory =/layout
问题就解决了,由于 这个配置的默认配置就是layout,所以直接注释掉也可以。