FreeMarker:预期为哈希,但这已评估为序列

时间:2019-03-29 10:31:43

标签: java freemarker

我知道相同的问题已经在这里。但不幸的是,这无助于我解决问题。我的FreeMarker模板中有HashSet。我想在视图上显示HashSet的大小。我对此有以下要求:

 <td>${repo.getRepoWords().size()}</td>

方法getRepoWords是:

 public HashSet getRepoWords() {
     return repoWords;
 }

结果我看到了这个异常

FreeMarker template error (DEBUG mode; use RETHROW in production!): For "." left-hand operand: Expected a hash, but this has evaluated to a sequence (wrapper: f.t.SimpleSequence): ==> repo.getRepoWords() [in template "repositories/detail.ftl" at line 24, column 23] ---- FTL stack trace ("~" means nesting-related): - Failed at: ${repo.getRepoWords().size()} [in template "repositories/detail.ftl" at line 24, column 21] ~ Reached through: #nested [in template "layouts/common.ftl" in macro "page" at line 20, column 1] ~ Reached through: @c.page title="Главная страница" [in template "repositories/detail.ftl" at line 2, column 1] ---- Java stack trace (for programmers): ---- freemarker.core.NonHashException: [... Exception message was already printed; see it above ...] at freemarker.core.Dot._eval(Dot.java:48) at freemarker.core.Expression.eval(Expression.java:83) at freemarker.core.MethodCall._eval(MethodCall.java:58) at freemarker.core.Expression.eval(Expression.java:83) at

我可以在FreeMarker模板中为HashSet使用size()方法吗?

1 个答案:

答案 0 :(得分:2)

我建议您像下面那样使用?size。请检查Document

${repo.repoWords?size}