自定义标签实现分页查询1.0版本

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/starjingweij/article/details/78726384

首先创建一个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 ?,?语句

看官们就自己去实现吧   


这是效果图



猜你喜欢

转载自blog.csdn.net/starjingweij/article/details/78726384