jsp分页显示完整实例
本文实例讲述了jsp分页显示的实现方法。分享给大家供大家参考。
具体实现方法如下:
<%@pagecontentType="text/html;charset=gb2312"language="java"import="java.sql.*"errorPage=""%> <%@includefile="../inc/ChkPwd.jsp"%> <%@includefile="../DB.jsp"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <head> <metaname="robots"content="noindex,nofollow"/> <metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/> <title>文章管理</title> <linkhref="Style.css"rel="stylesheet"type="text/css"/> </head> <body> <tablewidth="98%"height="77%"border="0"cellpadding="0"cellspacing="0"> <trclass="STYLE1"> <tdwidth="100%"height="26"background="images/admin_bg_1.gif"><strong><fontcolor="#FFFFFF">--<spanclass="STYLE1">内容管理</span></font></strong></td> </tr> <tr> <tdcolspan="10"> </td> </tr> <trclass="title"> <td height="25"background="images/admin_bg_1.gif"class="t2">| <% Stringop; StringID=request.getParameter("id"); op=request.getParameter("op"); if(op==null){ op="my"; } if(op.equals("del")&&ID!=null){ Stringbbb=newString(request.getParameter("BigClassName").getBytes("iso8859_1")); Statementstmt_ChannelDEL=conn.createStatement(); StringSqldel="deletefromhl_newswhereid="+ID; intshan=stmt_ChannelDEL.executeUpdate(Sqldel); if(shan>0) { out.print("<scripttype="text/javascript">alert("已成功删除!");window.location.href='Hanye_Manages.jsp?BigClassName="+bbb+"';</script>"); } stmt_ChannelDEL.close(); return; } StatementstmtA=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); StringSqlA="Select*fromhl_smallclassWHEREbigname='行业新闻'orderbyidasc"; ResultSetRsA=stmtA.executeQuery(SqlA); while(RsA.next()){ %> <aclass='t2'href='Hanye_Manages.jsp?modi=新闻&BigClassName=<%=RsA.getString("smallname")%>'><%=RsA.getString("smallname")%></a>| <% } RsA.close();//关闭记录集对象 stmtA.close();//关闭语句对象 %> </td> </tr> <tr> <tdheight="20"></td> </tr> <tr> <tdalign="center"valign="top"bgcolor="#ECF5FF"> <formname="form2"action="list.asp?action=delall&modi=新闻"method="post"> <tablewidth="100%"cellpadding="3"cellspacing="2"bgcolor="#FFFFFF"style="word-break:break-all"> <trclass="t2"> <tdwidth="32" height="28"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1">ID</td> <tdwidth="80"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1">新闻大类</td> <tdwidth="88"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1">新闻小类</td> <tdwidth="291"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1"style="word-break:break-all">新闻标题</td> <tdwidth="179"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1">新闻图片</td> <tdwidth="76"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1">发布时间</td> <tdwidth="64"align="center"background="images/admin_bg_1.gif"bgcolor="#BBDAFF"class="STYLE1">操作</td> </tr> <% intpageSize=10; //每页显示的文章数 intshowPage=1; //当前页面显示的页号 intrecCount=0; //ResultSet中的文章总数 intpageCount=0; //分页后的总页数 StringSqlArticle; StringtoPage=request.getParameter("page"); Stringbgname=newString(request.getParameter("BigClassName").getBytes("iso8859_1")); Statementstmt_Article=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); SqlArticle="Select*fromhl_newswheresmallname='"+bgname+"'orderbyIDDESC"; ResultSetRsArticle=stmt_Article.executeQuery(SqlArticle); if(RsArticle.next()){ RsArticle.last();//指针飞到最后 recCount=RsArticle.getRow();//去除最后一行的行号 pageCount=((recCount%pageSize)==0?(recCount/pageSize):(recCount/pageSize)+1);//得到一共可以分多少页 if(toPage!=null) showPage=Integer.parseInt(toPage); RsArticle.absolute((showPage-1)*pageSize+1);//跳到第showpage页的第一条 for(inti=1;i<=pageSize;i++){ intNID=RsArticle.getInt("ID"); Stringimg=RsArticle.getString("img"); Stringttt=RsArticle.getString("times"); //ttt=ttt.substring(0,10); %> <trclass="STYLE1"> <tdwidth="32" height="22"align="center"bgcolor="#BBDAFF"><%=NID%></td> <tdalign="center"bgcolor="#BBDAFF"><%=RsArticle.getString("bigname")%></td> <tdalign="center"bgcolor="#BBDAFF"><%=RsArticle.getString("smallname")%></td> <tdalign="left"bgcolor="#BBDAFF"><%=RsArticle.getString("Title")%></td> <tdalign="center"bgcolor="#BBDAFF"><ahref="/<%=img%>"target="_blank"><%=img%></a></td> <tdalign="center"bgcolor="#BBDAFF"><%=ttt%></td> <tdwidth="64"align="center"bgcolor="#BBDAFF"class="STYLE1"><ahref="Hanye_edit.jsp?id=<%=NID%>&modi=新闻">修改</a>|<ahref="Hanye_Manages.jsp?id=<%=NID%>&op=del&BigClassName=<%=bgname%>"onclick="returnconfirm('删除后将不能恢复,你确定删除么?')">删除</a></td> </tr> <% if(!RsArticle.next()) break; } } %> <inputname='AllID'type='hidden'value=> </table> <tablewidth="100%"border="0"align="center"cellpadding="0"cellspacing="0"> <trclass="STYLE1"> <tdwidth="50%"align="left"background="images/admin_bg_1.gif"> <spanclass="t2"><strong></strong> </span> <label> </label> </td> <tdwidth="56%"align="center"background="images/admin_bg_1.gif"> <divalign="left"class="t2"> 共<%=pageCount%>页<% for(inti=1;i<=pageCount;i++){ %><%if(i==showPage){%><b><%=i%></b><%}else{%>[<ahref='Hanye_Manages.jsp?BigClassName=<%=bgname%>&page=<%=i%>&modi=新闻'><%=i%></a>]<%}}%> </div></td> </tr> </table> </form> <formname="form1"method="post"action="Hanye_Manage.jsp"><tablewidth="51%"border="0"cellspacing="0"cellpadding="0"> <trclass="t4"> <tdwidth="34%"height="23"><spanclass="STYLE1">查询:</span> <label> <selectname="select"> <optionvalue="ID">按ID查询</option> <optionvalue="title">按标题查询</option> <optionvalue="times">按时间查询</option> </select> </label> <label> <inputtype="text"name="k"> <inputname="Submit"type="submit"class="t5"value="提交"> </label> </td> </tr> </table> </form> <br></td> </tr> </table>
<% RsArticle.close(); stmt_Article.close(); conn.close(); %> </body> </html>