WebWork2でS2Pagerを使いたい。
以下のように使っています。
Struts+Velocityとかでもちょっと修正すれば使えると思います。
pager.vm
#set ($property = $parameters.get('property')) #set ($pagerHelperCondition = $stack.findValue($property)) #set ($url = $parameters.get('url')) <table border="0" cellpadding="0" cellspacing="0"> <tr> <td width="200" align="left" valign="center"> ${action.getText('lbl.pager.count.1')} ${pagerHelperCondition.count} ${action.getText('lbl.pager.count.2')} #set ($offsetPlusOne = $pagerHelperCondition.offset + 1) ${offsetPlusOne} ${action.getText('lbl.pager.count.3')} ${pagerHelperCondition.currentLastOffset} ${action.getText('lbl.pager.count.4')} </td> <td align="right" valign="center"> #if(${pagerHelperCondition.prev}) <a href="$!{url}?${property}.offset=${pagerHelperCondition.prevOffset}">${action.getText('lbl.pager.prev')}</a> #end #foreach( $index in [0..${pagerHelperCondition.lastPageIndex}] ) #set ($offset = $index * $pagerHelperCondition.limit) #set ($count = $index + 1) #if($index != $pagerHelperCondition.pageIndex) <a href="$!{url}?${property}.offset=${offset}">${count}</a> #end #if($index == $pagerHelperCondition.pageIndex) [${count}] #end #end #if(${pagerHelperCondition.next}) <a href="$!{url}?${property}.offset=${pagerHelperCondition.nextOffset}">${action.getText('lbl.pager.next')}</a></td> #end #if(!$pagerHelperCondition.next) #end </tr> </table>
Pagerを使う場所
・・・・ #bodytag (Component "template=pager.vm") #param ("property" "pagerViewHelper") #param ("url" "FindItem.action") #end ・・・・
メッセージのプロパティファイル
lbl.pager.count.1=全 lbl.pager.count.2=件中 lbl.pager.count.3=件〜 lbl.pager.count.4=件を表示 lbl.pager.prev=< 前へ lbl.pager.next=次へ >
FindItemアクション。
FindItemConditionAwareを使用しているのがポイントです。
public class FindItem extends BaseAction implements FindItemConditionAware { // -------------------------------------------------[IoC Component] private FindItemCondition condition; public void setCondition(FindItemCondition condition) { this.condition = condition; } public FindItemCondition getCondition() { return this.condition; } public PagerViewHelper getPagerViewHelper() { return new PagerViewHelper(getCondition()); } // --------------------------------------------------[DI Component] private ItemService itemService; public void setItemService(ItemService itemService) { this.itemService = itemService; } // -------------------------------------------------[Action Method] /** 検索結果 */ private List result; public List getResult() { return result; } public String execute() throws Exception { result = itemService.findByCondition(condition); return SUCCESS; } }
FindItemConditionAware.java
検索条件DTOをセッションに保持するためのインターフェイス。
S2のSessionスコープでも代用可能かも。
/** * 検索条件用イネーブラーインターフェイス。 * * @author Toshitaka Agata */ public interface FindItemConditionAware { public void setCondition(FindItemCondition condition); }
session jp.co.nulab.webwork.action.FindItemCondition jp.co.nulab.webwork.action.FindItemConditionAware
検索条件DTO
/** * 検索条件。 */ public class FindItemCondition extends DefaultPagerCondition { // ------------------------------------------------[Class Veriable] /** 検索上限のデフォルト値 */ private static final int DEFAULT_LIMIT = 20; // ------------------------------------------------------[Property] /** TABLEアノテーション */ public static final String TABLE = "ITEM"; /** COLUMNアノテーション */ ・・・検索条件のプロパティが続く }