Fight the Future

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

Introducing Apache Wicketの超意訳(7)

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

I've added three methods to the WicketApplication class.
The first two provide access to the ContactDao used for persistence operations.
The third overrides the Application#get() method to allow for covariant return types, in this case the WicketApplication class.
This example uses the WicketApplication as a form of Service Locator http://java.sun.com/blueprints/patterns/ServiceLocator.html.
(There are various ways to provide access to your DAO and service layer objects, including annotations for Spring- or Guice-managed beans.
This is the easiest for our purposes.)
I've also changed the home page from HomePage.class to the nonexistent ListContacts.class.

WicketApplicationクラスに3つのメソッドを追加しました。
1つめと2つめのメソッドは永続化の操作を呼び出すためにContactDaoにアクセスできるようにするものです。
3つめのメソッドは共変戻り値*1を利用できるようにApplication#get()メソッドをオーバーライドします。この場合の共変戻り値はWicketApplicationクラスです。
このサンプルではService Locatorhttp://java.sun.com/blueprints/patterns/ServiceLocator.htmlの型としてWicketApplicationを使います。
(DAOやサービス層のオブジェトへのアクセス手段を提供する方法はいくつもあります。SpringやGuiceが管理するbeanに対するアノテーションも含みます。
これはそのもっとも簡単な方法です。)
またホームページをHomePage.classから存在しないListContacts.classに変更しました。

public class WicketApplication extends WebApplication {

    public Class getHomePage() {
        return ListContacts.class;
    }

    private ApplicationContext getContext() {
        return WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
    }

    public ContactDao getContactDao() {
        return (ContactDao) getContext().getBean("jdbcContactDao");
    }

    public static WicketApplication get() {
        return (WicketApplication) WebApplication.get();
    }
}

Next we need to create a base class for our application pages.
The base class allows us to take advantage of markup inheritance.
Let's look at the code first, then markup:

次にアプリケーションのページに対するベースクラスを作成する必要があります。
ベースクラスを使うとマークアップの継承を利用することができます。
まずコードを見て、そのあとマークアップを見ましょう。

public class BasePage extends WebPage {

    public BasePage() {
        add(new StyleSheetReference("stylesheet",
              BasePage.class, "styles.css"));
    }

}

Our BasePage class extends WebPage, a Wicket class that sets the markup type to "text/html".
If we wanted to serve up XML content instead of HTML, we'd extend Page and override the getMarkupType() method like so:

BasePageクラスはWebPageを継承します。Wicketのクラスはマークアップの型を"text/html"にセットします。
もしHTMLではなくXMLのコンテンツとするなら、Pageを継承し、getMarkupType()メソッドを次のようにオーバーライドします。

public String getMarkupType() {
    return "text/xml";
}

The markup type determines the file extension loaded for markup.
In our case, BasePage is looking for BasePage.html.
By default, markup files are located in the same directory as the component, but this is easily configured to meet your needs.


The BasePage constructor adds a StyleSheetReference.
The StyleSheetReference inserts the path to the referenced CSS into the element in the markup header.
Why would you want to use a StyleSheetReference instead of simply leaving the stylesheet under the web root?
Suppose your application needs to present different images or styles based on the user's locale, so you have two stylesheets: styles_en.css and styles_fr.css.
StyleSheetReference loads the correct locale-specific file automatically.
This behavior is the same across all of Wicket's resource loading, including properties files and images.


The BasePage.html markup is pretty simple:

マークアップの型はマークアップのためにロードするファイルの拡張子を決定します。
この場合、BasePageはBasePage.htmlを探します。
デフォルトではマークアップファイルは同じディレクトリにコンポーネントとして配置しますが、これは簡単に設定を変更できます。


BasePageのコンストラクタにStyleSheetReferenceを追加します。
StyleSheetReferenceはメークアップのヘッダーにある要素に参照するCSSのパスを設定します。
スタイルシートを単純にWebのルートに置くのではなくStyleSheetReferenceを使うのはなぜでしょう?
仮にユーザの場所によってアプリケーションの画像やスタイルを変更する必要があるとしましょう。
すると2つのスタイルシートが必要になります。styles_en.cssとstyles_fr.cssのように。
StyleSheetReferenceは場所にあった適切なファイルを自動的にロードします。
Wicketがリソースをロードするときはすべてこのような振る舞いになります。プロパティファイルや画像もそうです。


BasePage.htmlのマークアップは非常に単純です。

<html>
<head>
<title>Introduction to Apache Wicket - Example Application</title>
<link wicket:id="stylesheet"/>
</head>

<body>

<h1>
My Contacts
</h1>

<form id="search-form">
	<table>
		<tr>
			<td>Search</td>
			<td><input type="text" name="search"/></td>
			<td><input type="submit" value="Go" class="button"></td>
			<td><a href="#">Add New Contact</a></td>
		</tr>
	</table>
</form>

<wicket:child/>

</body>
</html>

*1:共変戻り値についてはhttp://d.hatena.ne.jp/muimy/20070609/1181380882に記載があります