Freemarker将散列转换为String

时间:2016-06-20 05:27:28

标签: java hash freemarker

要迭代哈希映射,可以使用hash built-in来访问Map的值和键。现在我想对键进行操作,即上键来键(这是一个字符串):

<#assign keys = formats?keys>
<#list keys as key>
  ${key.toUpperCase()} = ${formats[key].getFileName()}
</#list>

但是我收到了错误

For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> key  [in template "src/main/resources/test.ftl" at line 41, column 69]

----
FTL stack trace ("~" means nesting-related):
- Failed at: ${key.toString().toUpperCase()}  [in template "src/main/resources/test.ftl" at line 41, column 67]
----

如何提示如何在模板中将密钥(小写)设置为大写?或者如何将哈希转换为字符串?

1 个答案:

答案 0 :(得分:2)

在FreeMarker中,隐藏了许多基本类(例如StringNumberList等)的Java API。而不是他们的API-s,你应该使用所谓的&#34;内置插件&#34;,就像在这种情况下:key?upper_case。 (另见:http://freemarker.org/docs/ref_builtins.html