レベルエンター山本大のブログ

面白いプログラミング教育を若い人たちに

BLOCKVROCKリファレンス目次はこちら

Wicket Examples ■Hello World!

WicketのHelloWorldサンプルのチュートリアル



HelloWorld demonstrates the basic structure of a web application in Wicket.
A Label component is used to display a message on the home page for the application.


このHelloWorldはWicketを使ったWebアプリケーションの基本的な構造を説明するデモです。
アプリケーションの最初のページでラベルコンポーネントを使ってメッセージを表示します。



In all the Wicket examples, you have to put all files in the same package directory.
This means putting the markup files and the java files next to one another.
It is possible to alter this behavior, but that is beyond the scope of this example.
The only exception is the oblibatory web.xml file which should reside in the WEB-INF/ directory of your web application root folder.


すべてのWicket サンプルは、同じパッケージディレクトリにおいてください。
つまり、マークアップ(HTML)ファイルとJavaファイルを隣合うようにおきます。
(HTMLの配置は)他にも方法はありますが、それを説明するのはこのサンプルの範囲外です。
唯一の例外はウェブアプリケーションルートフォルダのWEB-INF/ディレクトリにあるweb.xmlファイルです。


If you wish to start building this example, you may want to take a look at the Wicket Quickstart project,
which provides a quick way of getting up and running without having to figure things out yourself.
The Quickstart project contains the necessary build files (Ant and Maven), libraries, minimal set of Java and markup files and an embedded Jetty server to run your application without having to go through the whole build-deploy cycle.


このサンプルを動かしたいなら、Wicket クイックスタートプロジェクトを参照してください。
(※訳注 TomcatでWebアプリをデプロイできる人なら別に読まなくてもかまいません。)
そこに、理解してない人でもすばやく起動・実行する方法を紹介しています。
クイックスタートプロジェクトは、ビルドに必要なファイル(Ant およびMaven)、ライブラリ、Javaマークアップファイルの最小のセット、内臓のJettyサーバーを含んでいます。


HelloWorldApplication.java


Each Wicket application is defined by an Application object. This object defines what the home page is, and allows for some configuration.


それぞれのWicketのアプリケーションは、Applicationオブジェクト(WebApplicationを継承したクラス)によって定義されます。このオブジェクトには、「Homeページは何か?」を定義しています。そして、幾つかの設定が記述できます。

package wicket.examples.helloworld;
import wicket.protocol.http.WebApplication;

public class HelloWorldApplication extends WebApplication
{
    public HelloWorldApplication()
    {
    }

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


Here you can see that we define wicket.examples.helloworld.HelloWorld to be our home page.
When the base URL of our application is requested, the markup rendered by the HelloWorld page is returned.


ホームページとなるwicket.examples.helloworld.HelloWorldクラスを定義しました。
アプリケーションのベースとなるURLがリクエストされたときに、HelloWorld ページ(WebPageを継承したクラス)によってマークアップが出力されて返されます。

HelloWorld.java


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!"));
    }
}


The Label is constructed using two parameters:


このラベルのコンストラクタは、2つのパラメータを使います。


"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.


はじめのパラメータは、コンポーネントの識別子です。この識別子は、WicketがHTMLマークアップのなかのラベルコンポーネントを識別するために利用します。
2つ目のパラメータは、ラベルに出力するメッセージです。

HelloWorld.html

The HTML file that defines our Hello World functionality is as follows:


Hello Worldの機能を定義したHTMLファイルは以下のようになります。

<html>
<body>
    <span wicket:id="message">Message goes here</span>
</body>
</html&gtIn this file, you see two elements that need some attention:


・the component declaration <span wicket:id="message">
・the message Message goes here


・<span wicket:id="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 <span> tags is removed when the component renders its message.
The final content of the component is determined by your Java code.


コンポーネントは、"message"というWicketの識別子で宣言されています。
コンポーネントの識別子はWebPageで定義したコンポーネントの名前と同じでなくてはなりません。
<span>タグで囲まれた間にある文字列は、コンポーネントがメッセージを書き出すときには消されます。
コンポーネントの最終的な内容はJavaCodeによって上書きされます。

web.xml
In order to deploy our HelloWorld program, we need to make our application known to the application server by means of the web.xml file.

Hello Worldプログラムをデプロイするには、アプリケーションサーバーにアプリケーションを知らせるという意味でweb.xmlファイルを作る必要があります。


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
      PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
      "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
    <display-name>Wicket Examples</display-name>
    <servlet>
        <servlet-name>HelloWorldApplication</servlet-name>
        <servlet-class>wicket.protocol.http.WicketServlet</servlet-class>
        <init-param>
          <param-name>applicationClassName</param-name>
          <param-value>wicket.examples.helloworld.HelloWorldApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloWorldApplication</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>
</web-app>


In this definition you see the Wicket servlet defined, which handles all requests.
In order to let Wicket know which application is available, only the applicationClassName servlet parameter is needed.


この定義の中で、すべてのリクエストを処理するWicketサーブレットを定義しています。
Wicketは、利用可能なアプリケーションをWicketに知らせるために、アプリケーションクラス名のサーブレットパラメータだけが必要です。


Also, notice the url-mapping to /app/*.
This ensures that the Wicket servlet will only process requests made to the application itself,
not to (static) resources that are outside the realm of the Wicket application, such as style sheets, JavaScript files, images and so forth.


また、「url-mapping」には、「/app/*」と記述してください。
こう記述するのは、Wicketサーブレットがアプリケーション自身が作成したリクエストのみを処理することを保証するためです。
たとえば、スタイルシートJavaScriptファイル、イメージなどのような、Wicketアプリケーションの領域外の静的なリソースへのリクエストを除外します。


Ready to deploy That's it.
No more configuration necessary! All you need to do now is to deploy the web application into your favourite application server.
Point your browser to the url: http://<servername>/<warfilename>/app, substituting servername and warfilename to the appropiate values, such as http://localhost:8080/helloworld/app.


デプロイの準備はここまでです。
設定ファイルは、もう必要ありません。
どんなアプリケーションサーバーを使っていても、Webアプリケーションをデプロイするためにやるべきことは、これだけです。ブラウザを使って、「http://<servername>/<warfilename>/app」のURLにアクセスしてください。
(<servername>や<warfilename>は、適切な値に置き換えてください「例:http://localhost:8080/helloworld/app」)


As you can see: no superfluous XML configuration files are needed to enable a Wicket application. Only the markup (HTML) files, the Java class files and the required web.xml were needed to create this application.


余分なXML設定ファイルやWicketアプリケーションでは全く必要ないことが確認できました。
このWebアプリケーションを作るのに必要なのは、マークアップファイル(HTML)とJavaクラスファイル、それからweb.xmlだけなのです。