50分求个通用的分页代码,帮顶的也有分,:)谢谢了
郁闷啊,写了老多,结果一提交,该页无法显示,后退,东西没有,
TNND!:(
还是关于分页技术,老实现不了,结果搞来搞去,还是在页面里面嵌代码来实现,实在是让人郁闷,搞了下,试着让他分离出来,结果总搞不定,貌似思路一直都有问题。
求一个,看看大大们怎么实现的,
希望能通用那种,最好用标签实现,然后能定义每页显示多少多少条,
50分给最终能完美解决的朋友,剩下的分给帮顶的朋友,不够的话再加,
先谢了.
PS:这次要是提交再无法显示的话,我就XX CSDN!- -#,还是先copy一下吧,保险点.
[解决办法]
<%@ page language= "java " import= "java.lang.* " pageEncoding= "gb2312 "%>
<%!int rowsPerPage=15; %>
<%@ include file= "connect.jsp " %>
<%
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> 数据库连接 </title>
<meta http-equiv= "pragma " content= "no-cache ">
<meta http-equiv= "cache-control " content= "no-cache ">
<meta http-equiv= "expires " content= "0 ">
<meta http-equiv= "keywords "
content= "keyword1,keyword2,keyword3 ">
<meta http-equiv= "description " content= "This is my page ">
<!--
<link rel= "stylesheet " type= "text/css " href= "styles.css ">
-->
</head>
<body>
<table border= "1 " bgcolor= "#00ff00 " align= "center " name= "table ">
<%
int currentPage=1;
try
{
stmt=con.createStatement
(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql= "select * from course order by course_no desc ";
rs=stmt.executeQuery(sql);
if(rs!=null)
{
rs.last();
int maxRows=rs.getRow();//总共行数
//总页数
int pageCount=(maxRows/rowsPerPage==0)?maxRows/rowsPerPage:
(maxRows/rowsPerPage+1);
String strCurPage=request.getParameter( "curPage ");
if(strCurPage==null)
currentPage=1;
else
{
currentPage=Integer.parseInt(strCurPage);
}
if(currentPage> pageCount)
currentPage=pageCount;
if(currentPage <1)
currentPage=1;
int position=(currentPage-1)*rowsPerPage+1;//光标移动到当前页的第一
行
rs.absolute(position);
%>
<tr>
<td> 课程编号 </td>
<td> 课程名 </td>
<td> 学分 </td>
<td> 教师编号 </td>
<td> 删除 </td>
</tr>
<%
for(int i=0;i <rowsPerPage;i++)
{
String name=rs.getString( "course_no ");
if(rs.getRow()> maxRows) break;
%>
<tr>
<td> <%=rs.getString(1) %> </td>
<td> <%=rs.getString(2) %> </td>
<td> <%=rs.getString(3) %> </td>
<td> <%=rs.getString(4) %> </td>
<td> <a href= "delete.jsp?name= <%=name %> "> 删除 </a> </td>
</tr>
<%
rs.next();
}
}
}catch(SQLException e){
e.printStackTrace();
e.getMessage();
}
%>
</table>
<div align= "center ">
<form action= "changePage.jsp " method= "post ">
<a href= "changePage.jsp?curPage= <%=currentPage-1 %> "> 上一页 </a>
<a href= "changePage.jsp?curPage= <%=currentPage+1%> "> 下一页 </a>
第 <input type= "text " name= "curPage " size= "5 "> 页
<input type= "submit " name= "submit " value= "GO ">
<input type= "submit " name= "excel " value= "save ">
</form> </div>
</body>
</html>
也不是很理想,但还是能实现功能的connect.jsp是和数据库建立连接的
[解决办法]
参考一下吧兄弟!!!
<%@ page contentType= "text/html; charset=GBK " %>
<%@page import= "java.util.ArrayList "%>
<%@page import= "java.util.Iterator "%>
<%@page import= "bean.xxx.bean名 "%>
<jsp:useBean id= "db " scope= "page " class= "database.连接数据库的类 "/>
<jsp:useBean id= "bean自己起的名 " scope= "page " class= "bean.xxx.bean名 "/>
......
<!--分页================================================================-->
<%
String p = request.getParameter( "page ");
int tempPage = 1;
if (p != null)
tempPage = Integer.parseInt(p);
//从会话里得到的 ArrayList
ArrayList list = (ArrayList) session.getAttribute( "放入会话的 ArrayList 名 ");
//如果会话里没有值 也就是第一次到本页面进行的处理 可保证不出现空指针异常
if(list == null){
String sql = "select * from 表名 ";
list = db.自己写的得到ArrayList的方法(sql);
}
int recordsCount = list.size(); //总记录数
// System.out.println(recordsCount);
int pageSize = 10; //每页记录数 可以改成任意数值
int pagesCount = (recordsCount + pageSize - 1) / pageSize; //总页数
// System.out.println(pagesCount);
if (tempPage < 0)
tempPage = 1;
if (tempPage > pagesCount)
tempPage = pagesCount;
int curPage = tempPage; //当前页结束页码
// System.out.println(curPage);
int start = (curPage - 1)*pageSize; //当前页起始页码
// System.out.println(start);
int end = curPage*pageSize + 1; //当前页结束页码
// System.out.println(end);
int i = 0;
Iterator it = list.iterator(); //叠代
while (it.hasNext()) { //it.hasNext()判断有无下一条记录
bean自己起的名 = (bean名) it.next(); //it.next()指针指向下一条
i++;
if (i > start && i < end) {
%>
<!--=====================================================================-->
........
<tr>
<td> <%=bean自己起的名.getXXXXX()%> </td>
</tr>
.......
<!--分页==========================================-->
<%
}
}
%>
<tr bgcolor= "#ffffff ">
<td height= "27 " colspan= "3 "> <div align= "center ">
<%if (curPage != 1) {%>
<a href= "本页面名.jsp?page=1 "> 第一页 </a> <a href= "本页面名.jsp?page= <%=curPage-1%> "> 上一页 </a>
<%
}
if (curPage != pagesCount) {
%>
<a href= "本页面名.jsp?page= <%=curPage+1%> "> 下一页 </a> <a href= "本页面名.jsp?page= <%=pagesCount%> "> 最后一页 </a>
<%}%>
</div> </td>
</tr>
<!--=========================================================-->
[解决办法]
jsp列表下方加上
<%
int startRow = 0,totalRow=0;
try {startRow=Integer.parseInt(request.getParameter( "statrow "));}catch (Exception ex) {}
try {totalRow=Integer.parseInt((String)request.getAttribute( "totalRow "));}catch (Exception ex) {}
PageDivider pageDiv = new PageDivider(selfUrl, "a=a ",totalRow,startRow,15);
%>
<table border= "0 ">
<%=pageDiv.getFontTypeOut()%>
</table>
pageDiv类
package com.sc.util;
/**
* <p> Title:用于分页时简化在jsp页面出现太多java代码 </p>
* <p> Description: </p>
* <p> Copyright: Copyright (c) 2003 </p>
* <p> Company:drgsqd </p>
* @author unascribed
* @version 1.0
*/
public class PageDivider implements java.io.Serializable {
private String _selfPath=null;
private String _paraStr=null;
private int _totalRow=0;
private int _startRow=0;
private int _showRow=0;
/////////////////自身参数
private int _priRow = 0;
private int _nextRow = 0;
private int _endRow = 0;
private int _totalPage = 0;
private int _nowPage = 0;
public PageDivider(String selfPath,String paraStr,int totalRow,int startRow,int showRow) {
this._selfPath = selfPath;
this._paraStr = paraStr;
this._totalRow = totalRow;
this._startRow = startRow;
this._showRow = showRow;
initValues();
}
private void initValues(){
if(this._paraStr==null)this._paraStr= " ";
if(this._selfPath==null)this._selfPath= " ";
if(this._showRow> this._totalRow)this._startRow=0;
if(this._startRow> this._totalRow)this._startRow=this._totalRow;
this._priRow = this._startRow - this._showRow;
this._nextRow = this._startRow + this._showRow;
if(this._totalRow%this._showRow!=0)
this._totalPage = this._totalRow/this._showRow+1;
else this._totalPage = this._totalRow/this._showRow;
this._endRow = this._showRow*(this._totalPage-1);
this._nowPage = this._startRow/this._showRow+1;
}
public String getFontTypeOut(){
StringBuffer outStrBuf=new StringBuffer();
outStrBuf.append( " <tr> <td> ");
outStrBuf.append( " 第 <font color=red> "+this._nowPage+ " </font> 页 / 共 <font color=red> "+this._totalPage+ " </font> 页 共 <font color=red> "+this._totalRow+ " </font> 条 </td> <td> ");
if(this._startRow> 0){
outStrBuf.append( " <a href=\ " "+this._selfPath+ "? "+this._paraStr+ "&statrow=0\ "> 首页 </a> \n ");
outStrBuf.append( " <a href=\ " "+this._selfPath+ "? "+this._paraStr+ "&statrow= "+this._priRow+ "\ "> 上一页 </a> \n ");
}else{
outStrBuf.append( " <font color=#cccccc> 首页 上一页 </font> \n ");
}
outStrBuf.append( " </td> <td> ");
if(this._nowPage <this._totalPage){
outStrBuf.append( " <a href=\ " "+this._selfPath+ "? "+this._paraStr+ "&statrow= "+this._nextRow+ "\ "> 下一页 </a> \n ");
outStrBuf.append( " <a href=\ " "+this._selfPath+ "? "+this._paraStr+ "&statrow= "+this._endRow+ "\ "> 尾页 </a> \n ");
}else{
outStrBuf.append( " <font color=#cccccc> 下一页 尾页 </font> \n ");
}
if(this._totalPage> 1){
outStrBuf.append( " </td> <td> ");
outStrBuf.append( " <script> function pageGoTo(objUrl,objcont,objValue){window.location= objUrl+(objValue*objcont);} </script> ");
outStrBuf.append( "转到第 <SELECT name=\ "page\ " class=\ "select\ " onchange=\ "pageGoTo( ' "+this._selfPath+ "? "+this._paraStr+ "&statrow= ', "+this._showRow+ ",this.value);\ "> ");
for(int i=1;i <=this._totalPage;i++){
outStrBuf.append( " <OPTION "+(this._nowPage==i? "selected ": " ")+ " value= "+(i-1)+ "> "+i+ " </OPTION> ");
}
outStrBuf.append( " </SELECT> 页 ");
}
outStrBuf.append( " </td> </tr> \n ");
return outStrBuf.toString();
}
public String getInputTypeOut(){
String outStr= " ";
outStr+= " ";
return outStr;
}
}
查询语句加上参数startRow,表示页数
[解决办法]
http://community.csdn.net/Expert/TopicView3.asp?id=5212550
我原先写的。
JAVA实现关系数据库的翻页
[解决办法]
/**
* Created by cyril.gu on Mar 24, 2007 12:00:57 PM
* Email:zjjsgwm@163.com
**/
import java.util.List;
public class PageList {
private int currentPage = 1;//当前页码
private int countPage = 0;//总页数
private int pageRows = 0;//每页显示记录数
private int countRows = 0; //总记录数
//private int currentSize;//当前页记录数
private List list;//结果集
public String toString() {
StringBuffer s = new StringBuffer();
s.append( " <input type=\ "hidden\ " id=\ "pageList.currentPage\ " name=\ "pageList.currentPage\ "/> ");
s.append( "第 "+currentPage+ "页/共 "+countPage+ "页 ");
s.append( " 总 "+countRows+ "条记录 ");
/**首页**/
if(this.countPage> 1 && this.currentPage> 1)
s.append( " <a href=\ "javascript:document.getElementById( 'pageList.currentPage ').value= '1 ';doPagination();\ "> 首页 </a> ");
else
s.append( " 首页 ");
/**上一页**/
if(this.currentPage> 1)
s.append( " <a href=\ "javascript:document.getElementById( 'pageList.currentPage ').value= ' "+(this.currentPage-1)+ " ';doPagination();\ "> 上一页 </a> ");
else
s.append( " 上一页 ");
/**下一页**/
if(this.countPage> 1 && this.currentPage <this.countPage)
s.append( " <a href=\ "javascript:document.getElementById( 'pageList.currentPage ').value= ' "+(this.currentPage+1)+ " ';doPagination();\ "> 下一页 </a> ");
else
s.append( " 下一页 ");
/**尾页**/
if(this.countPage> 1 && this.currentPage <this.countPage)
s.append( " <a href=\ "javascript:document.getElementById( 'pageList.currentPage ').value= ' "+this.countPage+ " ';doPagination();\ "> 尾页 </a> ");
else
s.append( " 尾页 ");
/**转到**/
s.append( " 转至: <input type=\ "text\ " id=\ "pageList.toPage\ " name=\ "pageList.toPage\ " value=\ " "+this.currentPage+ "\ " style=\ "width:25px;\ "/> ");
s.append( " <input type=\ "button\ " value=\ "GO\ " onclick=\ "document.getElementById( 'pageList.currentPage ').value=document.getElementById( 'pageList.toPage ').value;doPagination();\ "/ style=\ "width:25px;\ "> ");
s.append( " 每页显示: <input type=\ "text\ " id=\ "pageList.pageRows\ " name=\ "pageList.pageRows\ " value=\ " "+this.pageRows+ "\ " style=\ "width:25px;\ "/> ");
s.append( " <input type=\ "button\ " value=\ "OK\ " onclick=\ "doPagination();\ "/ style=\ "width:25px;\ "> ");
return s.toString();
}
public int getCountPage() {
return countPage;
}
public int getCountRows() {
return countRows;
}
/**
* 判断要跳至的页不能大于总页数
* @return
*/
public int getCurrentPage() {
return currentPage;
}
/**
* 当每页设置数为0时,自动调整为每页显示20
* @return
*/
public int getPageRows() {
if(pageRows==0)
pageRows = 20;
return pageRows;
}
public void setCountPage(int countPage) {
this.countPage = countPage;
}
public void setCountRows(int countRows) {
this.countRows = countRows;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageRows(int pageRows) {
this.pageRows = pageRows;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
[解决办法]
帮顶!!
[解决办法]
我这有一个分页代码,不知道你能用上不
另外你现在作jsp程序用的是什么框架
mvc吗
[解决办法]
是mvc还是struts
[解决办法]
顶!!!
[解决办法]
分页偶课本上就有,但也还是有JAVA代码啊。。
JSP页面没任何JAVA代码?。怎么也有几句~~~帮顶~~~~
[解决办法]
jpager,开源的,只要配置下,不用代码
[解决办法]
很多
最好LZ 写个JAVABEAN
以后用就是啊
[解决办法]
up
[解决办法]
同意上面的,用javabean来做
不过如果是初级的话,可以参考
[解决办法]
分多就是好啊,我没有代码帮你顶一下
记得给分阿 呵呵
[解决办法]
我用过jpager,在tomcat下没有问题,在resin下会抛异常,不知道是不是我配置的原因。
我现在的做法是写一个javaBean输出专门分页代码,用的是代理模式。接口定义如下:
// page接口
public interface I_Page {
// 数据
public List getCollection();
// 全部匹配的数据数
public int getTotalSize();
// 当前第几页
public int getCurrentPage();
// 每页多少数据
public int getPageSize();
// 其他方法
……
}
public interface I_PageProxy extends I_Page {
//
public void setPage(I_Page page);
// 取得页面HTML代码
public String getPageView();
}
然后用一个抽象类实现I_PageProxy除getPageView()以外的方法,还可以写一些辅助方法
public abstract class AbstractPageProxy implements I_PageProxy {
private I_Page page;
// 数据
public List getCollection() {
return this.page.getCollection();
}
// 全部匹配的数据数
public int getTotalSize() {
return this.page.getTotalSize();
}
// 当前第几页
public int getCurrentPage() {
return this.page.getCurrentPage();
}
// 每页多少数据
public int getPageSize() {
return this.page.getPageSize();
}
// 其他方法
……
//
pulic void setPage(I_Page page) {
this.page = page;
}
}
最后用一个具体类继承AbstractPageProxy
public class NumberPageProxy extends AbstractPageProxy {
public String getPageView() {
// 可以有多种实现方式,这里只做示范
return "共 "+ this.getTotalSize() + "条记录,当前第 " + this.getCurrentPage() + "页 ";
}
}
我用的webwork+spring框架,Dao层从数据库中取出的I_Page对象只包含数据,通过本地或者WEBSERVICE传递到Action,Action通过setPageProxy方法从spring容器中得到一个I_PageProxy实例(只要修改ApplicationContext.xml就可以改变所有页面分页显示的逻辑),将I_Page对象用I_PageProxy进行代理,页面再对I_PageProxy进行调用。
<ww:property value= "userList.pageView " escape= "false " /> (好像是这样的)
好处是数据和前台分页逻辑完全分开,坏处是在getPageView()方法中要写html和javascript代码,也就是javaBean和页面耦合。
也想过用自定义标签做,不过现在用的挺顺手,就算了。
[解决办法]
帮你顶,呵呵
[解决办法]
学习
[解决办法]
UP
[解决办法]
想了一个思路:比如说有4条数据,一页2条. 可不可以把每两条的bean对象 分别放到2个链表里,
然后再把两个链表放在一个总的链表里.... 放到session里传给jsp页
[解决办法]
支持,谢谢兄弟的共享
[解决办法]
hubing20082008@163.com
[解决办法]
1.取所有记录求出记录数totle
2.根据每页要显示的记录数acount及要定位的页page将记录集指针定位到相应位置
page*acount+1
3.刷新页面
[解决办法]
jf
[解决办法]
我也有 要的话 发我EMAIL:duzhanyang500@163.com
[解决办法]
mark
顶的好多啊