gpt4 book ai didi

java - thymeleaf 中带有填充列表的空索引异常

转载 作者:行者123 更新时间:2023-12-02 02:25:38 35 4
gpt4 key购买 nike

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

最佳答案

*{...} 表达式的行为与 ${...} 表达式不同。我不能确切地告诉你(你还没有发布足够的 html 来确定),但我猜这个表达式对你有用:

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

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

关于java - thymeleaf 中带有填充列表的空索引异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47815803/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com