struts 2の汎用ページングツールバー


JAvaコンポーネント

package com.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;

import com.opensymphony.xwork2.util.ValueStack;

public class Pagebar extends UIBean {

	protected String totalRecord;
	protected String pageSize;
	protected String pageNo;
	protected String url;
	protected String style;

	public String getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(String totalRecord) {
		this.totalRecord = totalRecord;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public Pagebar(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		super(stack, request, response);
	}

	@Override
	protected String getDefaultTemplate() {
		return "pagebar_" + style.toLowerCase();
	}

	@Override
	protected void evaluateExtraParams() {
		super.evaluateExtraParams();
		addParameter("totalRecord", findString(totalRecord));
		addParameter("pageSize", findString(pageSize));
		//          ,      
		String pno = findString(pageNo);
		if (pno == null || !pno.matches("^[1-9]+[0-9]*$"))
			pno = "0";
		addParameter("pageNo", pno);
		addParameter("url", findString(url));
		addParameter("style", style);
	}

}

package com.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.Component;
import org.apache.struts2.views.jsp.ui.AbstractUITag;

import com.opensymphony.xwork2.util.ValueStack;

public class PagebarTag extends AbstractUITag {

	private static final long serialVersionUID = 1L;
	protected String totalRecord;
	protected String pageSize;
	protected String pageNo;
	protected String url;
	protected String style = "basic";

	@Override
	public Component getBean(ValueStack stack, HttpServletRequest request,
			HttpServletResponse response) {
		return new Pagebar(stack, request, response);
	}

	protected void populateParams() {
		super.populateParams();
		Pagebar pagebar = (Pagebar) component;
		pagebar.setTotalRecord(totalRecord);
		pagebar.setPageSize(pageSize);
		pagebar.setPageNo(pageNo);
		pagebar.setUrl(url);
		pagebar.setStyle(style);
	}

	public String getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(String totalRecord) {
		this.totalRecord = totalRecord;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getPageNo() {
		return pageNo;
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

}

ラベルプロファイル、/WebRoot/WEB-INF/の下に配置し、struts-extendと命名する.tld

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>l</short-name>
	<uri>/lowca-tags</uri>
	<display-name>    struts  </display-name>
	
	<tag>
		<name>pagebar</name>
		<tag-class>com.tag.PagebarTag</tag-class>
		<body-content>jsp</body-content>
		<description>     </description>
		<attribute>
			<name>totalRecord</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>   </description>
		</attribute>		
		<attribute>
			<name>pageSize</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>       </description>
		</attribute>		
		<attribute>
			<name>pageNo</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>int</type>
			<description>  </description>
		</attribute>		
		<attribute>
			<name>url</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
			<description>url  </description>
		</attribute>		
		<attribute>
			<name>style</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
			<type>java.lang.String</type>
			<description>    </description>
		</attribute>
	</tag>
</taglib>

テンプレートファイルは、src/template/現在のstruts 2トピック/ディレクトリの下に、私のはsrc/template/simple/下に配置されます.pagebarと命名basic.ftlは、pagebarを書くなど、他のトピックを自分で書いてこのページングラベルを拡張することもできます.ajax.ftlはajax方式のページングをサポートします.

<#--           -->
<#assign url=parameters.url />
<#assign pageSize=parameters.pageSize?number />
<#assign totalRecord=parameters.totalRecord?number />
<#if (totalRecord%pageSize=0)>
	<#assign totalPage=totalRecord/pageSize />
<#else>
	<#assign totalPage=totalRecord/pageSize+1 />
</#if>
<#assign pageNo=parameters.pageNo?number />
<#if (pageNo<1)>
	<#assign pageNo=1 />
</#if>
<#if (pageNo>totalPage)>
	<#assign pageNo=totalPage />
</#if>
<#--      -->
<#if (totalRecord>0) && (totalPage>1) && (pageNo>0) && (pageNo<=totalPage)>
	<#--           -->
	<#if (pageNo<=5)>
		<#assign leftNo=1 />   
		<#if (totalPage<10)>
			<#assign rightNo=totalPage />
		<#else>
			<#assign rightNo=10 />
		</#if>
	<#elseif (pageNo>5) && (pageNo<=totalPage-5)>
		<#if (pageNo-4>0)>
			<#assign leftNo=pageNo-4 />
		<#else>
			<#assign leftNo=1 />
		</#if>
		<#if (pageNo+5<=totalPage)>
			<#assign rightNo=pageNo+5 />
		<#else>
			<#assign rightNo=totalPage />
		</#if>
	<#else>
		<#if (totalPage-10+1>0)>
			<#assign leftNo=totalPage-10+1 />
		<#else>
			<#assign leftNo=1 />
		</#if>
		<#assign rightNo=totalPage />
	</#if>
	<#--    -->
	<#lt/><!--================       ================-->
	<style>
	/*page*/
	.pagebar_warpper li { display:inline; margin:0 1px; left center no-repeat;}
	.pagebar_warpper{ width:auto; margin:0 auto; height:22px; line-height:22px; text-align:center; margin-top:20px;}
	.pagebar_desc,.pagebar_plist,.pagebar_gp{ float:left;}
	.pagebar_plist a{ border:1px solid #ccc; padding:5px; margin-right:3px; background:#fff; text-decoration:none;}
	.pagebar_plist a.pagebar_curr{ background:#72b003; color:#fff; border:1px solid #72b003;}
	.green{ font-weight:bold; margin-right:5px; color:#75bf0d;}
	.pagebar_gp_textbox{ width:30px; height:18px; border:1px solid #ccc; margin-right:1px;}
	.pagebar_gp_button{ background-color:#75bf0d; color:#fff; padding:2px; text-align:center; cursor:pointer; font-weight:bold;}
	</style>
	<div class="pagebar_warpper">
		<ul class="pagebar_ul">
			<li class="pagebar_desc">   <span class="pagebar_desc_num">${totalRecord?c}</span>&nbsp;&nbsp;   <span class="pagebar_desc_num">${totalPage?c}</span></li>
			<li class="pagebar_plist">
				<a href="<@getURL text=url page=1 />">  </a>
				<#if (pageNo-1>=1)>
				<a href="<@getURL text=url page=pageNo-1 />">   </a>
				</#if>
				<#list leftNo..rightNo as p>   
        			<#if (p==pageNo)>   
            			<a class="pagebar_curr" href="#">${p}</a>
        			<#else>   
            			<a href="<@getURL text=url page=p />">${p}</a>
        			</#if>
    			</#list> 
    			<#if (pageNo+1<=totalPage)>
				<a href="<@getURL text=url page=pageNo+1 />">   </a>
				</#if>
				<a href="<@getURL text=url page=totalPage />">  </a>
			</li>
			<li class="pagebar_gp"><input type="text" value="" class="pagebar_gp_textbox" /><input type="button" value="go" class="pagebar_gp_button" onclick="pagebar_gotoPage(this)"/></li>
		</ul>
	</div>
	<script type="text/javascript">
		function pagebar_gotoPage(btn) {
			if(!btn || !btn.previousSibling)
				return;
			var pno = btn.previousSibling.value;
			if(!pno || !/^[1-9]+[0-9]*$/.test(pno))
				pno = 1;
			else
				pno = parseInt(pno);
			if(pno>${totalPage})
				pno = ${totalPage};
			var currUrl = window.location.href;
			if(!currUrl)
				return;
			var pos = currUrl.indexOf("?");
			if(pos<=0) {
				var forwardUrl = currUrl+"?pageNo="+pno;
				window.location.href = forwardUrl;
				return;
			}
			var params = currUrl.substring(pos+1);
			var realUrl = currUrl.substring(0,pos);
			var paramArray = params.split("&");
			for(var i=0;i<paramArray.length;i++) {
				if(/^pageNo=.*$/i.test(paramArray[i])) {
					paramArray[i] = "pageNo="+pno;
					break;
				}
			}
			window.location.href = realUrl+"?"+paramArray.join("&");
		}
	</script>
	<!--================       ================--><#rt/>
</#if>

<#--    URL  -->
<#macro getURL text page>   
    <#if (text?index_of("?")>0)>   
        <#lt/>${text?replace("?","?pageNo="+page)}<#rt/>
    <#else>   
        <#lt/>${text}?pageNo=${page}<#rt/>
    </#if>      
</#macro>

このラベルはJSPで参照され、pageNoはページングパラメータの名前であり、pageSizeはページごとに表示されるエントリ数であり、totalRecordは合計レコード数であり、これらのパラメータはactionから伝達される.また、どのテンプレートを使用するかを示すオプションのパラメータstyleも受け入れられます.styleはデフォルトでpagebar_を使用します.basic.ftlというテンプレート.

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="l" uri="/lowca-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE-EmulateIE7" />
	<title>       </title>
</head>
<body>
	<l:pagebar pageNo="${param.pageNo}" url="${webroot }/test.jsp" pageSize="10" totalRecord="4000"></l:pagebar>
</body>
</html>

欠点と不足:
ジャンプ機能が不足しており、1ページに複数のページングツールバーがある場合、問題が発生する可能性があります.