【问题标题】:Spring / Thymeleaf: Property or field 'title' cannot be found on null. Why?Spring / Thymeleaf:在 null 上找不到属性或字段“标题”。为什么?
【发布时间】:2016-04-11 16:57:31
【问题描述】:

应用程序抛出 org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'title' cannot be found on null

我不明白为什么... 这里是html sn-p,master调用的。

<title th:fragment="title">Surveys Feed</title>
<div th:fragment="content" th:each="surv : ${allSurveys}" >
    <div id="surv-ct" style="min-width: 310px; max-width: 800px;     height:400px; margin: 0 auto">
    <script th:inline="javascript">
       /*<![CDATA[*/
           $('#surv-ct').highcharts({
               chart: {
                   type: 'bar'
               },
               title: {
                   text: [[${surv.title}]]
               },
             // ... other stuff 
    </script>
  </div>
</div>

然而,当应用程序启动时,字段标题不为空! 这里我们有对象 allSurveys 的映射,它从 repo 中获取所有调查并返回一个列表。

@ModelAttribute("allSurveys")
public List<Survey> allSurveys() {
    List<Survey> surveys = new Surveys(repo.findAll()).getSurveys();
    // print surveys titles
    for (int i = 0; i < surveys.size(); i++) {
        Survey sur = surveys.get(i);
        System.out.println("Survey info: " + sur.getTitle());
    }
    return surveys;
}

作为证据,我们可以看到调查的标题打印在控制台上: Console output

并且它们存在于数据库中 Surveys Database

那么为什么它说那是空的?我在互联网上看到了不同的答案,但似乎没有一个适合我的情况。

提前感谢您的帮助。

编辑: 根据建议,我尝试打印对象 ${surv}。我将内部 div 的“surv-ct”属性从 ID 更改为 class,因为它们应该很多。

<div th:fragment="content" th:each="surv : ${allSurveys}" >
    <div class="surv-ct" style="min-width: 310px; max-width: 800px; height: 400px; margin: 0 auto">
        <p>Object is: <span th:text=${surv}>obj</span></p>
        <!--
        <script th:inline="javascript">
           /*<![CDATA[*/

               $('.surv-ct').highcharts({
                   chart: {
                       type: 'bar'
                   },
                   // ... more code
    -->
    </div>
</div>

当我在 Mozilla 上分析代码时,结果是这样的。

    <p>Object is: <span></span></p>
    <!--
    <script th:inline="javascript">
       /*<![CDATA[*/
           $('.surv-ct').highcharts({
                // ... more code that i deleted
                // it's commented anyway
      -->

首先,只打印一个结果。其次,它没有得到任何生存对象。这就是我猜“标题”字段为空的原因。 那么为什么对象为空呢?有什么建议吗?

【问题讨论】:

  • 我要做的调试是暂时删除[[${surv.title}]],而只在某处打印${surv}。通过这种方式,您将看到哪些调查为空(因为其中一项或多项调查为空,而不是标题)、调查列表的大小是否准确等。
  • 通常标题应该是文本,但在你的情况下似乎是嵌套数组中的文本。
  • 不,这是 javascript-thymeleaf 表示法(显然)。 See here

标签: spring-mvc highcharts thymeleaf spelevaluationexception


【解决方案1】:

我怀疑这是因为您在使用片段时使用了th:include。根据Thymeleaf Documentation

虽然 th:include 会将片段的内容包含到它的主机标签中,但 th:replace 实际上会用片段的标签替换主机标签

这意味着您的 th:each 将被忽略 - 解释为什么您只看到打印的一个结果。

解决方案是使用th:replace,这将使用th:if。或者您可以稍微更改片段的格式,使th:each 成为片段内容的一部分。例如:

<th:block th:fragment="content">
    <div th:each="surv : ${allSurveys}">
        <div id="surv-ct" style="min-width: 310px; max-width: 800px;     height:400px; margin: 0 auto">
            <script th:inline="javascript">
               /*<![CDATA[*/
                   $('#surv-ct').highcharts({
                       chart: {
                           type: 'bar'
                       },
                       title: {
                           text: [[${surv.title}]]
                       },
                     // ... other stuff 
            </script>
        </div>
    </div>
</th:block>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 2018-02-06
    • 1970-01-01
    • 2018-03-31
    相关资源
    最近更新 更多