这可能很简单,但我无法在互联网上找到它。 我在freemarker中嵌套了如下对象,该对象具有多个键,我无法将值分配给多个键,我只能将值分配给一个键
<#assign modifiedObjectChng = {
"beforeDep": {
"range": [],
"newTravelDate": "",
"reissueAllowed": false,
"changeAllowed": false,
"maxPenality": ""
},
"afterDep": {
"range": [],
"newTravelDate": "",
"reissueAllowed": false,
"changeAllowed": false,
"maxPenality": ""
}
}>
<#assign modifiedObjectChng = modifiedObjectChng + { "beforeDep" : {"newTravelDate" : "AAA" }}>
<#assign modifiedObjectChng = modifiedObjectChng + { "beforeDep" : {"reissueAllowed" : "BBB" }}>
${modifiedObjectChng.beforeDep.newTravelDate} //Output : error following has evaluated to null
${modifiedObjectChng.beforeDep.reissueAllowed} // gives correct result
我的问题是我将如何只用一个键就可以为多个键赋值
预先感谢
答案 0 :(得分:0)
您可以这样做:
<#assign modifiedObjectChng = modifiedObjectChng + { "beforeDep" : modifiedObjectChng.beforeDep + {"newTravelDate" : "AAA" }}>
<#assign modifiedObjectChng = modifiedObjectChng + { "beforeDep" : modifiedObjectChng.beforeDep + {"reissueAllowed" : "BBB" }}>
不太好,但是FreeMarker不支持修改传递给模板的对象。如果公开了setter方法(请参阅?api
),则更为方便,但总的来说,如果模板修改了对象,那就很奇怪了。