JSP中的分页技术源码(MYSQL数据库)

<%@page import="cn.bestchance.biz.impl.BookInfoBizImpl"%>
<%@page import="cn.bestchance.biz.BookInfoBiz"%>
<%@page import="cn.bestchance.entity.BookInfo"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="gb2312">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta content="all" name="robots" />
<meta name="author" content="Fisher" />
<meta name="Copyright"
    content="Copyright 2007-2008, 版权所有 www.reefdesign.cn" />
<meta name="description" content="reefdesign" />
<meta name="keywords" content="reefdesign" />
<title>电子书城</title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" rev="stylesheet" href="css/style.css"
    type="text/css" media="all" />
</head>
<body class="main">
    <!-- top引入开始 -->
    <%@include file="top.jsp"%>
    <!-- top引入结束-->
    <!-- 构造图书信息数据 -->
    <%
        request.setCharacterEncoding("utf-8");
        ArrayList<BookInfo> bookArray = new ArrayList<BookInfo>();
        BookInfoBiz biz1 = new BookInfoBizImpl();
        String searchContent = request.getParameter("searchContent");
        String flag = request.getParameter("flag"); //获取搜索方式:1为右上角的搜索,2为左边的搜索
        if(flag == null){//默认使用第一中搜索方式
            flag="1";       
        }
        int pageSize = 3;   //每页行数
        int allRecords = 0; //总记录数
        int totalPages = 0; //总页数
        int pageIndex = 1;  //当前页
        allRecords = biz1.getAllRecords(searchContent);
        //计算总页数
        totalPages = (allRecords%pageSize == 0)?(allRecords/pageSize):(allRecords/pageSize+1);
        //获得当前页
        String pageIndexStr = request.getParameter("pageIndex");
        if(pageIndexStr == null){
            pageIndexStr = "1";
        }
        pageIndex = Integer.parseInt(pageIndexStr);
        pageIndex= pageIndex<2?1:pageIndex;
        pageIndex=pageIndex>totalPages?totalPages:pageIndex;
        bookArray = biz1.searchAll(pageSize*(pageIndex-1), pageSize,searchContent);
    %>
    <div id="divpagecontent">
        <table width="100%" border="0" cellspacing="0">
            <tr>
                <td width="25%">
                    <table width="100%" border="0" cellspacing="0"
                        style="margin-top: 30px">
                        <tr>
                            <td class="listtitle">缩小搜索范围</td>
                        </tr>
                        <tr>
                            <td class="listtd"><br />
                                <p>
                                    关键字:<input type="text" name="submitSearch" id="submitSearch" class="inputtable" />
                                </p>
                                <p>
                                类&nbsp;&nbsp;&nbsp;&nbsp;别:
                                <select name="select" id="selectTypeId">
                                <%
                                    //获取所有的父节点
                                    //获取所有父节点:用于左侧导航栏
                                    List<BookType> bookTypeList = bookTypebiz.searchAllBookType();
                                    for(BookType bt : bookTypeList){
                                        String typeName = bt.getType_name();
                                 %>
                                        <option value="<%=bt.getType_id()%>"><%=typeName %></option>
                                <%} %>
                                </select>
                                </p>
                                <p style="text-align: center">
                                    <input name="submitSearch" type="submit" class="inputbutton" value="提交" onclick="submitSearch();" />
                                </p></td>
                        </tr>
                    </table></td>
                <td>
                    <div style="text-align: right; margin: 5px 10px 5px 0px">
                        <a href="index.jsp">首页</a>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;公告新闻&nbsp;&nbsp;&nbsp;&nbsp;&gt;&nbsp;&nbsp;&nbsp;&nbsp;文章标题
                    </div>
                    <table cellspacing="0" class="infocontent">
                        <tr>
                            <td>
                                <table width="100%" border="0" cellspacing="0">
                                    <tr>
                                        <td style="padding: 10px">以下 <strong><%=allRecords%></strong>
                                            条结果按 <strong>销量</strong> 排列 每页显示<strong><%=pageSize%></strong><hr /> <%
    for(BookInfo books : bookArray){
 %>
                                            <table border="0" cellspacing="0" class="searchtable">
                                                <tr>
                                                    <td width="20%" rowspan="2">
                                                        <div class="divbookpic">
                                                            <p>
                                                                <a href="info.jsp?book_id=<%=books.getBookId()%>"> <img
                                                                    src="good_imgs/<%=books.getBookLogo()%>" width="115"
                                                                    height="129" border="0" /> </a>
                                                            </p>
                                                        </div></td>
                                                    <td colspan="2"><font class="bookname"><%=books.getBookName()%></font><br />
                                                        作者:<%=books.getBookAuthor()%><br /> <%
                                                String desc = books.getBookDes();
                                                desc = desc.length()>120?desc.substring(0, 120):desc;
 %> <%=desc+"..."%></td>
                                                </tr>
                                                <tr>
                                                    <td>售价:<font color="#FF0000"><%=books.getBookPrice()%></font>&nbsp;&nbsp;&nbsp;&nbsp;原价:<s><%=books.getBookPriceOld()%></s>
                                                    </td>
                                                    <td style="text-align: right"><a
                                                        href="addCart?bookId=<%=books.getBookId()%>&bookName=<%=books.getBookName()%>&bookPrice=<%=books.getBookPrice()%>&bookPriceOld=<%=books.getBookPriceOld()%>">
                                                            <img src="images/buy.gif" width="91" height="27"
                                                            border="0" style="margin-bottom: -8px" /> </a></td>
                                                </tr>
                                            </table> <%
    }
 %>
                                            <div class="pagination">
                                                <ul>
                                                <%
                                                    if(bookArray.size() != 0){
                                                 %>
                                                    <li class="nextpage"><a
                                                        href="search.jsp?pageIndex=<%=pageIndex-1%>">&lt;&lt;上一页</a>
                                                    </li>
                                                    <%}
            //若9页以下  直接显示
                if(bookArray.size() != 0){
                    if(totalPages <=9){
                        for(int i = 1;i<=totalPages;i++){
                            if(pageIndex==i){
                                out.print("<li class='currentpage'>"+i+"</li>");
                            }else{
                                out.print("<li ><a href='search.jsp?pageIndex="+i+"&searchContent="+searchContent+"'>"+i+"</a></li>");
                            }
                        }
                    }else{
                    //前4页直接显示
                        if(pageIndex <=4 ){
                            for(int i = 1;i<=9;i++){
                                if(pageIndex==i){
                                    out.print("<li class='currentpage'>"+i+"</li>");
                                }else{
                                    out.print("<li ><a href='search.jsp?pageIndex="+i+"&searchContent="+searchContent+"'>"+i+"</a></li>");
                                }
                            }
                        }else{ //5->n-4页的显示
                            if(pageIndex <= totalPages-4){
                                for(int i = pageIndex-4;i<=pageIndex+4;i++){
                                    if(pageIndex==i){
                                        out.print("<li class='currentpage'>"+i+"</li>");
                                    }else{
                                        out.print("<li ><a href='search.jsp?pageIndex="+i+"&searchContent="+searchContent+"'>"+i+"</a></li>");
                                    }
                                }
                            }else{
                                for(int i = totalPages-8;i<=totalPages;i++){
                                    if(pageIndex==i){
                                        out.print("<li class='currentpage'>"+i+"</li>");
                                    }else{
                                        out.print("<li ><a href='search.jsp?pageIndex="+i+"&searchContent="+searchContent+"'>"+i+"</a></li>");
                                    }
                                }
                            }
                        }
                    }
                }
            %>                                      <%
                                                        if(bookArray.size() != 0){
                                                     %>
                                                    <li class="nextpage"><a
                                                        href="search.jsp?pageIndex=<%=pageIndex+1%>">下一页
                                                            &gt;&gt;</a>
                                                    </li>
                                                    <%} %>
                                                </ul>
                                            </div></td>
                                    </tr>
                                </table></td>
                        </tr>
                    </table></td>
            </tr>
        </table>
    </div>
    <div id="divfoot">
        <table width="100%" border="0" cellspacing="0">
            <tr>
                <td rowspan="2" style="width: 10%"><img
                    src="images/bottomlogo.gif" width="195" height="50"
                    style="margin-left: 175px" /></td>
                <td style="padding-top: 5px; padding-left: 50px"><a href="#"><font
                        color="#747556"><b>CONTACT US</b> </font> </a></td>
            </tr>
            <tr>
                <td style="padding-left: 50px"><font color="#CCCCCC"><b>COPYRIGHT
                            2008 &copy; eShop All Rights RESERVED.</b> </font></td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">
        function submitSearch(){
            var searchContent = document.getElementById("submitSearch").value;
            var a=document.getElementById("selectTypeId");
            var selectTypeId=a.options[a.selectedIndex].value;
            window.location.href="search.jsp?keyValue=" + searchContent + "&selectTypeId=" + selectTypeId + "&flag=2" ;
        }
    </script>
</body>
</html>

效果图如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值