有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在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) 个答案

  1. # 1 楼答案

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

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

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