如何在 FreeMarker 中打印任何变量?

How to print any variable in FreeMarker?

我有一个 Map<String, Object>,其中包含字符串、双精度数、整数、布尔值和 null(缺失)作为值。我想打印出 FreeMarker 模板中的值。

当我使用 ${data[field]!?html} 时,它适用于所有其他变量,但在布尔值上崩溃:

freemarker.core._MiscTemplateException: Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy default computer-language format, and hence isn't accepted.

该应用程序是一个管理 UI,我不太关心确切的格式,但我更喜欢使用标准 truefalse 的布尔值. (boolean_format 标志控制布尔值的标准格式,但出于某些令人难以置信的原因,使用 truefalse 似乎是 explicitly prohibited。)

解决 FreeMarker 禁令的最佳方法是什么?

实现的方法是使用?string方法:${data[field]!?string?html}

这仅在 Configurable JavaDoc, and the ?string method is deprecated 中提到过。我希望它不会很快消失。

从 FreeMarker 2.3.29 开始,您可以将 boolean_format 配置设置设置为 c,这将像 ?c 那样将布尔值格式化为 truefalse.