Thymeleaf在null上调用方法报错 Property or field ‘getMessage‘ cannot be found on null

org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field ‘getMessage’ cannot be found on null

SpringBoot整合Thymeleaf报错
在这里插入图片描述

原因:在null上调用了方法。

错误,第一次的时候没有异常, 所以th:if="${not #strings.isEmpty(exception.getMessage())在调用getMessage()方法的时候就会出现方法在null放被调用的异常,修改成th:if="${not #strings.isEmpty(exception)直接判断exception是不是空就行了,不用调用它的方法

<!--错误,第一次的时候没有异常,
            所以th:if="${not #strings.isEmpty(exception.getMessage())在调用getMessage()方法的时候
            就会出现方法在null放被调用的异常,
            修改成th:if="${not #strings.isEmpty(exception)
            直接判断exception是不是空就行了,不用调用它的方法-->
<!--        <span th:text="${(exception.getMessage())}" th:if="${not #strings.isEmpty(exception.getMessage())}" style="color: red"></span>-->
<!--        <span th:text="${(exception)}" th:if="${not #strings.isEmpty(exception)}" style="color: red"></span>-->

        <span th:text="${(exception.getMessage())}" th:if="${not #strings.isEmpty(exception)}" style="color: red"></span>

猜你喜欢

转载自blog.csdn.net/qq_46070108/article/details/116951150
今日推荐