在处理以下代码时:
template.process(data, filewriter);模板是freemarker.template.Template类的对象。创建者
Configuration cfg = new Configuration(new Version("2.3.23"));
cfg.setDefaultEncoding("UTF-8");
try {
cfg.setDirectoryForTemplateLoading(new File(
"dir/to/templates"));
} catch (IOException e) {
e.printStackTrace();
}
this.template = cfg.getTemplate(path);文件写入器是filewriter对象
数据是地图,其填充形式为
key ->"page.title", value ->"Dummy Page title"
key ->"page.body", value ->"Dummy Page Body"我使用了以下模板
<html>
<head>
<title>${page.title}</title>
</head>
<body>
<p>This page is generated using freemarker
</p>
<p>
${page.body}
</p>
</body>
</html>在处理过程中,我收到以下错误:
FreeMarker template error:
The following has evaluated to null or missing:
==> page [in template "default.ftl" at line 3, column 10]
----
Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
----
----
FTL stack trace ("~" means nesting-related):
- Failed at: ${page.title} [in template "default.ftl" at line 3, column 8]
----请指出我做错了什么?
PS:如果我把名字"page.title“改成"pagetitle”,把"page.body“改成"pagebody”,整个设置就能正常工作。我真的很想用‘’来命名我的值。
发布于 2015-12-18 18:12:56
作为例外:
FTL stack trace ("~" means nesting-related):
- Failed at: ${page.title} [in template "default.ftl" at line 3, column 8]freemarker会将${page.title}解析为嵌套对象,您可以尝试使用dataModel
${.dataModel["page.title"]}发布于 2015-12-18 20:19:51
创建一个具有public String getTitle()和public String getBody()方法的公共Java类(比如com.example.Page),并将对象放入名为page的data中,或者创建一个Map<String, Object>,其中包含具有"title"和"body"键的Map条目,并将其放入名为<代码>D10的<代码>D9中。那么page.title之类的就可以工作了。或者,如果您确实需要使用点作为名称的一部分,则必须转义点,以便它知道您并不是要获得一个子变量:page\.title。
发布于 2017-05-17 17:13:56
https://stackoverflow.com/questions/34352781
复制相似问题