Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc Spring/Thymeleaf:属性或字段';标题';在null上找不到。为什么?_Spring Mvc_Highcharts_Thymeleaf_Spelevaluationexception - Fatal编程技术网

Spring mvc Spring/Thymeleaf:属性或字段';标题';在null上找不到。为什么?

Spring mvc Spring/Thymeleaf:属性或字段';标题';在null上找不到。为什么?,spring-mvc,highcharts,thymeleaf,spelevaluationexception,Spring Mvc,Highcharts,Thymeleaf,Spelevaluationexception,应用程序抛出org.springframework.expression.spel.SpelEvaluationException:EL1007E:(位置0):在null上找不到属性或字段“title” 我不明白为什么。。。 下面是主控程序调用的html代码段 <title th:fragment="title">Surveys Feed</title> <div th:fragment="content" th:each="surv : ${allSurveys}

应用程序抛出org.springframework.expression.spel.SpelEvaluationException:EL1007E:(位置0):在null上找不到属性或字段“title”

我不明白为什么。。。 下面是主控程序调用的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;
}
@modeldattribute(“所有调查”)
公共列表所有调查(){
列表调查=新调查(repo.findAll()).getSurveys();
//打印调查标题
对于(int i=0;i
作为证据,我们可以看到调查标题打印在控制台上:

它们存在于数据库中

那么为什么它说这是空的呢?我在网上看到了不同的答案,但似乎没有一个能解决我的问题

提前感谢您的帮助

编辑: 根据建议,我尝试打印对象${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>

对象是:obj

当我分析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”是空的。
那么为什么对象是空的呢?有什么建议吗?

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

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

/*

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