分页标签

来源:互联网 发布:安卓手机怎么备份数据 编辑:程序博客网 时间:2024/05/01 21:30

自定义标签,实现分页。

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%@ taglib uri="http://csdn.hbsi/pageTag" prefix="q"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "/";
%>


<!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>
<hr />
</div>
<div id="data">


<table border="1" width="600px" align="center">
<% //从请求中获取要进行分页的数据
List<String> datas=(List<String>)request.getAttribute("datas");
for(String str:datas){
out.println("<tr><td>"+str+"</td></tr>");
}
%>

</table>
</div>
<%--自定义分页标签 --%>
<q:Pager pageNo="${pageNO}" pageSize="${pageSize}" recordCount="${recordCount}" url="TestPaperServlet.jsp"/>

</div>
</body>

</html>


package com.csdn.hbsi.Servlet;


import java.io.IOException;
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 TestPaperServlet extends HttpServlet {



private static final long serialVersionUID = 1L;

private List<String> datas;//用于用户存储数据的集合
public static final int PAGER_PAGESIZE=10;//定义每页要显示的数据条数
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}


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("/Paper.jsp").forward(request, response);

}



public void init() throws ServletException {//在初始化servlet的时候进行的数据
//准备用户分页的数据
datas=new ArrayList<String>();
for(int i=1;i<=123;i++){
datas.add("字符串"+i);
}

}


}


<?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>Paper</name>
<tag-class>com.hbsi.csdn.tags.PaperTag</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>

原创粉丝点击