Fight the Future

Java言語とJVM、そしてJavaエコシステム全般にまつわること

Introducing Apache Wicketの超意訳(5)

http://www.theserverside.com/tt/articles/article.tss?l=IntroducingApacheWicket

So far we've looked at two IModel implementations, but there are a few more that are very useful when starting with Wicket.
You can also combine models using the familiar decorator pattern.
Suppose you have a single domain object used by several components in a page.
It doesn't make sense to load the object for each component.
This is where the CompoundPropertyModel comes in:

これまでIModelの実装を2つ見てきましたが、Wicketを始めるにあたっていくつかさらに役に立つことがあります。
なじみのあるデコレーターパターンを使ってモデルを組み合わせることもできます。
ページにおけるいくつかのコンポーネントが使用しているドメインオブジェクトが1つあるとしましょう。
コンポーネントがそのオブジェクトをロードすると意味がありません。
これがCompoundPropertyModelの必要なところです。

IModel playerModel =
  new CompoundPropertyModel(new LoadableDetachableModel() {
    public Object load() {
        return playerService.getPlayer();
    }
});
setModel(playerModel);

Setting the model at for the Page allows components to access properties based on their Wicket component ID.
Assuming we've set the model for the Page, populating components is trivial:

ページにモデルをセットすることでコンポーネントWicketコンポーネントIDを基にプロパティにアクセスできます。
ページにモデルをセットしてみましょう。コンポーネントを投入することはすぐにできます。

add(new Label("firstName"));

Label will look for a method in the model object called "getFirstName()".
What if the Wicket ID doesn't correspond to the property name of the model object?
You can specify another property name using the aptly named PropertyModel:

LabelはモデルオブジェクトからgetFirstName()メソッドを探します。
WicketのIDがモデルオブジェクトのプロパティ名と一致しないとしたらどうでしょうか?
適切に命名されたPropertyModelを使って他のプロパティ名を明示します。

add(new Label("fname", new PropertyModel(getModel(), "firstName"));

In addition to simple property access, PropertyModels can also access collection or array elements.

単純なプロパティアクセスに加えて、PropertyModelはコレクションや配列の要素にもアクセスできます。

Developers new to Wicket typically have difficulty grasping the model concept,
particularly if they haven't spent much time developing with other frameworks leveraging models, such as Swing.
Remember that models have a single purpose: to abstract how your components get data.
When should you use models?
You should always pass models to components, not domain objects.
Don't worry if you don't have a full understanding of models.
The example below will help explain the concept in more detail.

Wicketを始めた開発者はたいていモデルの概念を掴むときに壁があります。
Swingのようなモデルを利用したフレームワークを使って開発した経験があまりなければ特にそうです。
モデルには1つしか目的がないことを思い出してください。コンポーネントがデータを取得する方法を抽象化することです。
モデルはいつ使うべきか?
コンポーネントにはドメインオブジェクトではなくモデルをいつも渡すべきです。
もしモデルを完全に理解していなくても心配要りません。
下の例はそのコンセプトをより詳しく説明します。

So far we've reviewed components, including pages and panels, as well as models.
Next, we need to explore Wicket's Application class, the heart of every Wicket application.

これまでモデルだけでなくページやパネルも含んで、コンポーネントを概観しました。
次は、すべてのWicketアプリケーションの中心であるWicketのアプリケーションクラスを探ります。