リスト表示とページング
今回は一覧ページの表示とページングに挑戦。まずはHTMLファイルから。
<html> <head> <title>Wicketサンプル - ロケール一覧</title> </head> <body> <table border="1" cellspacing="1" cellpadding="1"> <tr> <th>No</th> <th>コード</th> <th>国名</th> <th>言語</th> </tr> <tr wicket:id="localeList"> <td wicket:id="index"> </td> <td wicket:id="localeCode"> </td> <td wicket:id="localeCountry"> </td> <td wicket:id="localeLanguage"> </td> </tr> <tr> <td align="center" colspan="4" wicket:id="pageNavi"></td> </tr> </table> </body> </html>
次にPageクラス。
/** * ロケール一覧用のページクラスです。 * * @author m-namiki */ public class LocaleListPage extends WebPage { /** 一ページに表示する最大件数です。 */ private static final int maxPageCount = 20; /** * コンストラクタです。 */ public LocaleListPage() { super(); createLocaleListPage(); } @SuppressWarnings("serial") public void createLocaleListPage() { // JVMが使用可能なLocaleを全件取得して、テストデータとします。 List<Locale> locales = Arrays.asList(Locale.getAvailableLocales()); // 一覧部分の設定 final PageableListView localeList = new PageableListView("localeList", locales, maxPageCount) { @Override protected void populateItem(ListItem item) { Locale locale = (Locale) item.getModelObject(); // OGNL式の確認 // TODO 本来はもっと簡単に書ける?(CompaundPropertyModelを利用するらしい) item.add(new Label("index", String.valueOf(item.getIndex() + 1))); item.add(new Label("localeCode", new PropertyModel(locale, "toString"))); item.add(new Label("localeCountry", new PropertyModel(locale, "displayCountry"))); item.add(new Label("localeLanguage", new PropertyModel(locale, "displayLanguage"))); } }; add(localeList); add(new PagingNavigator("pageNavi", localeList)); } }
通常ならばListViewを使うが、ページング用のPageableListViewというのがあるのでこれを使うと良いらしい。 PageableListView#populateItem()が実際の一覧表示処理。ここでは無理やりPropertyModelを使ってるのは、 OGNL式で書けるのを確認したかったから。CompaundPropertyModelというのを使うともっと簡単に書けるらしいが、よく分からなかったので今日はここまで。