标签分页项目代码

标签处理类:

package com.csdn.hbsi.Servlet;


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


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


public class TestPageServlet extends HttpServlet {


private static final long serialVersionUID = 1L;
private List<String> datas;//用户储存数据的集合
public static final int PAGER_PAGESIZE=10;//定义每页要显示数据的条数


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


this.doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("utf-8");
int recordCount=this.datas.size();//获取总记录数
int pageNo=1; //获取当前页号
String pageNoStr=request.getParameter("pageNo");
if(pageNoStr!=null&&pageNoStr.equals("")){
pageNo=Integer.parseInt(pageNoStr);
}
//获取分页数据
int start=(pageNo-1)*PAGER_PAGESIZE;   //定义开始位置
int end=start+PAGER_PAGESIZE; //定义结束位置
if(end>this.datas.size()){
end=this.datas.size();
}
List<String> result=this.datas.subList(start,end);  //subList方法返回列表中指定的start(包括)和end(不包括)之间的数据

//把用于分页的数据和分页标签的需要的属性放到request中
request.setAttribute("datas", result);
request.setAttribute("pageNo",pageNo);
request.setAttribute("pageSize",PAGER_PAGESIZE);
request.setAttribute("recordCount", recordCount);

//请求转发到jsp页面
request.getRequestDispatcher("/Myjsp.jsp").forward(request, response);

}


public void init() throws ServletException {
// 准备分页的数据
datas=new ArrayList<String>();
for(int i=1;i<=123;i++){
datas.add("字符串"+i);
}
}


}



jsp类:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!-- 引入自定义标签 -->
<%@ taglib uri="http://csdn.hbsi.pageTag" prefix="tt"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
    <title>自定义分页标签使用示例</title>
<style type="text/css">
body {
margin-top: 20px;
text-align: left;
font-family: 宋体, Arial, Verdana;
font-size: 13px;
line-height: 150%;
min-width:800px;
word-break:break-all;
}
/* 分页标签样式 */
.pagination {
    padding: 5px;
    float: right;
}
.pagination a, .pagination a:link, .pagination a:visited {
    padding: 2px 5px 2px 5px;
    margin: 2px;
    border: 1px solid #aaaadd;
    text-decoration: none;
    color: #006699;
}
.pagination a:hover, .pagination a:active {
    border: 1px solid #ff0000;
    color: #000;
    text-decoration: none;
}
.pagination span.current {
    padding: 2px 5px 2px 5px;
    margin: 2px;
    border: 1px solid #ff0000;
    font-weight: bold;
    background-color: #ff0000;
    color: #FFF;
}
.pagination span.disabled {
    padding: 2px 5px 2px 5px;
    margin: 2px;
    border: 1px solid #eee;
    color: #ddd;
}
</style>
  </head>
<body>
<div style="margin:0px auto;width:700px">
<div id="title">
<h3>自定义数据分页标签的使用示例</h3>
</div>
<div id="data">
<table boder="1" width="600px" alignr="center">
<% //获取要分页的数据
List<String> datas=(List<String>)request.getAttribute("datas");
for(String str:datas){
out.print("<tr><td>"+str+"</td></tr>");
}
 %>
</table>
</div>
<%--自定义分页标签 --%>
<tt:pager pageNo="${pageNo}" pageSize="${pageSize}" recordCount="${recordCount}" url="TestPageServlet"/>
</div>
</body>
</html>



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>w</short-name>
 <uri>http://csdn.hbsi.pageTag</uri>
 <tag>
  <name>pager</name>
  <tag-class>com.csdn.hbsi.tags.PagerTag</tag-class>
  <body-content>empty</body-content>
  <attribute>
  <name>pageNo</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
  <name>pageSize</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
  <name>recordCount</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
  <name>url</name>
  <required>true</required>
  <rtexprvalue>true</rtexprvalue>
  </attribute>
 </tag>
</taglib>



servlet类:

package com.csdn.hbsi.Servlet;


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


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


public class TestPageServlet extends HttpServlet {


private static final long serialVersionUID = 1L;
private List<String> datas;//用户储存数据的集合
public static final int PAGER_PAGESIZE=10;//定义每页要显示数据的条数


public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {


this.doPost(request, response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("utf-8");
int recordCount=this.datas.size();//获取总记录数
int pageNo=1; //获取当前页号
String pageNoStr=request.getParameter("pageNo");
if(pageNoStr!=null&&pageNoStr.equals("")){
pageNo=Integer.parseInt(pageNoStr);
}
//获取分页数据
int start=(pageNo-1)*PAGER_PAGESIZE;   //定义开始位置
int end=start+PAGER_PAGESIZE; //定义结束位置
if(end>this.datas.size()){
end=this.datas.size();
}
List<String> result=this.datas.subList(start,end);  //subList方法返回列表中指定的start(包括)和end(不包括)之间的数据

//把用于分页的数据和分页标签的需要的属性放到request中
request.setAttribute("datas", result);
request.setAttribute("pageNo",pageNo);
request.setAttribute("pageSize",PAGER_PAGESIZE);
request.setAttribute("recordCount", recordCount);

//请求转发到jsp页面
request.getRequestDispatcher("/Myjsp.jsp").forward(request, response);

}


public void init() throws ServletException {
// 准备分页的数据
datas=new ArrayList<String>();
for(int i=1;i<=123;i++){
datas.add("字符串"+i);
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值