いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Tapestry奮闘記-0007 (PageLinkコンポーネント)

PageLinkコンポーネントは文字通りページへのリンクを生成します。

[ページ仕様ファイル(Home.page)]

<page-specification class="gsf.samples.tapestry.Home">
    <!--

        PageLinkコンポーネント

        アンカー付きのリンクを出力するコンポーネント。
    -->
    <component id="pageLink001" type="PageLink">
        <!-- 
            hrefに出力して欲しいページへのリンクを属性pageに記述する。

            値は、リテラル値で指定しなければならない.
            (ex) literal:xxxxPage

            なお、値の部分にてliteralと指定しているのは、
            何も接頭辞を付けずに文字列を指定した場合、
            tapestryのページ仕様ファイルでは、ognlを指定した
            ことになってしまうから。

        -->
        <binding name="page">literal:SecondPage</binding>
    </component>

    <component id="insertText004" type="Insert">
        <binding name="value">literal:SecondPageへ</binding>
    </component>
</page-specification>

[テンプレートファイル(Home.html)]

<html jwcid="@Shell">
    <body jwcid="@Body">
        <!--
          ページリンクを作成
          (PageLinkコンポーネントを使用)
        -->
        <br/>
        <hr/>
        <div id="page_link_001" align="center">
            <b>
                PageLinkコンポーネントを使用して、ページリンクを出力
                <br/>
            </b>
            <br/>
            出力されたページリンク: <a href="./SecondPage.html" jwcid="pageLink001"><span jwcid="insertText004"></span></a>
        </div>
        <hr/>
        <br/>
    </body>
</html>

今回は、リンクなので遷移先のページも軽く作成します。

[ページ仕様ファイル(SecondPage.page)]

<page-specification class="org.apache.tapestry.html.BasePage">

    <component id="shell" type="Shell">
        <binding name="title">literal:"Secondページ [FROM SERVER SIDE]"</binding>
    </component>

    <component id="body" type="Body">
    </component>

    <component id="pageLink001" type="PageLink">
        <binding name="page">literal:Home</binding>
    </component>

</page-specification>

[テンプレートファイル(SecondPage.html)]

<html>
    <head>
        <title>Second Page [FROM LOCAL]
    </head>
    <body>
        <span jwcid="$content$">
            <span jwcid="shell">
                <span jwcid="body">
                    <br/>
                    <hr/>
                    <div id="page_link_001" align="center">
                        <a jwcid="pageLink001" href="./Home.html">Homeページに戻る</a>
                    </div>
                    <br/>
                    <hr/>
                </span>
            </span>
        </span>

        ローカルで見たときのみに見える文言.
        <br/>
    </body>
</html>

SecondPage側のテンプレートファイルにある

jwcid="$content$"

という記述は、Tapestryにとって特別なJWCになっていて、
このJWCの指定は、Tapestryに対してこのJWCが指定されているタグブロックの
内側のみを処理して、外側は無視しろという指示になります。
つまり、テンプレートファイルの

        <span jwcid="$content$">
            <span jwcid="shell">
                <span jwcid="body">
                    <br/>
                    <hr/>
                    <div id="page_link_001" align="center">
                        <a jwcid="pageLink001" href="./Home.html">Homeページに戻る</a>
                    </div>
                    <br/>
                    <hr/>
                </span>
            </span>
        </span>

のみが対象となり、Tapestryはその他の部分を無視します。
デザイナーさんが作成されたモックに対してTapestry化を
施す際にもともと存在するHTMLを削除したりする(不必要なmetaタグやjavascriptや文言など)と
今度はデザイナーさんが見たときにレイアウトが崩れたりするので
動的に変化させる部分に対して$content$を使用して、他は無視させます。
このようにすると、デザイナーさんが作成したモックも変えずに
ページに対して動的な処理を追加できることになります。

先ほどのSecondPage.htmlでは、実際にデプロイして画面を表示させると
$content$の外側にある

ローカルで見たときのみに見える文言.

という文言は表示されません。しかし、ローカルでブラウザからSecondPage.html
を開くと元のとおりに表示されるという具合です。

また、$content$と反対の意味をもつ特別なJWCも存在していまして、
こちらは、$remove$と記述します。意味はそのままですが、これが
記述されているタグブロックの内側をTapestryに無視させます。
なお、$content$と$remove$は、グローバルなJWCみたいなもので
ページ仕様ファイルなどに記述しなくてもそのまま使用できます。