首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

jsp中怎么实现分页显示

2012-02-11 
jsp中如何实现分页显示谁知道类似百度的分页是怎么实现的吗?我想用jsp来实现,有没有类似的代码?分页形式如

jsp中如何实现分页显示
谁知道类似百度的分页是怎么实现的吗?我想用jsp来实现,有没有类似的代码?分页形式如下
1 [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页 [尾页] 


[解决办法]
分页的代码网络上很多的,可以去搜索下的。
[解决办法]

HTML code
<%if(!rs.next())break;}%><tr>    <td width="786" height="30" colspan="9" align="right"><table width="786" align="center">    <tr>    <td width="786" height="30" colspan="9" align="right">共<%=pagecount%>页        <a href="/Manager/Placard/placard.jsp?topage=<%=1%>">第一页</a>        <a href="/Manager/Placard/placard.jsp?topage=<%=showpage-1%>">上一页</a>        <a href="/Manager/Placard/placard.jsp?topage=<%=showpage+1%>">下一页</a>        <a href="/Manager/Placard/placard.jsp?topage=<%=pagecount%>">最后一页</a>        <span class="bgcolor">      <input name="topage" type="text" class="txt_grey" size="3" value="<%=showpage%>">页</span></td></tr></table></td>  </tr><%}condata.close(con);}catch(Exception e){e.printStackTrace();}%></table></body></html>
[解决办法]
HTML code
<%@ page contentType="text/html; charset=gb2312" %><%@ page import="java.sql.*"%><jsp:useBean id="condata" scope="page" class="com.crm.util.DB"/><html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>placard page</title></head><%!Connection con=null;Statement stmt = null;ResultSet rs=null;String sql;int code;int pagesize=10;int rowcount=0;int pagecount=1;%><body bgcolor="#ffffff"><table width="785" height="117" border="1" align="center" cellpadding="0" cellspacing="0">  <tr>    <td width="786" height="38" align="center" colspan="6"><font face="隶书" size="7">网站公告信息</font></td>  </tr>  <tr>    <td width="85%" height="29" align="right" colspan="6">   <a href="/Manager/Placard/placard_show.jsp">发布公告信息</a></td>  </tr>  <tr>    <td width="10%" height="29" align="center">ID</td>    <td width="10%" height="29" align="center">标题</td>       <td width="10%" height="29" align="center">发布日期</td>    <td width="10%" height="29" align="center">作者</td>    <td width="10%" height="29" align="center">接收者</td>    <td width="10%" height="29" align="center">内容</td>    <td width="10%" height="29" align="center">修改</td>    <td width="15%" height="29" align="center">删除</td>  </tr><%sql="select ID,Title,publishtime,publisher,accepter,content from notice ";try{con=condata.getCon();stmt=condata.getStmt(con);rs=condata.executeQuery(stmt,sql);if(!rs.next()){%><script language="javascript">    //alert("没有公共信息");   // history.back();</script><%}else{rs.last();rowcount=rs.getRow();int showpage=1;pagecount=((rowcount%pagesize)==0?(rowcount/pagesize):(rowcount/pagesize)+1); String topage=request.getParameter("topage");if(topage!=null){showpage=Integer.parseInt(topage);if(showpage>pagecount){  showpage=pagecount;  }else if(showpage<=0){  showpage=1;  }}rs.absolute((showpage-1)*pagesize+1);for(int i=1;i<=pagesize;i++){code=rs.getInt("ID");%> <tr>    <td width="10%" height="32" align="center"><%=code%></td>    <td width="10%" height="32" align="center"><%=rs.getString("Title")%></td>    <td width="10%" height="32" align="center"><%=rs.getDate("publishtime")%></td>    <td width="10%" height="32" align="center"><%=rs.getString("publisher")%></td>     <td width="10%" height="32" align="center"><%=rs.getString("accepter")%></td>    <td width="10%" height="32" align="center"><%=rs.getString("content")%></td>    <td width="10%" height="32" align="center">   <a href="/Manager/Placard/placard_change.jsp?id=<%=code%>">修改</a></td>    <td width="15%" height="32" align="center">  <a href="/Manager/Placard/placard_delete.jsp?id=<%=code%>">删除</a></td>  </tr><%if(!rs.next())break;}%><tr>    <td width="786" height="30" colspan="9" align="right"><table width="786" align="center">    <tr>    <td width="786" height="30" colspan="9" align="right">共<%=pagecount%>页        <a href="/Manager/Placard/placard.jsp?topage=<%=1%>">第一页</a>        <a href="/Manager/Placard/placard.jsp?topage=<%=showpage-1%>">上一页</a>        <a href="/Manager/Placard/placard.jsp?topage=<%=showpage+1%>">下一页</a>        <a href="/Manager/Placard/placard.jsp?topage=<%=pagecount%>">最后一页</a>        <span class="bgcolor">      <input name="topage" type="text" class="txt_grey" size="3" value="<%=showpage%>">页</span></td></tr></table></td>  </tr><%}condata.close(con);}catch(Exception e){e.printStackTrace();}%></table></body></html> 


[解决办法]

Java code
<%@ page language="java" pageEncoding="GB2312"%><%    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>        <base href="<%=basePath%>">        <title>仿BAIDU & GOOGLE 分页页码显示格式</title>        <meta http-equiv="pragma" c>        <meta http-equiv="cache-control" c>        <meta http-equiv="expires" c>        <meta http-equiv="keywords" c>        <meta http-equiv="description" c>        <style>        <!--            body...{                .p...{                    PADDING-LEFT: 18px; FONT-SIZE: 14px; WORD-SPACING: 4px                }            }        -->        </style>    </head>    <body>        <div class="p">            <%                int pagesize = 10;//每页显示记录数                int liststep = 20;//最多显示分页页数                int pages = 1;//默认显示第一页                if (request.getParameter("pages") != null) ...{                    pages = Integer.parseInt(request.getParameter("pages"));//分页页码变量                }                //sql="select count(*) from table";//取出记录总数,把*换成记录名性能更加                int count = 305;//假设取出记录总数                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) ...{                    listbegin = 1;                }                int listend = pages + liststep/2;//分页信息显示到第几页                if (listend > pagescount) ...{                    listend = pagescount + 1;                }                //显示数据部分                int recordbegin = (pages - 1) * pagesize;//起始记录                int recordend = 0;                recordend = recordbegin + pagesize;                //最后一页记录显示处理                if (pages == pagescount) ...{                    recordend = (int) (recordbegin + pagesize * (count % pagesize) * 0.1);                }                //Connection conn = null;//要测试显示数据请自己链接数据库                //PreparedStatement stmt = null;                //String strSql = "select * from usertable limit recordbegin,pagesize";//取出分页记录,SQL因数据库而异!                //stmt = conn.prepareStatement(strSql);                //ResultSet rs = stmt.executeQuery();                //while(rs.next())){                //    response.getWriter().println(""+rs.getString("userId")+"<br>");//显示取出的数据                //}                for (int i = recordbegin; i < recordend; i++) ...{                    response.getWriter().println("record " + i + "<br>");                }                //显示数据部分                //<显示分页信息                //<显示上一页                if (pages > 1) ...{                    response.getWriter().println(                    "<a href=?pages=" + (pages - 1) + ">上一页</a>");                }//>显示上一页                //<显示分页码                for (int i = listbegin; i < listend; i++) ...{                    if (i != pages) ...{//如果i不等于当前页                        response.getWriter().println(                        "<a href=?pages=" + i + ">[" + i + "]</a>");                    } else ...{                        response.getWriter().println("[" + i + "]");                    }                }//显示分页码>                //<显示下一页                if (pages != pagescount) ...{                    response.getWriter().println(                    "<a href=?pages=" + (pages + 1) + ">下一页</a>");                }//>显示下一页                //>显示分页信息            %>        </div>    </body></html> 


[解决办法]

探讨
HTML code
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<jsp:useBean id="condata" scope="page" class="com.crm.util.DB"/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>
placard page
</title>
</head>
<%!
Connection con=null;
Statement stmt = null;
ResultSet rs=null;

String sql;
int code;
int pages…

[解决办法]
<%@ page language="java" pageEncoding="GB2312"%>
<%
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>
<base href="<%=basePath%>">
<title>仿BAIDU & GOOGLE 分页页码显示格式</title>
<meta http-equiv="pragma" c>
<meta http-equiv="cache-control" c>
<meta http-equiv="expires" c>
<meta http-equiv="keywords" c>
<meta http-equiv="description" c>
<style>
<!--
body...{
.p...{
PADDING-LEFT: 18px; FONT-SIZE: 14px; WORD-SPACING: 4px
}
}
-->
</style>
</head>
<body>
<div class="p">
<%
int pagesize = 10;//每页显示记录数
int liststep = 20;//最多显示分页页数
int pages = 1;//默认显示第一页
if (request.getParameter("pages") != null) ...{
pages = Integer.parseInt(request.getParameter("pages"));//分页页码变量
}
//sql="select count(*) from table";//取出记录总数,把*换成记录名性能更加
int count = 305;//假设取出记录总数
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) ...{
listbegin = 1;
}
int listend = pages + liststep/2;//分页信息显示到第几页
if (listend > pagescount) ...{
listend = pagescount + 1;
}
//显示数据部分
int recordbegin = (pages - 1) * pagesize;//起始记录
int recordend = 0;
recordend = recordbegin + pagesize;
//最后一页记录显示处理
if (pages == pagescount) ...{
recordend = (int) (recordbegin + pagesize * (count % pagesize) * 0.1);
}
//Connection conn = null;//要测试显示数据请自己链接数据库
//PreparedStatement stmt = null;
//String strSql = "select * from usertable limit recordbegin,pagesize";//取出分页记录,SQL因数据库而异!
//stmt = conn.prepareStatement(strSql);
//ResultSet rs = stmt.executeQuery();


//while(rs.next())){
// response.getWriter().println(""+rs.getString("userId")+"<br>");//显示取出的数据
//}
for (int i = recordbegin; i < recordend; i++) ...{
response.getWriter().println("record " + i + "<br>");
}
//显示数据部分
//<显示分页信息
//<显示上一页
if (pages > 1) ...{
response.getWriter().println(
"<a href=?pages=" + (pages - 1) + ">上一页</a>");
}//>显示上一页
//<显示分页码
for (int i = listbegin; i < listend; i++) ...{
if (i != pages) ...{//如果i不等于当前页
response.getWriter().println(
"<a href=?pages=" + i + ">[" + i + "]</a>");
} else ...{
response.getWriter().println("[" + i + "]");
}
}//显示分页码>
//<显示下一页
if (pages != pagescount) ...{
response.getWriter().println(
"<a href=?pages=" + (pages + 1) + ">下一页</a>");
}//>显示下一页
//>显示分页信息
%>
</div>
</body>
</html>

热点排行