복붙노트

[SPRING] 백리향, 조각 및 기본 매개 변수

SPRING

백리향, 조각 및 기본 매개 변수

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>

불행히도 위의 솔루션은 오류를 생성했습니다.

어떤 생각?

해결법

  1. ==============================

    1.Thymeleaf는 다음과 같은 명시적인 매개 변수없이 조각의 서명을 허용합니다.

    Thymeleaf는 다음과 같은 명시적인 매개 변수없이 조각의 서명을 허용합니다.

    <div th:fragment="my_fragment">
        <p th:text="${content}"></p>
        <p th:text="${defaultParameter}"></p>
    </div>
    

    이 조각을 호출하고 내용과 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. 
    

    따라서 이전 버전과의 호환성을 유지하려면 조각을 호출하는 동안 명명 된 매개 변수를 사용해야하며 조각의 서명에 매개 변수를 지정하지 않아야합니다.

  2. ==============================

    2.프래그먼트에 선택적 매개 변수를 허용하는 가장 좋은 방법은 "th : with"로 선언하고 의미있는 기본값으로 설명하는 것입니다.

    프래그먼트에 선택적 매개 변수를 허용하는 가장 좋은 방법은 "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>
    

    (태그 내부의 모든 값은 동적 값으로 대체됩니다. 더 읽기 쉽게하기위한 것입니다.)

  3. ==============================

    3.예를 들어 기본값을 제공하려는 경우 elvis 연산자를 사용하여 기본값을 지정할 수 있습니다. 예를 들면 다음과 같습니다.

    예를 들어 기본값을 제공하려는 경우 elvis 연산자를 사용하여 기본값을 지정할 수 있습니다. 예를 들면 다음과 같습니다.

    <div th:fragment="my_fragment">
        <p th:text="${content}"></p>
        <p th:text="${defaultParameter ?: 'the backwards compat value'}"></p>
    </div>
    

    참조 : elvis-operator

  4. ==============================

    4.마침내 나는 다음과 같은 작업을 해결했습니다.

    마침내 나는 다음과 같은 작업을 해결했습니다.

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

    그러나 오버 헤드입니다.

    또한 프래그먼트 매개 변수가 기본값을 갖도록 허용

  5. from https://stackoverflow.com/questions/22093149/thymeleaf-fragments-and-default-parameters by cc-by-sa and MIT license