타임리프, 프래그먼트 및 기본 파라미터
fragments.html 파일을 만들었습니다.여기에는 다음과 같은 조각이 포함되어 있습니다.
<div th:fragment="my_fragment(content)">
<p th:text="${content}"></p>
</div>
위 조각을 보기 파일에 넣었습니다.
<div th:replace="fragments :: my_fragment('test')"></div>
이제 두 개의 매개 변수를 my_fragment에 전달하고 싶지만 하위 호환성을 보장해야 합니다.
저는 다음과 같이 문제를 해결하려고 했습니다.
<div th:fragment="my_fragment(content, defaultParameter='default value')">
<p th:text="${content}"></p>
</div>
안타깝게도 위 솔루션에서 오류가 발생했습니다.
org.springframework.web.graphics.NestedServletException: 요청 처리에 실패했습니다. Nested 예외는 org.thymeleaf입니다.예외들템플릿 처리 예외: 조각을 확인할 수 없습니다.서명 "my_parameter(content, defaultParameter='default value')"는 2개의 매개 변수를 선언하지만 프래그먼트 선택은 1개의 매개 변수를 지정합니다.조각 선택이 올바르게 일치하지 않습니다.
감 잡히는 게 없어요?
타임리프는 다음과 같은 명시적인 매개변수 없이 단편의 서명을 허용합니다.
<div th:fragment="my_fragment">
<p th:text="${content}"></p>
<p th:text="${defaultParameter}"></p>
</div>
이 조각을 호출하고 전달하려면 다음과 같이 하십시오.content그리고.defaultParameter프래그먼트는 다음과 같이 부를 수 있습니다.
<!-- both parameters not specified -->
<div th:replace="fragments :: my_fragment"></div>
<!-- only content parameter specified -->
<div th:replace="fragments :: my_fragment(content='a')"></div>
<!-- both parameters specified -->
<div th:replace="fragments :: my_fragment(content='a', defaultParameter='b')"></div>
그러나 아래는 작동하지 않습니다.
<div th:replace="fragments :: my_fragment('a', 'b')"></div>
그리고 그 메시지는 스스로 설명할 수 있습니다.
Signature "my_fragment" declares no parameters, but fragment selection did specify parameters in a synthetic manner (without names), which is not correct due to the fact parameters cannot be assigned names unless signature specifies these names.
따라서 하위 호환성을 유지하려면 fragment를 호출하는 동안 명명된 매개 변수를 사용해야 하며 fragment의 서명에 매개 변수를 지정하지 않아야 합니다.
프래그먼트에 대한 선택적 매개변수를 허용하는 가장 좋은 방법은 "th:with"로 선언하고 의미 있는 기본값으로 설명하는 것입니다.
따라서 프래그먼트의 선언 태그에서 필수 값과 선택 값을 명시적으로 정의합니다.
다음은 1개의 필수 파라미터와 2개의 선택 파라미터로 구성된 간단한 예입니다.
<div th:fragment="printGreetings (name)" th:with="title=${title} ?: 'Mr.', greeting=${greeting} ?: 'Hello'">
<span th:text="${greeting}">Hello</span>
<span th:text="${title}">Mr.</span>
<span th:text="${name}">John Doe</span>
</div>
그러면 다음과 같이 부를 수 있습니다.
<div th:replace="fragments :: printGreetings (name='daniel')">
Hello Mr. Daniel
</div>
<div th:replace="fragments :: printGreetings (name='Lea', title='Ms.')>
Hello Ms. Lea
</div>
<div th:replace="fragments :: printGreetings (name='Lea', title='Ms.', greeting='Hi')>
Hi Ms. Lea
</div>
(태그 내부의 모든 값은 동적 값으로 대체됩니다.단지 더 나은 읽기를 위해서입니다.)
예를 들어, 기본값을 제공하려는 경우 elvis 연산자를 사용하여 기본값을 지정할 수 있습니다.
<div th:fragment="my_fragment">
<p th:text="${content}"></p>
<p th:text="${defaultParameter ?: 'the backwards compat value'}"></p>
</div>
보기: 엘비스-엘비스
마침내 나는 다음과 같은 과제를 해결했습니다.
<div th:fragment="my_fragment2(content, param2)">
<th:block th:replace="my_fragment(${content})"/>
</div>
<div th:fragment="my_fragment(content)" th:with="param2=${param2} ?: 'default value'">
<p th:text="${content}"></p>
<p th:text="${param2}"></p>
</div>
<div th:replace="fragments :: my_fragment2('test', 'my_value')"></div>
하지만 그건 오버헤드입니다:(
프래그먼트 매개변수가 기본값을 가지도록 허용을 발행하기 위해 질문도 합니다.
내게 가장 좋은 방법은:
조각:
<div th:fragment="my_fragment(content)">
<p th:text="${content}"></p>
</div>
모델에서 컨텐츠가 제공되는 경우:
<div th:replace="fragments :: my_fragment(${content})></div>
기타:
<div th:replace="fragments :: my_fragment('content')></div>
언급URL : https://stackoverflow.com/questions/22093149/thymeleaf-fragments-and-default-parameters
'programing' 카테고리의 다른 글
| HTML div 태그의 테두리를 설정하는 방법 (0) | 2023.09.09 |
|---|---|
| jquery, 클래스별로 다음 요소 찾기 (0) | 2023.09.09 |
| VS Code에서 [pwsh]와 [Powershell Integrated Console]의 차이점은 무엇입니까? (0) | 2023.09.09 |
| 클래스 선택기 및 속성 선택기를 jQuery와 결합 (0) | 2023.09.09 |
| REGEX를 사용하여 표를 쿼리하여 A OR B 찾기 (0) | 2023.09.09 |