使用 freemarker 为自定义变量格式化 xml 模板
format xml template for the custom variables using freemarker
我使用 freemarker 为我在 xml 中的自定义变量格式化模板,比如 _prop.Var1。下面是我在地图中保存所有道具的上下文并尝试使用地图格式化变量的数据。
<我的文档>
<道具>
<道具名称="Var1" 值="XXX" />
下面是我正在使用的代码片段:
template.process(变量,输出);
注意:variables是一个map,包含了name-value对的所有属性。
变量:{Var1:XXX},尝试使用 map : {_prop.Var1:XXX} 但这也不起作用。
这是我在使用上述代码行时遇到的异常。
已编辑:
freemarker.core.InvalidReferenceException:以下评估为空或缺失:
==> 冗长 [在模板 "template" 第 1 行第 83 列]
提示:如果已知失败的表达式合法引用有时为空或缺失的内容,请指定默认值,如 myOptionalVar!myDefault,或使用 <#if myOptionalVar??>when-present<#else >当失踪。 (这些仅涵盖表达式的最后一步;要涵盖整个表达式,请使用括号:(myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL 堆栈跟踪(“~”表示与嵌套相关):
- 失败于:${_prop.Var1} [在模板 "template" 第 3 行第 63 列]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:80) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:43) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.NonHashException.<init>(NonHashException.java:49) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Dot._eval(Dot.java:48) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Expression.eval(Expression.java:81) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:96) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.DollarVariable.accept(DollarVariable.java:59) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Environment.visit(Environment.java:327) [freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Environment.visit(Environment.java:333) [freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Environment.process(Environment.java:306) [freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.template.Template.process(Template.java:386) [freemarker-2.3.25-incubating.jar:2.3.25]
请帮我解决这个问题。
提前致谢。
我注意到你说的是 "tried with map : {_prop.Var1:XXX} but this also doesn't worked"。我不确定那到底是什么意思(比如,Java 代码是什么),但是如果您尝试过 variables.put("_prop.Var1", "XXX")
之类的东西,那将不起作用,因为在模板中您编写 _prop.Var1
然后 FreeMarker 将首先查找 _prop
,如果找到,然后在其中查找 Var1
。它不会查找“_prop.Var1”键(因为您必须在模板中编写 _prop\.Var1
,以便它将点视为名称的一部分,而不是运算符)。要使模板中的 _prop.Var1
正常工作,您必须执行以下操作:
Map<String, Object> prop = new HashMap<>();
variables.put("_prop", prop);
prop.put("Var1", "XXX");
... add further subvariables to prop
BTW prop
不必是 Map
,它也可以是具有所需 bean 属性的 Java bean。因此,如果它是 public class 和 public String getVar1() { return "XXX"; }
,那么您可以将其称为 _prop.var1
(小写 v
,如 JavaBeans 规范是这样说的)。
我使用 freemarker 为我在 xml 中的自定义变量格式化模板,比如 _prop.Var1。下面是我在地图中保存所有道具的上下文并尝试使用地图格式化变量的数据。
<我的文档>
<道具>
<道具名称="Var1" 值="XXX" />
下面是我正在使用的代码片段:
template.process(变量,输出);
注意:variables是一个map,包含了name-value对的所有属性。 变量:{Var1:XXX},尝试使用 map : {_prop.Var1:XXX} 但这也不起作用。
这是我在使用上述代码行时遇到的异常。
已编辑:
freemarker.core.InvalidReferenceException:以下评估为空或缺失: ==> 冗长 [在模板 "template" 第 1 行第 83 列]提示:如果已知失败的表达式合法引用有时为空或缺失的内容,请指定默认值,如 myOptionalVar!myDefault,或使用 <#if myOptionalVar??>when-present<#else >当失踪。 (这些仅涵盖表达式的最后一步;要涵盖整个表达式,请使用括号:(myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
FTL 堆栈跟踪(“~”表示与嵌套相关):
- 失败于:${_prop.Var1} [在模板 "template" 第 3 行第 63 列]
at freemarker.core.InvalidReferenceException.getInstance(InvalidReferenceException.java:134) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.UnexpectedTypeException.newDesciptionBuilder(UnexpectedTypeException.java:80) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.UnexpectedTypeException.<init>(UnexpectedTypeException.java:43) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.NonHashException.<init>(NonHashException.java:49) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Dot._eval(Dot.java:48) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Expression.eval(Expression.java:81) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.DollarVariable.calculateInterpolatedStringOrMarkup(DollarVariable.java:96) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.DollarVariable.accept(DollarVariable.java:59) ~[freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Environment.visit(Environment.java:327) [freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Environment.visit(Environment.java:333) [freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.core.Environment.process(Environment.java:306) [freemarker-2.3.25-incubating.jar:2.3.25]
at freemarker.template.Template.process(Template.java:386) [freemarker-2.3.25-incubating.jar:2.3.25]
请帮我解决这个问题。
提前致谢。
我注意到你说的是 "tried with map : {_prop.Var1:XXX} but this also doesn't worked"。我不确定那到底是什么意思(比如,Java 代码是什么),但是如果您尝试过 variables.put("_prop.Var1", "XXX")
之类的东西,那将不起作用,因为在模板中您编写 _prop.Var1
然后 FreeMarker 将首先查找 _prop
,如果找到,然后在其中查找 Var1
。它不会查找“_prop.Var1”键(因为您必须在模板中编写 _prop\.Var1
,以便它将点视为名称的一部分,而不是运算符)。要使模板中的 _prop.Var1
正常工作,您必须执行以下操作:
Map<String, Object> prop = new HashMap<>();
variables.put("_prop", prop);
prop.put("Var1", "XXX");
... add further subvariables to prop
BTW prop
不必是 Map
,它也可以是具有所需 bean 属性的 Java bean。因此,如果它是 public class 和 public String getVar1() { return "XXX"; }
,那么您可以将其称为 _prop.var1
(小写 v
,如 JavaBeans 规范是这样说的)。