Thymeleaf-조각에 변수 전달

Sep 21 2020

변수를 전달하는 조각이있는 다음 템플릿이 있습니다.

<div th:fragment="main">
    <span>
        <th:block th:include=" :: inner(${item})" /> </span> <a> <th:block th:include=" :: inner(${item})" />
    </a>
</div>

<th:block th:fragment="inner(item)">
    [[${item.name}]]
</th:block>

렌더링하려고하면 다음과 같은 오류가 발생합니다.

Property or field 'name' cannot be found on null

이렇게 렌더링하면

<div th:fragment="main">
    <span>
        <th:block th:include=" :: inner(${item})" />
        [[${item.name}]] </span> <a> <th:block th:include=" :: inner(${item})" />
    </a>
</div>

조각에 변수를 할당 할 때 내가 뭘 잘못하고 있니?

답변

michal.jakubeczy Sep 21 2020 at 21:47

그것을 알아 냈습니다.

오류의 원인은 메인 프래그먼트와 함께 렌더링하는 동안 '내부'프래그먼트가 렌더링 되었기 때문입니다. 따라서 자체적으로 렌더링 된 경우 내포물과 함께. 그리고이 마지막 렌더링은 그것을 망쳤습니다.

가능한 해결책은 다음과 같습니다.

  1. 내부 조각을 다른 파일로 이동
  2. th:if="false"자체 렌더링을 피하기 위해 '내부'조각에 추가 합니다 (포함은 계속 작동합니다).