分页技术
上一篇 / 下一篇 2012-04-24 22:56:28 / 个人分类:jsp
分页其实比较简单,取数时有两种方法,一个是全部取出来,然后根据页数展示相应数据;另一种是只取特定位置数据,需要数据库支持(此处未用)。代码分成两部分,一部分是数据展示,一部分是翻页展示。
1 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
2 <tr>
3 <td height="25"><b><font color="#FF0000">信息列表</font></b></td>
4 </tr>
5 <tr>
6 <td><table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#799AE1">
7 <tr>
8 <form action="newsgl.jsp" method="post" name="toGetBWList" id="Form1">
9 <td height="30" align=left> 搜索:
10 <input name="keyword" type=text value="" class="button1">
11 <input type="submit" name="Submit" value=" 搜 索 " class="button2">
12 </td>
13 <td align=right>
14 <div align="center">
15 <input type="button" name="add" value=" 新 增 " class="button2" onclick="javascript.:window.open('newsmodify.jsp');">
16 <input name=reloadPage type=button value=" 刷 新 " id="reloadPage" onClick="javascript.:window.location.reload();" class="button2">
17 </div></td>
18 </form>
19 </tr>
20 </table>
21 <br>
22 <table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" bordercolorlight="#CCCCCC">
23 <tr bgcolor="#799AE1">
24 <td width="6%"><div align='center'><font color="#FFFFFF">选定</font></div></td>
25 <td height="20" bgcolor="#799AE1"><div align="center"><font color="#FFFFFF">标题</font></div></td>
26 <td width="22%" height="20"><div align="center"><font color="#FFFFFF">作者</font></div></td>
27
28 <td width="7%"><div align="center"><font color="#FFFFFF">修改</font></div></td>
29 </tr>
30 <form name="delnews" action="newsgl.jsp" method=post>
31 <input name="action" type="hidden" value="" id="action">
32 <%
33 int intPageSize; //一页显示的记录数
34 int intRowCount=0; //记录总数
35 int intPageCount=1; //总页数
36 int intPage; //待显示页码
37 String strPage;
38 int i;
39 //设置一页显示的记录数
40 intPageSize = 20;
41 //取得待显示页码
42 strPage = request.getParameter("page");
43 if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
44 intPage = 1;
45 }
46 else{//将字符串转换成整型
47 intPage = java.lang.Integer.parseInt(strPage);
48 if(intPage<1) intPage = 1;
49 }
50 String keyword=request.getParameter("keyword");
51 if(keyword==null)keyword="*";
52 else if(keyword.equals(""))keyword="*";
53 else keyword = "*"+keyword+"*";
54 String action = request.getParameter("action");
55 if((action!=null)&&(action.equals("del"))){//delete items
56 try{
57 Connection con1=DriverManager.getConnection(dburl) ;
58 for(int j=0;j<request.getParameterValues("NewsID").length;j++){
59 PreparedStatement stmt1 = con1.prepareStatement("delete from news where nid=?");
60 stmt1.setInt(1,Integer.parseInt(request.getParameterValues("NewsID")[j]));
61 stmt1.executeUpdate();
62 stmt1.close();
63 }
64 con1.close();
65 }catch(Exception ex){
66 ex.printStackTrace();
67 }
68 }
69 try{
70 String title="";
71 String author="";
72 String content = "";
73 int nid = 0;
74 Connection con=DriverManager.getConnection(dburl) ;
75 PreparedStatement stmt = con.prepareStatement("select * from news",java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
76 ResultSet rst=stmt.executeQuery();
77
78 //获取记录总数
79 rst.last();//??光标在最后一行
80 intRowCount = rst.getRow();//获得当前行号
81 //记算总页数
82 intPageCount = (intRowCount+intPageSize-1) / intPageSize;
83 //调整待显示的页码
84 if(intPage>intPageCount) intPage = intPageCount;
85
86 if(intPageCount>0){
87 //将记录指针定位到待显示页的第一条记录上
88 rst.absolute((intPage-1) * intPageSize + 1);
89
90 int c = 0;
91 while(c<intPageSize && !rst.isAfterLast())
92 {
93 c++;
94 title=rst.getString("title");
95 author=rst.getString("author");
96 content=rst.getString("content");
97 nid=rst.getInt("nid");
98 %>
99 <tr>
100 <td width="6%"><div align="center"><input type="checkbox" name="NewsID" value="<%=nid%>"></div></td>
101 <td height="43%" align="center" style="word-break:break-all;"><a href='newsmodify.jsp?nid=<%=nid %>' class='tt4' target='_blank'><%=title %></a></td>
102 <td width="43%" height="22"><div align="center"><a href='newsmodify.jsp?nid=<%=nid %>' class='tt4' target='_blank'><%=author %></a></div></td>
103 <td width="8%"><div align='center'><a href="newsmodify.jsp?nid=<%=nid %>" class="tt2">修改</a></div></td>
104 </tr>
105 <%
106 rst.next();
107 }
108 }
109 rst.close();
110 stmt.close();
111 con.close();
112 }catch(Exception ex){
113 ex.printStackTrace();
114 }
115 %>
116 </form>
117 </table>
118 <table width="100%" border="0" cellpadding="0" cellspacing="0">
119 <form name="pageform" method="post" action="newsgl.jsp">
120 <tr>
121 <td width="6%" height="25">
122 <div align="center">
123 <input type='checkbox' name='selectCheck' id=selectCheck2 onclick="javascript.:SelectCheckBox();">
124 </div></td>
125 <td width="33%">全部选中
126 <input name="delbtn" value="删除" type="button" class="button2" onclick="javascript.:cdel();">
127
128 </td>
129 <td>
130 <div align="right">
131 共<%=intRowCount %>条信息 当前为:<font color="#FF0000"><%=intPage %></font>/<%=intPageCount %>页
132 <%if(intPage>1){%><a href="newsgl.jsp?page=<%=intPage-1%>&keyword=<%=keyword%>">上一页</a><%}%>
133 <%if(intPage<intPageCount){%><a href="newsgl.jsp?page=<%=intPage+1%>">下一页</a><%}%>
134 跳到第 <input type=text size=3 name="page" value="" class="button1"> 页
135 <input type="hidden" name="keyword" value="<%=keyword%>">
136 <input name="imageField" type="image" src="go.gif" border="0" align="absmiddle">
137 </div></td>
138 </tr>
139 </form>
140 </table></td>
141 </tr>
142 </table>
2 <tr>
3 <td height="25"><b><font color="#FF0000">信息列表</font></b></td>
4 </tr>
5 <tr>
6 <td><table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#799AE1">
7 <tr>
8 <form action="newsgl.jsp" method="post" name="toGetBWList" id="Form1">
9 <td height="30" align=left> 搜索:
10 <input name="keyword" type=text value="" class="button1">
11 <input type="submit" name="Submit" value=" 搜 索 " class="button2">
12 </td>
13 <td align=right>
14 <div align="center">
15 <input type="button" name="add" value=" 新 增 " class="button2" onclick="javascript.:window.open('newsmodify.jsp');">
16 <input name=reloadPage type=button value=" 刷 新 " id="reloadPage" onClick="javascript.:window.location.reload();" class="button2">
17 </div></td>
18 </form>
19 </tr>
20 </table>
21 <br>
22 <table width="100%" border="1" cellspacing="0" cellpadding="0" bordercolordark="#FFFFFF" bordercolorlight="#CCCCCC">
23 <tr bgcolor="#799AE1">
24 <td width="6%"><div align='center'><font color="#FFFFFF">选定</font></div></td>
25 <td height="20" bgcolor="#799AE1"><div align="center"><font color="#FFFFFF">标题</font></div></td>
26 <td width="22%" height="20"><div align="center"><font color="#FFFFFF">作者</font></div></td>
27
28 <td width="7%"><div align="center"><font color="#FFFFFF">修改</font></div></td>
29 </tr>
30 <form name="delnews" action="newsgl.jsp" method=post>
31 <input name="action" type="hidden" value="" id="action">
32 <%
33 int intPageSize; //一页显示的记录数
34 int intRowCount=0; //记录总数
35 int intPageCount=1; //总页数
36 int intPage; //待显示页码
37 String strPage;
38 int i;
39 //设置一页显示的记录数
40 intPageSize = 20;
41 //取得待显示页码
42 strPage = request.getParameter("page");
43 if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据
44 intPage = 1;
45 }
46 else{//将字符串转换成整型
47 intPage = java.lang.Integer.parseInt(strPage);
48 if(intPage<1) intPage = 1;
49 }
50 String keyword=request.getParameter("keyword");
51 if(keyword==null)keyword="*";
52 else if(keyword.equals(""))keyword="*";
53 else keyword = "*"+keyword+"*";
54 String action = request.getParameter("action");
55 if((action!=null)&&(action.equals("del"))){//delete items
56 try{
57 Connection con1=DriverManager.getConnection(dburl) ;
58 for(int j=0;j<request.getParameterValues("NewsID").length;j++){
59 PreparedStatement stmt1 = con1.prepareStatement("delete from news where nid=?");
60 stmt1.setInt(1,Integer.parseInt(request.getParameterValues("NewsID")[j]));
61 stmt1.executeUpdate();
62 stmt1.close();
63 }
64 con1.close();
65 }catch(Exception ex){
66 ex.printStackTrace();
67 }
68 }
69 try{
70 String title="";
71 String author="";
72 String content = "";
73 int nid = 0;
74 Connection con=DriverManager.getConnection(dburl) ;
75 PreparedStatement stmt = con.prepareStatement("select * from news",java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
76 ResultSet rst=stmt.executeQuery();
77
78 //获取记录总数
79 rst.last();//??光标在最后一行
80 intRowCount = rst.getRow();//获得当前行号
81 //记算总页数
82 intPageCount = (intRowCount+intPageSize-1) / intPageSize;
83 //调整待显示的页码
84 if(intPage>intPageCount) intPage = intPageCount;
85
86 if(intPageCount>0){
87 //将记录指针定位到待显示页的第一条记录上
88 rst.absolute((intPage-1) * intPageSize + 1);
89
90 int c = 0;
91 while(c<intPageSize && !rst.isAfterLast())
92 {
93 c++;
94 title=rst.getString("title");
95 author=rst.getString("author");
96 content=rst.getString("content");
97 nid=rst.getInt("nid");
98 %>
99 <tr>
100 <td width="6%"><div align="center"><input type="checkbox" name="NewsID" value="<%=nid%>"></div></td>
101 <td height="43%" align="center" style="word-break:break-all;"><a href='newsmodify.jsp?nid=<%=nid %>' class='tt4' target='_blank'><%=title %></a></td>
102 <td width="43%" height="22"><div align="center"><a href='newsmodify.jsp?nid=<%=nid %>' class='tt4' target='_blank'><%=author %></a></div></td>
103 <td width="8%"><div align='center'><a href="newsmodify.jsp?nid=<%=nid %>" class="tt2">修改</a></div></td>
104 </tr>
105 <%
106 rst.next();
107 }
108 }
109 rst.close();
110 stmt.close();
111 con.close();
112 }catch(Exception ex){
113 ex.printStackTrace();
114 }
115 %>
116 </form>
117 </table>
118 <table width="100%" border="0" cellpadding="0" cellspacing="0">
119 <form name="pageform" method="post" action="newsgl.jsp">
120 <tr>
121 <td width="6%" height="25">
122 <div align="center">
123 <input type='checkbox' name='selectCheck' id=selectCheck2 onclick="javascript.:SelectCheckBox();">
124 </div></td>
125 <td width="33%">全部选中
126 <input name="delbtn" value="删除" type="button" class="button2" onclick="javascript.:cdel();">
127
128 </td>
129 <td>
130 <div align="right">
131 共<%=intRowCount %>条信息 当前为:<font color="#FF0000"><%=intPage %></font>/<%=intPageCount %>页
132 <%if(intPage>1){%><a href="newsgl.jsp?page=<%=intPage-1%>&keyword=<%=keyword%>">上一页</a><%}%>
133 <%if(intPage<intPageCount){%><a href="newsgl.jsp?page=<%=intPage+1%>">下一页</a><%}%>
134 跳到第 <input type=text size=3 name="page" value="" class="button1"> 页
135 <input type="hidden" name="keyword" value="<%=keyword%>">
136 <input name="imageField" type="image" src="go.gif" border="0" align="absmiddle">
137 </div></td>
138 </tr>
139 </form>
140 </table></td>
141 </tr>
142 </table>
其中33-98行是分页数据产生代码;99-115行是循环展示的HTML代码;129-137行是翻页代码。
转载于:http://www.cnblogs.com/badwood316/archive/2012/04/24/2468450.html
TAG:
追随自己的梦想,永不言弃。
标题搜索
日历
|
|||||||||
日 | 一 | 二 | 三 | 四 | 五 | 六 | |||
1 | 2 | 3 | 4 | 5 | 6 | ||||
7 | 8 | 9 | 10 | 11 | 12 | 13 | |||
14 | 15 | 16 | 17 | 18 | 19 | 20 | |||
21 | 22 | 23 | 24 | 25 | 26 | 27 | |||
28 | 29 | 30 |
我的存档
数据统计
- 访问量: 71868
- 日志数: 62
- 文件数: 1
- 书签数: 4
- 建立时间: 2011-01-27
- 更新时间: 2012-06-16