百里香叶中的空索引异常,带有填充列表

时间:2017-12-14 14:32:11

标签: java spring-boot thymeleaf

我在java端有一个非空的列表。但在前端我有一个空索引异常。你有什么想法吗?

我有这段代码:

<div th:if="!${#strings.endsWith(partitions[__${rowPartitionStat.index}__].nom,'_in')}">

我也试过这段代码但没有成功:

<div th:if="!${#strings.endsWith(*{partitions[__${rowPartitionStat.index}__].nom},'_in')}">

在最后一种情况下,我有一个解析异常,所以我认为第一个更好。

第一个导致此异常:

2017-12-14 15:23:28.937 ERROR 8272 --- [nio-8990-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "#strings.endsWith(partitions[0].instances[0].natures[0].nom,'_in')" (topologieCles:155)] with root cause

org.springframework.expression.spel.SpelEvaluationException: EL1012E:(pos 9): Cannot index into a null value
    at org.springframework.expression.spel.ast.Indexer.getValueRef(Indexer.java:142) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.Indexer.getValueInternal(Indexer.java:89) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getArguments(MethodReference.java:154) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueRef(MethodReference.java:71) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:66) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267) ~[spring-expression-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:139) ~[thymeleaf-spring4-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.thymeleaf.standard.expression.VariableExpression.executeVariable(VariableExpression.java:154) ~[thymeleaf-2.1.5.RELEASE.jar:2.1.5.RELEASE]

虽然很少一行后来这一行正常工作:

<label th:text="*{partitions[__${rowPartitionStat.index}__].nom}" class="col-sm-4 control-label">...</label>

以下是pom.xml的摘录:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

1 个答案:

答案 0 :(得分:2)

*{...}表达式的行为与${...}表达式不同。我无法确切地说出来(你还没有发布足够的HTML确认),但我猜这个表达式对你有用:

<div th:if="!${#strings.endsWith(#object.partitions[__${rowPartitionStat.index}__].nom,'_in')}">

您获得空指针访问权限的原因是因为分区可能是th:object上的属性而不是模型属性。这对于*{...}表达式来说很好,但对于${...}表达式,您必须提供整个路径 - 这就是我使用特殊变量#object的原因。<\ n / p>