自定義標籤實現分頁查詢1.0版本

首先創建一個PageBean類 類中要有pc(當前頁碼),ps(每頁記錄數),tr(總記錄數) List<T> beanList(裝載對象的集合) url(訪問地址,用於跳轉頁面)

接下來創建seter geter方法 再自己添加一個getTp方法(用於得到全部頁碼數,可由tr和ps得到)

import java.util.List;

public class PageBean {
	private int pc;
	private int ps;
	private int tr;
	private List<CommodityModel> beanList;
	private String url;
	@Override
	public String toString() {
		return "PageBean [pc=" + pc + ", ps=" + ps + ", tr=" + tr + ", beanList=" + beanList + ", url=" + url + "]";
	}
	public int getPc() {
		return pc;
	}
	public void setPc(int pc) {
		this.pc = pc;
	}
	public int getPs() {
		return ps;
	}
	public void setPs(int ps) {
		this.ps = ps;
	}
	public int getTr() {
		return tr;
	}
	public void setTr(int tr) {
		this.tr = tr;
	}
	public List<CommodityModel> getBeanList() {
		return beanList;
	}
	public void setBeanList(List<CommodityModel> beanList) {
		this.beanList = beanList;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public int getTp() {
		int tp=tr/ps;
		return tr%ps==0? tp: tp+1;
	}
}


Servler中處理好數據後得到pagebean對象pb,使用request.setAttribute("pb",pb);把pb保存到request中,以便用EL表達式獲取。


以下是部分JSP代碼,顯示查詢結果

<%@ taglib prefix="ex" uri="/WEB-INF/page.tld" %> 
該語句用於JSP導入我的自定義標籤 uri中爲WEB-INF下的名爲page.tld的文件

<table align='center' width='20%'>
		<tr>
			<td>條碼</td>
			<td>名稱</td>
			<td>類別</td>
			<td>價格</td>
		</tr>

<%--  <ex:page list="${pb.beanList }"/>  --%>
<c:forEach items="${pb.beanList }" var="goods">
		<tr>
			<td>${goods.barcode }</td>
			<td>${goods.name }</td>
			<td>${goods.type }</td>
			<td>${goods.price }</td>
			<td><a href="/CommodityMIS/DeleteServlet?method=delete&barcode=${goods.barcode }">刪除</a>
				  
				<a href="/CommodityMIS/UpdateServlet?barcode=${goods.barcode }">更新</a>
			</td>
		</tr>
</c:forEach>
	</table>
<ex:page pb="${pb }"/>

其中的<ex:page pb="${pb }"/>就是我們的重頭戲了

只使用該標籤可以直接實現分頁


下面介紹創建該自定義標籤的代碼

<?xml version="1.0" encoding="UTF-8"?>
<j2ee:taglib version="2.0" xmlns:j2ee="http://java.sun.com/xml/ns/j2ee" xmlns:xml="http://www.w3.org/XML/1998/namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd ">
  <j2ee:tlib-version>0.0</j2ee:tlib-version>
  <j2ee:short-name>Example</j2ee:short-name>
  	<j2ee:tag>
  		<j2ee:name>page</j2ee:name>
  		<j2ee:tag-class>Demo.PageTag</j2ee:tag-class>
  		<j2ee:body-content>scriptless</j2ee:body-content>
  			<j2ee:attribute>
  				<j2ee:name>pb</j2ee:name>
  				<j2ee:required>true</j2ee:required>
  				<j2ee:rtexprvalue>true</j2ee:rtexprvalue>
  			</j2ee:attribute>
  	</j2ee:tag>
</j2ee:taglib>

介紹如何創建簡單tld文件在上一篇博客有介紹,這個tld文件中規定要有名爲pb的屬性,也就是我所要得到的pagebean對象


下面介紹一下該標籤的標籤處理類創建 創建過程在上一篇博客有簡單介紹,我就直接上代碼了

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import edu.gdut.ec.commodity.entity.CommodityModel;
import edu.gdut.ec.commodity.entity.PageBean;

public class PageTag extends SimpleTagSupport {
	private PageBean pb;

	public PageBean getPb() {
		return pb;
	}

	public void setPb(PageBean pb) {
		this.pb = pb;
	}

	@Override
	public void doTag() throws JspException, IOException {
		int begin;
		int end;
		getJspContext().getOut().print("<center>");
		getJspContext().setAttribute("pb", pb);
		getJspContext().getOut().print("第" + pb.getPc() + "頁/共" + pb.getTp() + "頁");
		getJspContext().getOut().print("<a href =\"");
		getJspContext().getOut().print(pb.getUrl() + "&pc=1\"" + ">首頁</a>");
		if (pb.getPc() > 1) {
			getJspContext().getOut().print("<a href =\"");
			getJspContext().getOut().print(pb.getUrl() + "&pc=" + (pb.getPc() - 1) + "\"" + ">上一頁</a>");
		}
		if (pb.getTp() <= 10) {
			begin = 1;
			end = pb.getTp();
		} else {
			begin = pb.getPc() - 5;
			end = pb.getPc() + 4;
			if (begin < 1) {
				begin = 1;
				end = 10;
			}
			if (end > pb.getTp()) {
				begin = pb.getTp() - 9;
				end = pb.getTp();
			}
		}
		for (int i = begin; i < end; i++) {
			if (i == pb.getPc()) {
				getJspContext().getOut().print("[" + i + "]");
			} else {
				getJspContext().getOut().print("<a href =\"");
				getJspContext().getOut().print(pb.getUrl() + "&pc=" + i + "\"" + ">[" + i + "]</a>");
			}
		}
		if (pb.getPc() < pb.getTp()) {
			getJspContext().getOut().print("<a href=\"" + pb.getUrl() + "&pc=" + (pb.getPc() + 1) + "\">下一頁</a>");
		}
		getJspContext().getOut().print("<a href=\"" + pb.getUrl() + "&pc=" + pb.getTp() + "\">尾頁</a>");
		getJspContext().getOut().print("</center>");
	}

}

在JSP頁面用EL表達式得到pagebe對象中已經有所有屬性,實現的操作就是在Servlet中操作數據庫給pagebean對象填充各項屬性,利用到MYSQL的分頁查詢語句進行兩次查詢,可以得到tr和tp,tr用select count(*) from 。。。 tp用select*from 。。。limt ?,?語句

看官們就自己去實現吧   


這是效果圖



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章