春天/ Thymeleaf:财产或领域'标题'无法在null上找到。为什么?

时间:2016-01-07 10:30:46

标签: spring-mvc highcharts thymeleaf spelevaluationexception

应用程序抛出org.springframework.expression.spel.SpelEvaluationException:EL1007E:(pos 0):属性或字段' title'在null

上找不到

我无法理解为什么...... 这是由主人调用的html片段。

<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;
}

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

他们现在在数据库中 Surveys Database

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

提前感谢您的帮助。

编辑: 根据建议,我试图打印对象$ {surv}。我改变了&#34;幸存者&#34;内部div从ID到类的属性,因为它们被认为很多。

<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
      -->

首先,只打印一个结果。其次,它没有得到任何幸存物体。这就是为什么这个领域&#34;标题&#34;我猜是空的。 那么为什么对象为null呢?有什么建议吗?

1 个答案:

答案 0 :(得分:2)

我怀疑这是因为您在使用片段时正在使用th:include。根据{{​​3}}:

  

而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>
相关问题