【发布时间】: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