JSP仿百度分页,谷歌分页页码处理

来源:互联网 发布:微信扫描淘宝二维码 编辑:程序博客网 时间:2024/05/07 04:16
像百度一样的jsp分页效果,像goolge一样的分页效果!
根据设定参数一次取一页记录内容显示

jsp页面代码如下:


Java代码
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="dao.*"%>
<%@ page import="bean.*"%>
<html>
<head>
</head>
<body>
<%
int pagesize = 10;//每页显示记录数
int liststep = 10;//最多显示分页页数
int pages = 1;//默认显示第一页
if (request.getParameter("pages") != null) {
pages = Integer.parseInt(request.getParameter("pages"));//分页页码变量
}
int count = PageDao.getmaxpagecount();//假设取出记录总数
int pagescount = (int) Math.ceil((double) count / pagesize);//求总页数,ceil(num)取整不小于num
if (pagescount < pages) {
pages = pagescount;//如果分页变量大总页数,则将分页变量设计为总页数
}
if (pages < 1) {
pages = 1;//如果分页变量小于1,则将分页变量设为1
}
int listbegin = (pages - (int) Math.ceil((double) liststep / 2));//从第几页开始显示分页信息
if (listbegin < 1) { //当前页-(总显示的页列表数/2)
listbegin = 1;
}
int listend = pages + liststep / 2;//分页信息显示到第几页//当前页+(总显示的页列表数/2)
if (listend > pagescount) {
listend = pagescount + 1;
}
%>
<table align="center">
<tr>
<th>
图书编号
</th>
<th>
图书名称
</th>
<th>
出版社
</th>
<th>
作者
</th>
<th>
价格
</th>
</tr>
<%
List<Book> list = PageDao.getAllPageInfo(pages);
Iterator<Book> it = list.iterator();
while (it.hasNext()) {
Book b = it.next();
if (b.getId() % 2 == 0) {
out.println("<tr bgcolor='blue'>");
} else {
out.println("<tr bgcolor='red'>");
}
%>
<td><%=b.getId()%></td>
<td><%=b.getBookname()%></td>
<td><%=b.getBookpublish()%></td>
<td><%=b.getBookauthor()%></td>
<td><%=b.getBookprice()%></td>
<%
out.println("<tr bgcolor='red'>");
}
%>
</table>
<table align="center">
<tr>
<%
//<显示分页信息
//<显示上一页
if (pages > 1) {
out.println("<td><a href=?pages=" + (pages - 1)
+ ">上一页</a></td>");
}//>显示上一页
//<显示分页码
for (int i = listbegin; i < listend; i++) {
if (i != pages) {//如果i不等于当前页
out.println("<td><a href=?pages=" + i + ">[" + i
+ "]</a></td>");
} else {
out.println("<td>[" + i + "]</td>");
}
}//显示分页码>
//<显示下一页
if (pages != pagescount) {
out.println("<td><a href=?pages=" + (pages + 1)
+ ">下一页</a></td>");
}//>显示下一页
//>显示分页信息
%>
</tr>
</table>
</body>
</html>



Dao层代码:


Java代码
package dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import bean.Book;

public class PageDao {
public static int getmaxpagecount() {
int num = 0;
Connection conn = null;
Statement stm = null;
ResultSet rs = null;
try {
conn = ConnectionManager.getInstances();
stm = conn.createStatement();
rs = stm.executeQuery("select count(*) from book");
if (rs.next()) {
num = rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return num;
}

public static List<Book> getAllPageInfo(int curpage) {
List<Book> list = new ArrayList<Book>();
Connection conn = null;
Statement stm = null;
ResultSet rs = null;

try {
conn = ConnectionManager.getInstances();
conn.setAutoCommit(false);
stm = conn.createStatement();
rs = stm
.executeQuery(("select top 10 * from book where id not in (select top "
+ ((curpage - 1) * 10) + " id from book order by id)order by id"));
while (rs.next()) {
Book b = new Book();
b.setId(rs.getInt("id"));
b.setBookname(rs.getString("bookname"));
b.setBookpublish(rs.getString("bookpublish"));
b.setBookauthor(rs.getString("bookauthor"));
b.setBookprice(rs.getDouble("bookprice"));
list.add(b);
}
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
rs.close();
stm.close();
conn.close();
} catch (SQLException e) {
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}
}
return list;
}
}



连接代码:




Java代码
package dao;
import java.sql.*;
public class ConnectionManager {

public static final String DRIVER="com.microsoft.jdbc.sqlserver.SQLServerDriver";
public static final String URL="jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pagination";
public static final String UID="sa";
public static final String PWD="112233";

public static Connection getInstances(){
Connection conn=null;

try {
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,UID,PWD);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {

e.printStackTrace();
}
return conn;
}
}
原创粉丝点击