<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
		xmlns:xhtml="http://www.w3.org/1999/xhtml"
>

<channel>
	<title>イージーネット Tech Blog &#187; Wicket</title>
	<atom:link href="http://blog.eni.co.jp/tech/category/program/java/wicket/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.eni.co.jp/tech</link>
	<description>株式会社イージーネットのスタッフによる技術系ブログです</description>
	<lastBuildDate>Tue, 12 Jan 2010 01:46:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.eni.co.jp/tech/category/program/java/wicket/feed" />
		<item>
		<title>Wicket小話その3. Wicketで確認メッセージを出すにはどうすればよいのか？(その2)</title>
		<link>http://blog.eni.co.jp/tech/2009/09/wicket_03_confirm02.html</link>
		<comments>http://blog.eni.co.jp/tech/2009/09/wicket_03_confirm02.html#comments</comments>
		<pubDate>Tue, 29 Sep 2009 08:20:46 +0000</pubDate>
		<dc:creator>武輪 恭代</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://blog.eni.co.jp/tech/?p=1812</guid>
		<description><![CDATA[ども、武輪です。
前回のWicket小話で、Confirmメッセージを出すにはどうすればよいのか？と言った話をしたのですが、今回も引き続きConfirmメッセージの話をしたいと思います。
前回、AjaxボタンでConfirmメッセージを表示する方法として、Wicket-extensionのコンポーネントであるModalWindowを使い、独自のポップアップメッセージを表示する方法をとりました。
しかしながら、これってぶっちゃけConfirmメッセージじゃないよね…！
というわけで今回はAjaxボタンで正真正銘(?)のConfirmメッセージを表示する方法をご紹介します。

AjaxボタンでConfirmメッセージ
&#62;Ajaxで中身を入れ替える以上、onclickアトリビュートをいじるのはややこしそうなので&#8230;
と前回書いたのですが、
「confirmで確認後、OKだったら既存AjaxのJavaScriptを実行してあげて、キャンセルだったらfalseを返す」
ようなJavaScriptを書けば問題ないんじゃ…？と思いまして。
そんなわけでさらっとサンプルを書いてみました。
サンプルは前回同様、ボタンを押した時に確認メッセージを表示し、OKだったらカウントする、キャンセルだったら何もしない、といった仕様です。
Confirm2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class Confirm2 extends WebPage&#123;
&#160;
	private int count = 0;
	private Label result;
&#160;
	/**
	 * コンストラクタ
	 */
	public Confirm2&#40;&#41; &#123;
		add&#40;result = new Label&#40;&#34;result&#34;, new PropertyModel&#60;Integer&#62;&#40;this, &#34;count&#34;&#41;&#41;&#41;;
		result.setOutputMarkupId&#40;true&#41;;
		Form&#60;?&#62; form = new Form&#60;Confirm2&#62;&#40;&#34;form&#34;&#41;;
&#160;
		form.add&#40;new AjaxButton&#40;&#34;count&#34;, new Model&#60;String&#62;&#40;&#34;カウントしますか？&#34;&#41;&#41;&#123;
&#160;
			private static final long serialVersionUID = 1L;
&#160;
			@Override
			protected void onSubmit&#40;AjaxRequestTarget target, Form&#60;?&#62; form&#41; &#123;
				count++;
				target.addComponent&#40;result&#41;;
			&#125;
&#160;
			@Override
			protected IAjaxCallDecorator getAjaxCallDecorator&#40;&#41; &#123;
				return new AjaxPreprocessingCallDecorator&#40;super.getAjaxCallDecorator&#40;&#41;&#41; &#123;
					private static final long serialVersionUID = 1L;
&#160;
					@Override
					public CharSequence preDecorateScript&#40;CharSequence [...]]]></description>
			<content:encoded><![CDATA[<p>ども、武輪です。</p>
<p><a href="http://blog.eni.co.jp/tech/2009/09/wicket_02_confirm.html">前回のWicket小話</a>で、Confirmメッセージを出すにはどうすればよいのか？と言った話をしたのですが、今回も引き続きConfirmメッセージの話をしたいと思います。</p>
<p>前回、AjaxボタンでConfirmメッセージを表示する方法として、Wicket-extensionのコンポーネントであるModalWindowを使い、独自のポップアップメッセージを表示する方法をとりました。<br />
しかしながら、これってぶっちゃけConfirmメッセージじゃないよね…！</p>
<p>というわけで今回はAjaxボタンで正真正銘(?)のConfirmメッセージを表示する方法をご紹介します。</p>
<p><!-- more --></p>
<h3>AjaxボタンでConfirmメッセージ</h3>
<p><em>&gt;Ajaxで中身を入れ替える以上、onclickアトリビュートをいじるのはややこしそうなので&#8230;</em></p>
<p>と前回書いたのですが、<br />
「confirmで確認後、OKだったら既存AjaxのJavaScriptを実行してあげて、キャンセルだったらfalseを返す」<br />
ようなJavaScriptを書けば問題ないんじゃ…？と思いまして。</p>
<p>そんなわけでさらっとサンプルを書いてみました。<br />
サンプルは前回同様、ボタンを押した時に確認メッセージを表示し、OKだったらカウントする、キャンセルだったら何もしない、といった仕様です。</p>
<p><span class="strong">Confirm2.java</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p18122"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
</pre></td><td class="code" id="p1812code2"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Confirm2 <span style="color: #000000; font-weight: bold;">extends</span> WebPage<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a> result<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * コンストラクタ
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Confirm2<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		add<span style="color: #009900;">&#40;</span>result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> PropertyModel<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		result.<span style="color: #006633;">setOutputMarkupId</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Form<span style="color: #339933;">&lt;?&gt;</span> form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #339933;">&lt;</span>Confirm2<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		form.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AjaxButton<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;カウントしますか？&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span>AjaxRequestTarget target, Form<span style="color: #339933;">&lt;?&gt;</span> form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				count<span style="color: #339933;">++;</span>
				target.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">protected</span> IAjaxCallDecorator getAjaxCallDecorator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> AjaxPreprocessingCallDecorator<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">getAjaxCallDecorator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
					@Override
					<span style="color: #000000; font-weight: bold;">public</span> CharSequence preDecorateScript<span style="color: #009900;">&#40;</span>CharSequence script<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;if(!confirm('&quot;</span> <span style="color: #339933;">+</span> getModelObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;')) return false;&quot;</span> <span style="color: #339933;">+</span> script<span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		add<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">25～34行目付近</span>:<br />
AjaxButtonのgetAjaxCallDecoratorをオーバライドしてあげることで、出力されるAjax用JavaScriptに手を加えています。</p>
<p>実際に出力されるhtmlは以下のようになります。</p>
<pre lang="Html" line="none">
onclick="if(!confirm('カウントしますか？')) return false;var wcall=wicketSubmitFormById('form2', ';[長いので略]"
</pre>
<h4>まとめ</h4>
<p>というわけで、AjaxButtonでも前回みたいな似非確認ダイアログではなくちゃんとした(?)Confirmメッセージを出すことができました。<br />
AjaxButtonを記述する度にgetAjaxCallDecorator()をオーバライドするのも面倒なので、AjaxButtonを継承したサブクラスを作っておくのが便利そうです。</p>
<h3>動作環境</h3>
<p>今回のサンプルソースは以下の環境で動作させています。</p>
<table class="content">
<tr>
<th></th>
<th>バージョン</th>
</tr>
<tr>
<th style="text-align:left">JDK</th>
<td>1.6.0_13</td>
</tr>
<tr>
<th style="text-align:left">Apache Tomcat</th>
<td>6.0.16</td>
</tr>
<tr>
<th style="text-align:left">Apache Wicket</th>
<td>1.4.0</td>
</tr>
</table>
<h3>あわせて読みたい</h3>
<ul>
<li><a href="http://blog.eni.co.jp/tech/2009/08/wicket_01_start.html">Wicket小話その1. とりあえずWicketを始めよう</a></li>
<li><a href="http://blog.eni.co.jp/tech/2009/08/wicket_02_confirm.html">Wicket小話その2. Wicketで確認メッセージを出すにはどうすればよいのか？</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.eni.co.jp/tech/2009/09/wicket_03_confirm02.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.eni.co.jp/tech/2009/09/wicket_03_confirm02.html" />
	</item>
		<item>
		<title>Wicket小話その2. Wicketで確認メッセージを出すにはどうすればよいのか？</title>
		<link>http://blog.eni.co.jp/tech/2009/09/wicket_02_confirm.html</link>
		<comments>http://blog.eni.co.jp/tech/2009/09/wicket_02_confirm.html#comments</comments>
		<pubDate>Fri, 04 Sep 2009 02:35:23 +0000</pubDate>
		<dc:creator>武輪 恭代</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Wicket]]></category>

		<guid isPermaLink="false">http://blog.eni.co.jp/tech/?p=1365</guid>
		<description><![CDATA[ども、武輪です。
Webアプリケーションでは、データを登録したり編集したり削除したりといった操作が日常茶飯事です。
そして「削除ボタンを押した時にいきなり削除するのではなく、confirmメッセージを出す」というのはよくあるパターンですね。
さてWicket小話その2では、「Wicketで確認メッセージを出すにはどうすればよいのか？」について、実際のコードを踏まえて試してみようと思います。

非Ajaxボタンの場合
例えばこんなボタンがあるとします。

これはcountボタンを押すと画面上に何回クリックしたのかを表示する簡単なサンプルです。
Confirm.html

1
2
3
4
5
6
7
8
9
10
11
12
&#60;?xml version=&#34;1.0&#34; encoding=&#34;UTF-8&#34;?&#62;
&#60;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&#62;
&#60;head&#62;
	&#60;title&#62;Wicket Examples - confirm&#60;/title&#62;
&#60;/head&#62;
&#60;body&#62;
	&#60;span wicket:id=&#34;result&#34; /&#62; 回目
	&#60;form wicket:id=&#34;form&#34;&#62;
		&#60;input wicket:id=&#34;count&#34; type=&#34;submit&#34; value=&#34;count&#34; /&#62;
	&#60;/form&#62;
&#60;/body&#62;
&#60;/html&#62;

Confirm.java(非AjaxButton編)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Confirm extends WebPage &#123;
&#160;
	private int count = 0;
&#160;
	/**
	 * コンストラクタ
	 */
	public Confirm&#40;&#41; &#123;
		add&#40;new Label&#40;&#34;result&#34;, new PropertyModel&#60;Integer&#62;&#40;this, &#34;count&#34;&#41;&#41;&#41;;
		Form&#60;?&#62; form = new Form&#60;Confirm&#62;&#40;&#34;form&#34;&#41;;
		Button countButton;
		form.add&#40;countButton = new Button&#40;&#34;count&#34;&#41; &#123;
			private static final long serialVersionUID = 1L;
&#160;
			@Override
			public void onSubmit&#40;&#41; &#123;
				super.onSubmit&#40;&#41;;
				count++;
			&#125;
&#160;
		&#125;&#41;;
		add&#40;form&#41;;
	&#125;
&#160;
	/**
	 * countを取得します。
	 * @return [...]]]></description>
			<content:encoded><![CDATA[<p>ども、武輪です。</p>
<p>Webアプリケーションでは、データを登録したり編集したり削除したりといった操作が日常茶飯事です。<br />
そして「削除ボタンを押した時にいきなり削除するのではなく、confirmメッセージを出す」というのはよくあるパターンですね。</p>
<p>さてWicket小話その2では、「Wicketで確認メッセージを出すにはどうすればよいのか？」について、実際のコードを踏まえて試してみようと思います。</p>
<p><span id="more-1365"></span></p>
<h3>非Ajaxボタンの場合</h3>
<p>例えばこんなボタンがあるとします。<br />
<img src="http://blog.eni.co.jp/tech/wp-content/uploads/2009/09/sample011-300x214.png" alt="confirmサンプル" title="confirmサンプル" width="300" height="214" class="alignleft size-medium wp-image-1728" /><br clear="all"/><br />
これはcountボタンを押すと画面上に何回クリックしたのかを表示する簡単なサンプルです。</p>
<p><span class="strong">Confirm.html</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136515"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1365code15"><pre class="html" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;title&gt;Wicket Examples - confirm&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;span wicket:id=&quot;result&quot; /&gt; 回目
	&lt;form wicket:id=&quot;form&quot;&gt;
		&lt;input wicket:id=&quot;count&quot; type=&quot;submit&quot; value=&quot;count&quot; /&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><span class="strong">Confirm.java(非AjaxButton編)</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136516"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code" id="p1365code16"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Confirm <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * コンストラクタ
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Confirm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		add<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> PropertyModel<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Form<span style="color: #339933;">&lt;?&gt;</span> form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #339933;">&lt;</span>Confirm<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> countButton<span style="color: #339933;">;</span>
		form.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>countButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onSubmit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				count<span style="color: #339933;">++;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		add<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * countを取得します。
	 * @return count
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h4>html側で対応</h4>
<p>html側に直接JavaScriptを埋め込むことで対応。<br />
Javaのコードには一切変更点はありません。</p>

<div class="wp_codebox"><table width="100%" ><tr id="p136517"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p1365code17"><pre class="html" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;title&gt;Wicket Examples - confirm&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;span wicket:id=&quot;result&quot; /&gt; 回目
	&lt;form wicket:id=&quot;form&quot;&gt;
		&lt;input wicket:id=&quot;count&quot; type=&quot;submit&quot; value=&quot;count&quot; onclick=&quot;return confirm('カウントしますか？')&quot; /&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">9行目</span>:<br />
onclickイベントを追加しただけです。</p>
<h4>Java側で対応</h4>
<p>SimpleAttributeModifierを使うことで、任意のアトリビュートを付加することができます。<br />
htmlには一切変更点はありません。</p>

<div class="wp_codebox"><table width="100%" ><tr id="p136518"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code" id="p1365code18"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Confirm <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * コンストラクタ
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Confirm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		add<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> PropertyModel<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Form<span style="color: #339933;">&lt;?&gt;</span> form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #339933;">&lt;</span>Confirm<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a> countButton<span style="color: #339933;">;</span>
		form.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>countButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Abutton+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Button</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onSubmit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				count<span style="color: #339933;">++;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		countButton.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> SimpleAttributeModifier<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;onclick&quot;</span>, <span style="color: #0000ff;">&quot;return confirm('カウントしますか？')&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		add<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * countを取得します。
	 * @return count
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">22行目</span>:<br />
countButtonにSimpleAttributeModifierをadd。</p>
<p>html側で対応した場合も、Java側で対応した場合も、動作的には下図のように、countボタンをクリックすると確認ダイアログが表示されます。<br />
確認ダイアログのOKをクリックすれば、数字がインクリメントされ、キャンセルをクリックすれば数字は元のままです。<br />
<img src="http://blog.eni.co.jp/tech/wp-content/uploads/2009/09/sample02-300x215.png" alt="Confirmサンプル" title="Confirmサンプル" width="300" height="215" class="alignleft size-medium wp-image-1733" /><br clear="all"/></p>
<p><br clear="all"/></p>
<h3>Ajaxボタンの場合</h3>
<p>続きましてAjaxボタンの場合。<br />
動作的には変わらないのですが、先ほどのサンプルコードを下記のように、ButtonではなくAjaxButtonを使用するように変更します。<br />
html側には変更はありません。(Wicketってこういうとこ便利ですよね)</p>
<p><span class="strong">Confirm.java(AjaxButton編)</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136519"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code" id="p1365code19"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Confirm <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * コンストラクタ
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Confirm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a> result<span style="color: #339933;">;</span>
		add<span style="color: #009900;">&#40;</span>result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> PropertyModel<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		result.<span style="color: #006633;">setOutputMarkupId</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Form<span style="color: #339933;">&lt;?&gt;</span> form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #339933;">&lt;</span>Confirm<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		AjaxButton countButton<span style="color: #339933;">;</span>
		form.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>countButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AjaxButton<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span>AjaxRequestTarget target, Form<span style="color: #339933;">&lt;?&gt;</span> form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				count<span style="color: #339933;">++;</span>
				target.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		add<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * countを取得します。
	 * @return count
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">11行目</span>:<br />
Ajaxを用いてコンポーネントを更新する場合は、setOutputMarkupIdをtrueにすることを忘れずに！</p>
<h4>こんな失敗</h4>
<p>非Ajaxボタンの時と同じように、onclickアトリビュート追加してやればいいんじゃないの？<br />
　↓<br />
html側に「onclick=&#8221;return confirm(&#8217;カウントしますか？&#8217;)&#8221;」をつけたしてみる。<br />
　↓<br />
カウントはされるが、確認ダイアログは出てこない。あれ？<br />
　↓<br />
生成されたhtmlソースを見てみる<br />
　↓</p>

<div class="wp_codebox"><table width="100%" ><tr id="p136520"><td class="code" id="p1365code20"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;submit&quot; value=&quot;count&quot; onclick=&quot;var wcall=wicketSubmitFormById('ida', '?wicket:interface=:5:form:count::IActivePageBehaviorListener:0:&amp;amp;wicket:ignoreIfNotActive=true', 'count' ,null,null, function() {return Wicket.$$(this)&amp;amp;&amp;amp;Wicket.$$('ida')}.bind(this));;; return false;&quot; name=&quot;count&quot; id=&quot;idb&quot;/&gt;</pre></td></tr></table></div>

<p>　↓<br />
Ajax用のonclickになってるよーorz<br />
　↓<br />
じゃあ、SimpleAttributeModifierを使うパターンは？<br />
　↓<br />
確認ダイアログは出るけど、OKクリックしてもカウントアップされない<br />
　↓<br />
生成されたhtmlソースを見てみる<br />
　↓</p>

<div class="wp_codebox"><table width="100%" ><tr id="p136521"><td class="code" id="p1365code21"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;submit&quot; value=&quot;count&quot; name=&quot;count&quot; id=&quot;id3&quot; onclick=&quot;return confirm('カウントしますか？')&quot;/&gt;</pre></td></tr></table></div>

<p>　↓<br />
見事Ajax用のonclickイベントはconfirmに置き換わってる<br />
　↓<br />
そりゃそうだorz</p>
<h4>力技で解決</h4>
<p>Ajaxで中身を入れ替える以上、onclickアトリビュートをいじるのはややこしそうなので、とっても強引に解決してみました。</p>
<p>もう独自にConfirmウィンドウ実装しちゃえばいいんじゃね？<br />
というわけで、できたのがこんな画面。</p>
<p><img src="http://blog.eni.co.jp/tech/wp-content/uploads/2009/09/sample03-300x205.png" alt="Confirmサンプル" title="Confirmサンプル" width="300" height="205" class="alignleft size-medium wp-image-1746" /><br clear="all"/></p>
<p>Wicket-extensionにはいくつかの便利なAjaxコンポーネントが備わっています。<br />
そのうちの一つであるModalWindowを使って、強引に確認ダイアログのようなものを表示させてみました。</p>
<p>追加したのは下記3ファイル(ConfirmModalWindowPage.html、ConfirmModalWindowPage.java、IConfirmPage.java)。</p>
<p><span class="strong">ConfirmModalWondowPage.html</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136522"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code" id="p1365code22"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;meta http-equiv=&quot;content-type&quot; content=&quot;application/xhtml+xml;charset=UTF-8&quot; /&gt;
	&lt;title&gt;ModalWindow&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div wicket:id=&quot;info&quot;&gt;メッセージ&lt;/div&gt;
	&lt;form wicket:id=&quot;confirmForm&quot;&gt;
		&lt;div style=&quot;padding:1em; text-align:center&quot;&gt;
			&lt;input type=&quot;submit&quot; wicket:id=&quot;ok&quot; value=&quot;OK&quot; /&gt;
			&lt;input type=&quot;submit&quot; wicket:id=&quot;cancel&quot; value=&quot;キャンセル&quot; /&gt;
		&lt;/div&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p><span class="strong">ConfirmModalWondowPage.java</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136523"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
</pre></td><td class="code" id="p1365code23"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> ConfirmModalWindowPage <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #009900;">&#123;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">public</span> ConfirmModalWindowPage<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> IConfirmPage page, <span style="color: #000000; font-weight: bold;">final</span> ModalWindow confirmModalWindow, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> info<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		add<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;info&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> Model<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span>info<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Form confirmForm<span style="color: #339933;">;</span>
		add<span style="color: #009900;">&#40;</span>confirmForm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;confirmForm&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// OKボタン</span>
		confirmForm.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AjaxButton<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ok&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span>AjaxRequestTarget target, Form<span style="color: #339933;">&lt;?&gt;</span> form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000066; font-weight: bold;">boolean</span> result <span style="color: #339933;">=</span> page.<span style="color: #006633;">ok</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// 処理に成功した時の動作</span>
					confirmModalWindow.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// 処理に失敗した時の動作</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// キャンセルボタン</span>
		confirmForm.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> AjaxButton<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;cancel&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
			@Override
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span>AjaxRequestTarget target, Form<span style="color: #339933;">&lt;?&gt;</span> form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// キャンセルボタンが押されたときの処理</span>
				confirmModalWindow.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 確認用ModalWindowを取得する.
	 * @param id
	 * @param page
	 * @return
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> ModalWindow getConfirmModalWindow<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> id, <span style="color: #000000; font-weight: bold;">final</span> IConfirmPage page, <span style="color: #000000; font-weight: bold;">final</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> info<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> ModalWindow modal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModalWindow<span style="color: #009900;">&#40;</span>id<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setCssClassName</span><span style="color: #009900;">&#40;</span>ModalWindow.<span style="color: #006633;">CSS_CLASS_GRAY</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setInitialHeight</span><span style="color: #009900;">&#40;</span>150<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setInitialWidth</span><span style="color: #009900;">&#40;</span>350<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setResizable</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;確認メッセージ&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setCookieName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;confirm_window&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modal.<span style="color: #006633;">setPageCreator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ModalWindow.<span style="color: #006633;">PageCreator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">public</span> Page createPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> ConfirmModalWindowPage<span style="color: #009900;">&#40;</span>page, modal, info<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		modal.<span style="color: #006633;">setWindowClosedCallback</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ModalWindow.<span style="color: #006633;">WindowClosedCallback</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onClose<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				page.<span style="color: #006633;">refleshComponent</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		modal.<span style="color: #006633;">setCloseButtonCallback</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ModalWindow.<span style="color: #006633;">CloseButtonCallback</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onCloseButtonClicked<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> modal<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">11～23行目</span>:<br />
ConfirmウィンドウのOKボタンの定義です。実際の処理は、元のPageのokメソッドに記述しています。<br />
処理の成功/失敗を返すようにしておくと、処理に失敗した場合はConfirmウィンドウ内にエラーメッセージを表示するなんて使い方もできそうです。</p>
<p><span class="blue">26～33行目</span>:<br />
Confirmウィンドウのキャンセルボタンの定義です。Confirmウィンドウを閉じているだけです。</p>
<p><span class="blue">43～73行目</span>:<br />
Confirmウィンドウ用のModalWindowを取得するメソッドです。元のPageから取得するためstaticメソッドにしています。</p>
<p><span class="blue">45～51行目</span>:<br />
ModalWindowの設定です。<br />
　setCssClassName　→　CSS_CLASS_GRAYかCSS_CLASS_BLUEを選択。デフォルトでは青色。<br />
　setInitialHeight、setInitialWidth　→　ModalWindowのサイズ指定。確認ダイアログなので小さめにしておきます。<br />
　setResizable　→　ModalWindowのリサイズを可能にするかしないか。<br />
　setTitle　→　ModalWindowのタイトル文字列。<br />
　setCookieName　→　クッキーの名前。このクッキーはModalWindowの位置などを記憶しています。</p>
<p><span class="blue">58～63行目</span>:<br />
ModalWindowが閉じられたときの処理を記述します。<br />
ここでは元画面のコンポーネントの更新を行っています。</p>
<p><span class="blue">65～70行目</span>:<br />
ModalWindowの×ボタンで閉じられたときの処理を記述します。<br />
ここでは特に何も行っていません。</p>
<p><span class="strong">IConfirmPage.java</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136524"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code" id="p1365code24"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> IConfirmPage <span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 画面の更新
	 * @param target
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> refleshComponent<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * OKボタンを押した時の処理
	 * @param target
	 * @return 処理が成功したかどうか true:成功 false:失敗
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> ok<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * キャンセルボタンを押した時の処理
	 * @param target
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> cancel<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>【メモ】<br />
Confirmウィンドウを表示するPageでこのインターフェースを実装します。</p>
<p>このインターフェースを実装するようにConfirm.javaを以下のように修正しました。<br />
<span class="strong">Confirm.java(修正後)</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136525"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
</pre></td><td class="code" id="p1365code25"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Confirm <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #000000; font-weight: bold;">implements</span> IConfirmPage<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a> result<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * コンストラクタ
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> Confirm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		add<span style="color: #009900;">&#40;</span>result <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Alabel+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Label</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;result&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> PropertyModel<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		result.<span style="color: #006633;">setOutputMarkupId</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Form<span style="color: #339933;">&lt;?&gt;</span> form <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Form<span style="color: #339933;">&lt;</span>Confirm<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #000000; font-weight: bold;">final</span> ModalWindow confirmModalWindow <span style="color: #339933;">=</span> ConfirmModalWindowPage.<span style="color: #006633;">getConfirmModalWindow</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;confirmModalWindow&quot;</span>,
				Confirm.<span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #0000ff;">&quot;カウントしますか？&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		form.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>confirmModalWindow<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		AjaxButton countButton<span style="color: #339933;">;</span>
		form.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>countButton <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AjaxButton<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;count&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
&nbsp;
			@Override
			<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> onSubmit<span style="color: #009900;">&#40;</span>AjaxRequestTarget target, Form<span style="color: #339933;">&lt;?&gt;</span> form<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				confirmModalWindow.<span style="color: #006633;">show</span><span style="color: #009900;">&#40;</span>target<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		add<span style="color: #009900;">&#40;</span>form<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * countを取得します。
	 * @return count
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getCount<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> count<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Confirmウィンドウでキャンセルが押されたときの処理
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> cancel<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// キャンセル時の処理</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * ConfirmウィンドウでOKが押されたときの処理
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> ok<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		count<span style="color: #339933;">++;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * 画面上のコンポーネントの更新
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> refleshComponent<span style="color: #009900;">&#40;</span>AjaxRequestTarget target<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		target.<span style="color: #006633;">addComponent</span><span style="color: #009900;">&#40;</span>result<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">14～16行目</span>:<br />
Confirmウィンドウ用のModalWindowを取得します。</p>
<p><span class="blue">23～25行目</span>:<br />
countボタンがクリックされたときの処理。ModalWindowを表示します。</p>
<p><span class="blue">40～45行目</span>:<br />
Confirmウィンドウでキャンセルボタンを押されたときの実処理を記述します。ここでは特に何もしていません。</p>
<p><span class="blue">50～53行目</span>:<br />
ConfirmウィンドでOKボタンが押されたときの実処理を記述します。ここではcountをインクリメントしています。</p>
<p><span class="blue">58～60行目</span>:<br />
Ajaxで画面の更新を行います。ここではresultラベルの表示を更新しています。</p>
<p><span class="strong">Confirm.html(変更後)</span></p>

<div class="wp_codebox"><table width="100%" ><tr id="p136526"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code" id="p1365code26"><pre class="html" style="font-family:monospace;">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;title&gt;Wicket Examples - confirm&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;span wicket:id=&quot;result&quot; /&gt; 回目
	&lt;form wicket:id=&quot;form&quot;&gt;
		&lt;input wicket:id=&quot;count&quot; type=&quot;submit&quot; value=&quot;count&quot;/&gt;
		&lt;div wicket:id=&quot;confirmModalWindow&quot;&gt;削除ConfirmModalWindow&lt;/div&gt;
	&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></td></tr></table></div>

<p>【メモ】<br />
<span class="blue">10行目</span>:<br />
Confirmウィンドウ表示領域の確保。</p>
<h4>まとめ</h4>
<p>とりあえずこんな感じの実装で、AjaxButtonの場合でも確認ダイアログっぽいものは出すことができました。</p>
<p>もっともこのままだと、1つの画面でOK、キャンセルが押されたときの処理が1つしか記述できないので、実際汎用的に使おうと思えば、さらなる改造が必要ですけどね:）</p>
<h3>動作環境</h3>
<p>今回のサンプルソースは以下の環境で動作させています。</p>
<table class="content">
<tr>
<th></th>
<th>バージョン</th>
</tr>
<tr>
<th style="text-align:left">JDK</th>
<td>1.6.0_13</td>
</tr>
<tr>
<th style="text-align:left">Apache Tomcat</th>
<td>6.0.16</td>
</tr>
<tr>
<th style="text-align:left">Apache Wicket</th>
<td>1.4.0</td>
</tr>
</table>
<h3>あわせて読みたい</h3>
<ul>
<li><a href="http://blog.eni.co.jp/tech/2009/08/wicket_01_start.html">Wicket小話その1. とりあえずWicketを始めよう</a></li>
<li><a href="http://blog.eni.co.jp/tech/2009/09/wicket_03_confirm02.html">Wicket小話その3. Wicketで確認メッセージを出すにはどうすればよいのか？(その2) </a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.eni.co.jp/tech/2009/09/wicket_02_confirm.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.eni.co.jp/tech/2009/09/wicket_02_confirm.html" />
	</item>
		<item>
		<title>Wicket小話その1. とりあえずWicketを始めよう</title>
		<link>http://blog.eni.co.jp/tech/2009/08/wicket_01_start.html</link>
		<comments>http://blog.eni.co.jp/tech/2009/08/wicket_01_start.html#comments</comments>
		<pubDate>Mon, 31 Aug 2009 08:35:02 +0000</pubDate>
		<dc:creator>武輪 恭代</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Wicket]]></category>
		<category><![CDATA[フレームワーク]]></category>

		<guid isPermaLink="false">http://blog.eni.co.jp/tech/?p=1108</guid>
		<description><![CDATA[ども、武輪です。
Wicket小話と題しまして、Wicket初心者の私がWicketを使ってみて、

これちょっと便利な機能だなー
こんなコンポーネントを使ってみたよ！
こんな実装やってみたよ！

などなど。
そんなちょっとしたメモ書きを徒然と残していこうと思います。
基本的に情報はWebと書籍「オープンソース徹底活用 WicketによるWebアプリケーション開発」(矢野勉さん著)から得ています。
あとはWicketのソースとか。
さてWicket小話その1として、「とりあえずWicketを始めよう」という副題で徒然と書いていきます。

はじめに
そもそもWicketって何よって方に簡単に。
Wicketとは、Webアプリケーションフレームワークの1つです。
HtmlとJavaでWebアプリケーションが作れちゃいます。
設定ファイルはほとんど必要ありません。(そりゃweb.xmlは必須ですけども)
2009年8月現在Wicketは1.4がリリースされています。
Wicket小話では特に注意書きがない限り、この最新リリースのWicket1.4で試したことを書いていこうと思います。
開発環境などは以下の通り。



バージョン


JDK
1.6.0_13


Apache Tomcat
6.0.16


Apache Wicket
1.4.0


さてここまで書いたところで、ショックな出来事が1つ。
ちょ…、Wicket1.4.1もう出てるじゃんorz
ま、まぁいっか。
準備
Apache Maven2 のインストール手順
Maven2を使うことで簡単にWicketプロジェクトを開始することができます。
(事前に1.5以上のJDKがインストールされている前提で話を進めていきます。)

1. ダウンロード
Apache Maven Projectより、アーカイブをダウンロードします。
私は2.1.0を使用していますが、2.2.1が既に出ているようです。


2. 環境変数の設定
適当なディレクトリ(MAVEN_HOME)にダウンロードしてきたアーカイブを展開してください。
環境変数PATHに%MAVEN_HOME%binを追加。
(環境変数はシステムのプロパティ→詳細設定→環境変数から変更できます。既存のPATHの値を消してしまわないように気を付けてください。)
※JAVA_HOMEを設定していないとMaven2は動かないので注意。


3. インストール確認
コマンドプロンプトでmvn &#8211;versionを実行することでインストールを確認することができます。

C:>mvn --version
Apache Maven 2.1.0 (r755702; 2009-03-19 04:10:27+0900)
Java version: 1.6.0_06
Java home: C:javajdk1.6.0_06jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"


早速Wicketを始めよう
Moven2のインストールが完了したら、早速Wicketを開始してみましょう。
サンプルアプリケーションを動かしてみよう！

1. ダウンロード
http://wicket.apache.org/ から1.4系のアーカイブをダウンロードしてきます。
適当なディレクトリでアーカイブを展開します。
libディレクトリにはwicketの各種ライブラリーの他に、サンプルアプリケーションのwar(wicket-examples-1.4.x.war)も入っています。
このwarファイルをローカルのTomcatのwebappディレクトリに放り込んでTomcatを再起動してあげれば、とりあえずサンプルを動かすことができるんですが、このwarにはソースが含まれていないので動作を見るだけになってしまいます。
折角なのでEclipseで作業できるようにしましょう。


2. Eclipseにインポート
コマンドプロンプト上で、srcディレクトリ以下のwicket-examplesに移動します。
mvn eclipse:eclipse -DdownloadSources=true を実行することでEclipse用の設定ファイルが生成されます。

%展開先のディレクトリ%apache-wicket-1.4.xsrcwicket-examples>mvn eclipse:eclipse -DdownloadSources=true

あとはEclipseで「インポート」→「既存プロジェクトをワークスペースへ」からインポートすればOK。


3. サンプルを動かしてみよう
wicket-examplesパッケージにはJettyというJAVAのWebサーバが同梱されているので、簡単にサンプルを動作させることができます。
org.apache.wicket.examples.StartExamplesクラスを実行すればJettyサーバが起動します。
そしたらあとは http://localhost:8080/wicket-examples/ にアクセスするだけでWicketのサンプルの数々を試すことができます。
勿論Tomcatで動かしても全然問題ありません。

新規プロジェクトのスタート

サンプルアプリケーションは、既に1つの完成されたアプリケーションです。
新規プロジェクトをスタートさせる時には、ブランクのプロジェクトがあれば便利だなーって思いませんか？
というわけでブランクプロジェクトの作り方です。

Maven2の使い方なんかわかんないよーって人にも簡単！
WicketのQuickStartのページからコマンドラインを作成することができます。
上記サイトにアクセスすると、こんな入力欄があります。
各入力欄の意味は、
　GroupId:　パッケージとして使用される
　ArtifactId:　プロジェクト名として使用される
　Version:　Wicketのバージョン
こんな感じです。
これらを入力していくと、勝手にCommand Line欄にコマンドが組み立てられていくって寸法です。なんて便利！
次にコマンドプロンプト上でプロジェクトを作成したいディレクトリに移動し、先ほど生成したコマンドラインをコピペして実行します。
(※Proxyの設定などはこちらを参考に。→Maven2のTipsを集めるwiki#固有の環境設定)

C:test>mvn archetype:create -DarchetypeGroupId=org.apache.wicket -DarchetypeArtifactId=wicket-archetype-quickstart -DarchetypeVersion=1.4.1 -DgroupId=jp.co.eni.wicket -DartifactId=sample
[INFO] Scanning [...]]]></description>
			<content:encoded><![CDATA[<p>ども、武輪です。</p>
<p>Wicket小話と題しまして、Wicket初心者の私がWicketを使ってみて、</p>
<ul>
<li>これちょっと便利な機能だなー</li>
<li>こんなコンポーネントを使ってみたよ！</li>
<li>こんな実装やってみたよ！</li>
</ul>
<p>などなど。<br />
そんなちょっとしたメモ書きを徒然と残していこうと思います。</p>
<p>基本的に情報はWebと書籍「<a href="http://www.amazon.co.jp/exec/obidos/ASIN/4798022217/enieni-22/ref=nosim/" name="amazletlink" target="_blank">オープンソース徹底活用 WicketによるWebアプリケーション開発</a>」(矢野勉さん著)から得ています。<br />
あとはWicketのソースとか。</p>
<p>さてWicket小話その1として、「とりあえずWicketを始めよう」という副題で徒然と書いていきます。</p>
<p><span id="more-1108"></span></p>
<h3>はじめに</h3>
<p>そもそもWicketって何よって方に簡単に。<br />
<b>Wicketとは、Webアプリケーションフレームワークの1つです。</b><br />
HtmlとJavaでWebアプリケーションが作れちゃいます。<br />
設定ファイルはほとんど必要ありません。(そりゃweb.xmlは必須ですけども)</p>
<p>2009年8月現在Wicketは1.4がリリースされています。<br />
Wicket小話では特に注意書きがない限り、この最新リリースのWicket1.4で試したことを書いていこうと思います。</p>
<p>開発環境などは以下の通り。</p>
<table class="content">
<tr>
<th></th>
<th>バージョン</th>
</tr>
<tr>
<th style="text-align:left">JDK</th>
<td>1.6.0_13</td>
</tr>
<tr>
<th style="text-align:left">Apache Tomcat</th>
<td>6.0.16</td>
</tr>
<tr>
<th style="text-align:left">Apache Wicket</th>
<td>1.4.0</td>
</tr>
</table>
<p>さてここまで書いたところで、ショックな出来事が1つ。<br />
ちょ…、Wicket1.4.1もう出てるじゃんorz</p>
<p>ま、まぁいっか。</p>
<h3>準備</h3>
<h4>Apache Maven2 のインストール手順</h4>
<p>Maven2を使うことで簡単にWicketプロジェクトを開始することができます。<br />
(事前に1.5以上のJDKがインストールされている前提で話を進めていきます。)</p>
<p>
<span class="entry">1. ダウンロード</span><br />
<a href="http://maven.apache.org/download.html">Apache Maven Project</a>より、アーカイブをダウンロードします。<br />
私は2.1.0を使用していますが、2.2.1が既に出ているようです。
</p>
<p>
<span class="entry">2. 環境変数の設定</span><br />
適当なディレクトリ(MAVEN_HOME)にダウンロードしてきたアーカイブを展開してください。<br />
環境変数PATHに%MAVEN_HOME%binを追加。<br />
(環境変数はシステムのプロパティ→詳細設定→環境変数から変更できます。既存のPATHの値を消してしまわないように気を付けてください。)</p>
<p>※JAVA_HOMEを設定していないとMaven2は動かないので注意。
</p>
<p>
<span class="entry">3. インストール確認</span><br />
コマンドプロンプトでmvn &#8211;versionを実行することでインストールを確認することができます。</p>
<pre style="background-color:black; color:white">
C:>mvn --version
Apache Maven 2.1.0 (r755702; 2009-03-19 04:10:27+0900)
Java version: 1.6.0_06
Java home: C:javajdk1.6.0_06jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows xp" version: "5.1" arch: "x86" Family: "windows"
</pre>
</p>
<h3>早速Wicketを始めよう</h3>
<p>Moven2のインストールが完了したら、早速Wicketを開始してみましょう。</p>
<h4>サンプルアプリケーションを動かしてみよう！</h4>
<p>
<span class="entry">1. ダウンロード</span><br />
<a href="http://wicket.apache.org/">http://wicket.apache.org/</a> から1.4系のアーカイブをダウンロードしてきます。</p>
<p>適当なディレクトリでアーカイブを展開します。<br />
libディレクトリにはwicketの各種ライブラリーの他に、サンプルアプリケーションのwar(wicket-examples-1.4.x.war)も入っています。<br />
このwarファイルをローカルのTomcatのwebappディレクトリに放り込んでTomcatを再起動してあげれば、とりあえずサンプルを動かすことができるんですが、このwarにはソースが含まれていないので動作を見るだけになってしまいます。<br />
折角なのでEclipseで作業できるようにしましょう。
</p>
<p>
<span class="entry">2. Eclipseにインポート</span><br />
コマンドプロンプト上で、srcディレクトリ以下のwicket-examplesに移動します。<br />
mvn eclipse:eclipse -DdownloadSources=true を実行することでEclipse用の設定ファイルが生成されます。</p>
<pre style="background-color:black; color:white">
%展開先のディレクトリ%apache-wicket-1.4.xsrcwicket-examples>mvn eclipse:eclipse -DdownloadSources=true
</pre>
<p>あとはEclipseで「インポート」→「既存プロジェクトをワークスペースへ」からインポートすればOK。
</p>
<p>
<span class="entry">3. サンプルを動かしてみよう</span><br />
wicket-examplesパッケージにはJettyというJAVAのWebサーバが同梱されているので、簡単にサンプルを動作させることができます。<br />
org.apache.wicket.examples.StartExamplesクラスを実行すればJettyサーバが起動します。<br />
そしたらあとは <a href="http://localhost:8080/wicket-examples/">http://localhost:8080/wicket-examples/</a> にアクセスするだけでWicketのサンプルの数々を試すことができます。<br />
勿論Tomcatで動かしても全然問題ありません。
</p>
<h4>新規プロジェクトのスタート</h4>
<p>
サンプルアプリケーションは、既に1つの完成されたアプリケーションです。<br />
新規プロジェクトをスタートさせる時には、ブランクのプロジェクトがあれば便利だなーって思いませんか？<br />
というわけでブランクプロジェクトの作り方です。
</p>
<p>Maven2の使い方なんかわかんないよーって人にも簡単！<br />
<a href="http://wicket.apache.org/quickstart.html">WicketのQuickStartのページ</a>からコマンドラインを作成することができます。</p>
<p>上記サイトにアクセスすると、こんな入力欄があります。<br />
<div id="attachment_1323" class="wp-caption aligncenter" style="width: 645px"><img src="http://blog.eni.co.jp/tech/wp-content/uploads/2009/08/quickstart.png" alt="quickstart" title="quickstart" width="635" height="283" class="aligncenter size-full wp-image-1323" /><p class="wp-caption-text">QuickStartページ</p></div></p>
<p>各入力欄の意味は、<br />
　GroupId:　パッケージとして使用される<br />
　ArtifactId:　プロジェクト名として使用される<br />
　Version:　Wicketのバージョン<br />
こんな感じです。<br />
これらを入力していくと、勝手にCommand Line欄にコマンドが組み立てられていくって寸法です。なんて便利！</p>
<p>次にコマンドプロンプト上でプロジェクトを作成したいディレクトリに移動し、先ほど生成したコマンドラインをコピペして実行します。<br />
(※Proxyの設定などはこちらを参考に。→<a href="http://wiki.fdiary.net/maven2/?CookBook#l41">Maven2のTipsを集めるwiki#固有の環境設定</a>)</p>
<pre class="wp_codebox" style="background-color:black; color:white">
C:test>mvn archetype:create -DarchetypeGroupId=org.apache.wicket -DarchetypeArtifactId=wicket-archetype-quickstart -DarchetypeVersion=1.4.1 -DgroupId=jp.co.eni.wicket -DartifactId=sample
[INFO] Scanning for projects...
[INFO] Searching repository for plugin with prefix: 'archetype'.
[INFO] ------------------------------------------------------------------------
[INFO] Building Maven Default Project
[INFO]    task-segment: [archetype:create] (aggregator-style)
[INFO] ------------------------------------------------------------------------
[INFO] Setting property: classpath.resource.loader.class => 'org.codehaus.plexus.velocity.ContextClassLoaderResourceLoader'.
[INFO] Setting property: velocimacro.messages.on => 'false'.
[INFO] Setting property: resource.loader => 'classpath'.
[INFO] Setting property: resource.manager.logwhenfound => 'false'.
[INFO] [archetype:create]
[WARNING] This goal is deprecated. Please use mvn archetype:generate instead
[INFO] Defaulting package to group ID: jp.co.eni.wicket
Downloading: http://repo1.maven.org/maven2/org/apache/wicket/wicket-archetype-quickstart/1.4.1/wicket-archetype-quickstart-1.4.1.jar
13K downloaded  (wicket-archetype-quickstart-1.4.1.jar)
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating OldArchetype: wicket-archetype-quickstart:1.4.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: jp.co.eni.wicket
[INFO] Parameter: packageName, Value: jp.co.eni.wicket
[INFO] Parameter: package, Value: jp.co.eni.wicket
[INFO] Parameter: artifactId, Value: sample
[INFO] Parameter: basedir, Value: C:test
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] ********************* End of debug info from resources from generated POM ***********************
[INFO] OldArchetype created in dir: C:testsample
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9 seconds
[INFO] Finished at: Wed Aug 26 11:11:43 JST 2009
[INFO] Final Memory: 7M/14M
[INFO] ------------------------------------------------------------------------
</pre>
<p>最後に「<b>[INFO] BUILD SUCCESSFUL</b>」が出てればOK。</p>
<p>
さて、実はもうこれだけで実行可能な状態なんです。<br />
上記例では、C:testにsampleというディレクトリが作成されています。<br />
コマンドプロンプト上でこのsampleディレクトリに移動し、以下のコマンドを実行します。</p>
<pre style="background-color:black; color:white">
C:testsample>mvn jetty:run
</pre>
<p>http://localhost:8080/sample にアクセスしてみてください。</p>
<pre>
Wicket Quickstart Archetype Homepage 

If you see this message wicket is properly configured and running
</pre>
<p>と表示されていれば成功です。
</p>
<p>
さらにEclipseで開発するには、sampleディレクトリ上で</p>
<pre style="background-color:black; color:white">
C:testsample>mvn eclipse:eclipse -DdownloadSources=true
</pre>
<p>を実行してEclipse用の設定ファイルが生成して、Eclipseで既存プロジェクトのインポートをすればOKです。
</p>
<h3>リンク集</h3>
<p><a href="http://wicket.apache.org/">Apache Wicket</a><br />
Apache Wicketの本家サイト</p>
<p><a href="http://www.wicket-ja.org/">Wicket-ja</a><br />
日本Wicketユーザーグループ(wicket-ja)の公式サイト。<br />
過去のメーリングリストの内容などが参照できます。<br />
ちなみに「うぃけっと　じぇーえー」ではなく「うぃけっとじゃ」と読むらしいです。</p>
<p><a href="http://d.hatena.ne.jp/t_yano/">矢野勉のはてな日記</a><br />
Wicket-ja創設者のブログ。<br />
著書の「<a href="http://www.amazon.co.jp/exec/obidos/ASIN/4798022217/enieni-22/ref=nosim/" name="amazletlink" target="_blank">オープンソース徹底活用 WicketによるWebアプリケーション開発</a>」には大変お世話になっております。</p>
<p><a href="http://wicket-study.seesaa.net/">Wicket学習帳</a><br />
最終更新が2008年08月19日で止まっていて、Ｗｉｃｋｅｔ1.3ベースまでの話が多いですが、参考になります。</p>
<h3>最後に</h3>
<p>さて、「とりあえずWicketを始めよう」という目的は達成できたでしょうか。<br />
次回以降のWicket小話では、具体的なコンポーネントの使い方とかを載せていければ、と思っております。</p>
<p>それでは、また。</p>
<h3>あわせて読みたい</h3>
<ul>
<li><a href="http://blog.eni.co.jp/tech/2009/09/wicket_02_confirm.html">Wicket小話その2. Wicketで確認メッセージを出すにはどうすればよいのか？</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.eni.co.jp/tech/2009/08/wicket_01_start.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<xhtml:link rel="alternate" media="handheld" type="text/html" href="http://blog.eni.co.jp/tech/2009/08/wicket_01_start.html" />
	</item>
	</channel>
</rss>
