使用自定义标签分页

如果不使用标签的话,在jsp里面分页的话就会牵涉到大量的Java代码,为了避免这个问题我们今天就来使用自定义标签来实现分页的功能。当然,如果你有更好的方法也可以一起交流下经验,互相学习嘛

1.java代码

Pager.java

package tag;


public class Pager {
private int currentPage; //当前页
private int totalRecord; //记录总数
private int pageLength=10; //每页最大显示记录条数

public Pager(int currentPage,int totleRecord,int pageLength)
{
this.currentPage=currentPage;
this.totalRecord=totleRecord;
this.pageLength=pageLength;
}
public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public int getTotalRecord() {
return totalRecord;
}

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

public int getPageLength() {
return pageLength;
}

public void setPageLength(int pageLength) {
this.pageLength = pageLength;
}


public int getTotalPage() {
return (totalRecord%pageLength==0)?totalRecord/pageLength:totalRecord/pageLength+1;
}

public int getFirstPage()
{
return 1;
}

public int getLastPage()
{
return getTotalPage();
}

public int getCurrentRecord() {
if(currentPage==0)
return 0;

return pageLength*(currentPage-1);
}

}












PagerTag.java

package edu.zjgsu.cie.tag;

import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.TagSupport;

import edu.zjgsu.cie.util.SqlFunction;
public class PagerTag extends TagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String sql=null;
private int pageLength=10; //每页显示的数量
private int currentPage=1; //当前页
private int totleRecord=0; //数据总量
private String path =null; //本页的URL地址
Pager page;

public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public int getPageLength() {
return pageLength;
}
public void setPageLength(int pageLength) {
this.pageLength = pageLength;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotleRecord() {
return totleRecord;
}
public void setTotleRecord(int totleRecord) {
this.totleRecord = totleRecord;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int doStartTag()
{
if(path==null)
path=((HttpServletRequest)pageContext.getRequest()).getPathInfo();
SqlFunction sqlfunction=new SqlFunction();
ResultSet rs=sqlfunction.executeQuery(sql);
try {
rs.last();
totleRecord=rs.getRow();
rs.absolute((currentPage-1)*pageLength+1); //移到指定的行数上
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
page=new Pager(currentPage,totleRecord,pageLength);
pageContext.getRequest().setAttribute("result",rs);
return (EVAL_BODY_INCLUDE);
}
public int doEndTag()
{
if(path==null)
return SKIP_BODY;
try
{
pageContext.getOut().write(this.getHtmlPager(page));

}
catch (Exception e)
{
e.printStackTrace();
}
return EVAL_PAGE;
}
private String getHtmlPager(Pager page)
{
StringBuffer buf=new StringBuffer();
if(page.getTotalPage()>0)
{
buf.append("<table width=/"100%/" border=/"0/" cellspacing=/"0/" cellpadding=/"0/"><tr><td align=/"center/">");
buf.append("第"+String.valueOf(page.getCurrentPage())+"页/共"+String.valueOf(page.getTotalPage())+"页");
buf.append("&nbsp;记录&nbsp;"+String.valueOf(page.getCurrentRecord()+1)+"&nbsp;到&nbsp;"
+(String.valueOf((page.getCurrentPage()*page.getPageLength()>page.getTotalRecord())?
page.getTotalRecord():
page.getCurrentPage()*page.getPageLength()
)));
if(page.getCurrentPage()!=1)
buf.append("&nbsp;<a href=/""+this.path+"?page=1/"/>第一页</a>");
if(page.getCurrentPage()>1)
{
buf.append("&nbsp;<a href=/""+this.path+"?page="
+String.valueOf(page.getCurrentPage()-1)+"/">上一页</a>");
}
if(page.getCurrentPage()<page.getTotalPage())
{
buf.append("&nbsp;<a href=/""+this.path+"?page="
+String.valueOf(page.getCurrentPage()+1)+"/">下一页</a>");
}
buf.append("&nbsp;<a href=/""+this.path+"?page="
+String.valueOf(page.getTotalPage())+"/"/>最后一页</a>");
buf.append("</td></tr></table>");
}
return buf.toString();
}
}




2. demo.tld 文件 。该文件默认是放到WEB-INF/tlds目录下的,当然也可以自己修改路径

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">

<!-- a tab library descriptor -->

<taglib>
<tlibversion >1.6</tlibversion>
<jspversion>1.2</jspversion>
<short-name>page</short-name>
<uri>emis</uri>

<tag>
<name>pages</name>
<tagclass>tag.PagerTag</tagclass>
<bodycontent>jsp</bodycontent>

<attribute>
<name>sql</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>pageLength</name> //每页的长度
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>currentPage</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

<attribute>
<name>path</name> //这个是当前页的URL地址,必须的
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>

</tag>
</taglib>







3. Pages.jsp

<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@page import="java.sql.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
<%@ taglib prefix="pg" uri="WEB-INF/tlds/demo.tld" %> //如果要改tld文件的路径的话,这里要随之改动
<title>分页demo</title>
</head>
<body>
<%
Object currentPage=request.getParameter("page");
int currentpage=1;
if(currentPage==null)
;
else{
currentpage=Integer.parseInt(currentPage.toString());}%>
<pg:pages sql= 自己的SQL语句 pageLength="10" currentPage="<%=currentpage%>" path="Pages.jsp">
<table align="center">
<TR>
<TH>新闻名称</TH></TR>
<%
ResultSet rs=(ResultSet)request.getAttribute("result");
int i=0;
while(rs.next()&&i<10)
{
%>
<TR>
<TD>
<%=i+1%>.<%=rs.getString(1)%>
</TD>
</TR>
<%
i++;
}
%>
</table>
</pg:pages>
</body>
</html>

看到这里大家一定迫不及待的跃跃欲试了吧。OK,下面是你们大家身手的时候了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值