legacy-knowledge-base
公開されました Jul. 2, 2025

テンプレートで構造体フィールドを変更した場合の対処法

written-by

Roberto Díaz

How To articles are not official guidelines or officially supported documentation. They are community-contributed content and may not always reflect the latest updates to Liferay DXP. We welcome your feedback to improve How To articles!

While we make every effort to ensure this Knowledge Base is accurate, it may not always reflect the most recent updates or official guidelines.We appreciate your understanding and encourage you to reach out with any feedback or concerns.

legacy-article

learn-legacy-article-disclaimer-text

問題

  • ユーザーがWebコンテンツの構造を変更し、ネストされたフィールドをこの構造の最上位レベルに移動させました。
  • 一部の記事でレンダリング時にエラーが発生する。
  • 例を挙げて説明しましょう:
    1. 2つのフィールドを持つ構造体を作成します:
      • 親ページ
    2. このコードで新しいTemplateが作成されます:
      <p>Parent:${parent.getData()}</p>
      <p>Child:${parent.child.getData()}</p>
    3. このとき、新しい記事(ARTICLE-1という名前)が作成されます。
    4. 子フィールドは構造体のルートに移動します。 これで、2つの兄弟フィールドができました:
      • 親ページ
    5. この時点で新しい記事(ARTICLE-2と命名)が作成されます。
    6. ページ内に2つのWebコンテンツ表示ポートレットを追加し、両方の記事(各ポートレットに1つずつ)を選択します。
    7. 結果:
      • ARTICLE-1は期待通りにレンダリングされる(テンプレートがフィールド名を必要なように処理するため)
      • ARTICLE-2では、エラーが発生します:
        2022-05-10 08:26:37.549 ERROR [http-nio-8080-exec-6][runtime:59] Error executing FreeMarker template
        FreeMarker template error:
        The following has evaluated to null or missing:
        ==> parent.child [in template "20101#20128#35258" at line 10, column 12]

        ----
        Tip: It's the step after the last dot that caused this error, not those before it.
        ----
        Tip: If the failing expression is known to legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing</#if>. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
        ----

        ----
        FTL stack trace ("~" means nesting-related):
        - Failed at: ${parent.child.getData()} [in template "20101#20128#35258" at line 10, column 10]
        ----

        Java stack trace (for programmers):
        ----

Environment

  • Liferay DXP 7.2+

解決策

  • エラーの根本的な原因は、構造を変更してもその記事の内容が更新されず、フィールド名が異なっていることです。
  • 正しくレンダリングするためには、テンプレートで両方のケースを処理する必要があるということです:
    • ARTICLE-1フィールドの値をレンダリングするには、テンプレートでこのコードを使用する必要があります:
      <p>Parent:${parent.getData()}</p>
      <p>Child:${parent.child.getData()}</p>
    • ARTICLE-2フィールドの値をレンダリングするために、テンプレートでこのコードを使用する必要があります:
      <p>Parent:${parent.getData()}</p>
      <p>Child:${child.getData()}</p>
  • この問題を解決するために、私たちは2つの解決策を考えています:
    1. それぞれのケースを処理するために2つのテンプレートを作成します。
    2. 同じテンプレートで処理します:
      <p>Parent:${parent.getData()}</p>

      //original field

      <p>
      <#if parent.child?? && parent.child.getData()?? >
      Nested child field value: ${parent.child.getData()}
      <#else>
      parent.child value is null
      </#if>
      </p>

      // modifided-moved field

      <p>
      <#if child?? && child.getData()?? >
      Modified child filed value: ${child.getData()}
      <#else>
      child value is null
      </#if>
      </p>
did-this-article-resolve-your-issue

legacy-knowledge-base