【问题标题】:Apache Freemarker NonHashException: Expected a hash, but this has evaluated to a stringApache Freemarker NonHashException:需要一个哈希,但这已经评估为一个字符串
【发布时间】:2022-11-10 15:40:12
【问题描述】:

我想用 Spring 和 Apache Freemarker 建立一个搜索站点。我有一个表单,其中包含有关先前搜索的一些数据,它存储在一个简单的数据类中,如下所示:

import lombok.Data;

@Data
public class TicketSearchForm {
    private String status = "ALL";
    
    //More fields...
}

我希望使用通过我的 Spring 请求的Model 传递的数据来确定选择的默认值:

<select name="status" id="status" class="form-control select2">
                                        <option value="ALL" <#if previousSearch.getStatus().equals("ALL")>selected</#if>>Alle anzeigen</option>
                                        <option value="OPEN" <#if previousSearch.status.equals("OPEN")>selected</#if>>Offen</option>
                                        <option value="DONE" <#if previousSearch.status.equals("DONE")>selected</#if>>Geschlossen</option>
                                    </select>

但是,这会得到错误的回报

Error executing FreeMarker template

freemarker.core.NonHashException: For "." left-hand operand: Expected a hash, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> previousSearch.status  [in template "ticket/search.ftlh" at line 22, column 66]

----
FTL stack trace ("~" means nesting-related):
    - Failed at: #if previousSearch.status.equals("ALL")  [in template "ticket/search.ftlh" in macro "page_body" at line 22, column 61]
    - Reached through: @page_body  [in template "base.ftlh" at line 86, column 9]
    - Reached through: #include "/base.ftlh"  [in template "ticket/search.ftlh" at line 1, column 1]
----

previousSearch 确实包含所有数据,我可以在网站上将其显示为字符串,但我似乎无法访问它的任何属性。为什么会这样,我该如何访问它们?我宁愿不通过模型将我需要的所有值作为单独的布尔值传递,这对我来说似乎是错误的。

【问题讨论】:

    标签: java spring freemarker


    【解决方案1】:

    问题出在.equals()

    由于我的应用程序的 java 上下文,我感到困惑。 Freemarker 对字符串的处理与 Java 不同,要走的路是使用== 来比较字符串。

    【讨论】:

      猜你喜欢
      • 2019-08-20
      • 2014-03-16
      • 1970-01-01
      • 2015-12-28
      • 2014-01-28
      • 1970-01-01
      • 2016-10-21
      • 2020-10-22
      • 2017-05-13
      相关资源
      最近更新 更多