HelloWorld #3

引き続きHelloWorldです。
http://wicket.sourceforge.net/ExampleHelloWorld.html
さっさと終えろという感じもしてきましたので、これでラスに。

さて、http://d.hatena.ne.jp/kohata/20050829#p1のつづきですが、
HelloWorldというクラスが Webページに対応する(らしい)のですが、どんなもんでしょう?

package wicket.examples.helloworld;
import wicket.markup.html.WebPage;
import wicket.markup.html.basic.Label;

public class HelloWorld extends WebPage {
    public HelloWorld() {
        add(new Label("message", "Hello World!"));
    }
}

add()? Label? 何でしょう?いまいちイメージがつかめませんです。

The Label is constructed using two parameters:
- "message"
- "Hello World!"
The first parameter is the component identifier, which Wicket uses to identify the Label component in your HTML markup. The second parameter is the message which the Label should render

"Hello World!"は、表示データで、"message" はキーということですね。
おっと、request#setAttribute()がすぐにイメージされるのは職業病か。。
これは、Labelっていうコンポーネント(画面の部品なのか。。Swing ライクとか言ってたし。)に対する識別子だそうです。

うーむ、こいつはコントローラなのかビューなのか。。そもそもそういうモデルとも無縁なのか。テンプレートとJavaクラスの接合部分がいまひとつピンとこないので、我慢しないで、テンプレートをみてみようと思います。

ええと、、あぁ、次にあるね。

HelloWorld.html:


    <span wicket:id="message">Message goes here</span>


おー、簡素簡素。普通のHTML(ファイル拡張子もhtml)なんですね。
Message goes hereのところに、さっきの Hello World! が表示されるのでしょうね。

In this file, you see two elements that need some attention:
- the component declaration <span wicket:id="message">
- the message Message goes here
The component declaration consists of the Wicket identifier wicket and the component identifier message. The component identifier should be the same as the name of the component you defined in your WebPage. The message between the tags is removed when the component renders its message. The final content of the component is determined by your Java code.

spanタグのとこに wicket固有の属性がみえますね。<span wicket:id="〜">が、コンポーネントの宣言?なのか。
そんでもって、なになに。コンポーネントの宣言部というのは、Wicket ID(wicketというとこですね)とコンポーネントの識別子(ここでは、message?)から成り立つ。。
コンポーネントの識別子というのは、HelloWorldクラスで Labelに指定したキーですね。
ようやくわかってきました。
<span wicket:id="〜"> と </span> の間の領域が、wicket:idで指定されるキーで関連付けられるコンポーネントと紐付くんですね。
Message goes hereは仮データで、実際はコンポーネントレンダリングで上書きされるのですね。
ふーん。なるほど面白い。

※ちなみに、テンプレートとなるHTMLソースは、WebPageクラスと同じパッケージディレクトリに置いていかなければならないようです。ほんとにそうなのか???プログラムと一緒って、これまで考えたこともありませんでした。