`
kong0itey
  • 浏览: 299757 次
社区版块
存档分类
最新评论

struts2的通用分页工具条

阅读更多
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/当前struts2主题/ 目录下,我的是放在 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>




缺点和不足:

点击跳页功能存在不足,当一个页面有多个分页工具条时候,可能会出问题。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics