我觉得分页还是在数据库那头做 写好sql语句,直接把要选记录排序,定位,传出来<%sSql = "select MOBILE,SENDTIME,sendmsg,rettime,retmsg from (select rownum n,MOBILE,SENDTIME,sendmsg,rettime,retmsg from (select MOBILE,SENDTIME,sendmsg,rettime,retmsg from king_bbs order by sendtime desc)) where n>'"+iFrom+"' and n<='"+iTo+"' order by sendtime desc"; %> 看我这句话是否对你有帮助效率低了点,嘿嘿
我做的TableListTag,一直用没问题。代码共享给大家,里面用到了用户身份验证 查询数据库等类,不在这里公开了,根据自己的情况改改就是了。/* cxt 2002.8.26 Generate a Table */ package com.taglib;import java.io.IOException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import javax.servlet.http.*; import java.sql.*; import com.nordsan.tfcrm.UserObject; import java.util.*; import java.io.InputStream; import org.xml.sax.SAXException; import org.apache.struts.digester.*;import com.nordsan.tfcrm.util.*;public final class TableTag extends TagSupport { private String sqlstatement = ""; //required,SQL String, ex."Select * From Proj_info group by ...." public String getSqlstatement() { return this.sqlstatement; } public void setSqlstatement(String sqlstatement) { this.sqlstatement = sqlstatement; } private String tabletitle = ""; //required,Table Title, ex."title1;title2;title3" public String getTabletitle() { return this.tabletitle; } public void setTabletitle(String tabletitle) { this.tabletitle = tabletitle; } private String haslinenumber = ""; //not required, True(default) or False,If display 序号列 public String getHaslinenumber() { return this.haslinenumber; } public void setHaslinenumber(String haslinenumber) { this.haslinenumber = haslinenumber; } private String tablewidth = ""; //not required,Width of the Table, ex. "760" public String getTablewidth() { return this.tablewidth; } public void setTablewidth(String tablewidth) { this.tablewidth = tablewidth; } private String titlealign = ""; //not required,Alignment of the Title(must equals title element number), ex. "Left;;;Right;;" public String getTitlealign() { return this.titlealign; } public void setTitlealign(String titlealign) { this.titlealign = titlealign; } private String columnwidth = ""; //not required,Width of each Column(must equals title element number,exclude linenumber column),ex. "80;150;;;;;" public String getColumnwidth() { return this.columnwidth; } public void setColumnwidth(String columnwidth) { this.columnwidth = columnwidth; } private String columnalign = ""; //not required,Alignment of each Column(must equals title element number), ex. "Left;;;Right;;" public String getColumnalign() { return this.columnalign; } public void setColumnalign(String columnalign) { this.columnalign = columnalign; } private String currentpage = ""; //not required,Define which page to be shown first(default is page 1), ex. "2" public String getCurrentpage() { return this.currentpage; } public void setCurrentpage(String currentpage) { this.currentpage = currentpage; } private String linesperpage = "15"; //not required,Line Number per Page default is 15, ex "20" public String getLinesperpage() { return this.linesperpage; } public void setLinesperpage(String linesperpage) { this.linesperpage = linesperpage; } private String linkcolumn = ""; //not required,Define which Column Used to be the link Col(now you can only set one link col), ex."title1;Project_id;AddProject.jsp" public String getLinkcolumn() { return this.linkcolumn; } public void setLinkcolumn(String linkcolumn) { this.linkcolumn = linkcolumn; } private String operationcolumn = ""; //not required,Define which Column Used to be the operation Col(now you can only set 1 col with max 5 items), ex."title3;Project_id;删除;delete.jsp;添加;add.jsp" public String getOperationcolumn() { return this.operationcolumn; } public void setOperationcolumn(String operationcolumn) { this.operationcolumn = operationcolumn; } public void ReadString(String [] destination, String source, char separator) { for (int i = 0,j = 0,StartPosition = 0;i < source.length();i++ ) { if (source.charAt(i) == separator && i == source.length() - 1) { destination[j++] = source.substring(StartPosition,i); destination[j] = ""; } else if (source.charAt(i) == separator || i == source.length() - 1) { if (i != source.length()-1) { destination[j++] = source.substring(StartPosition,i); StartPosition = i + 1; } else { destination[j++] = source.substring(StartPosition,i + 1); StartPosition = i; } } } } public int doStartTag() throws JspException { try { String TableHtmlString = ""; String Title[]; String TitleAlignment[]; String ColumnWidth[]; String ColumnAlignment[]; String LinkColumn[]; String OperationColumn[]; int OperationNumber; int CurrentPageNo; int LinesPerPage; int TotalLineNo; int TotalPage; TotalLineNo = (new TableDAO()).GetRecordNumber(sqlstatement); LinesPerPage = Integer.parseInt(linesperpage); TotalPage = TotalLineNo/LinesPerPage + ((TotalLineNo % LinesPerPage == 0)?0:1); if(pageContext.getRequest().getParameter("PageNo") == null) CurrentPageNo = (currentpage.length() > 0)?Integer.parseInt(currentpage):1; else CurrentPageNo = (pageContext.getRequest().getParameter("PageNo").length()>0)?Integer.parseInt(pageContext.getRequest().getParameter("PageNo")):1; if (CurrentPageNo < 1) CurrentPageNo = 1; if (CurrentPageNo > TotalPage) CurrentPageNo = TotalPage;
int ItemNumber = 1; for (int i = 0;i < tabletitle.length();i++ ) if (tabletitle.charAt(i) == ';') ItemNumber ++; Title = new String[ItemNumber]; TitleAlignment = new String[ItemNumber]; ColumnWidth = new String[ItemNumber]; ColumnAlignment = new String[ItemNumber]; LinkColumn = new String[4]; OperationColumn = new String[12]; //Max 5 Operations ,change this number to support more opertaions ReadString(Title, tabletitle, ';'); if (titlealign.length() > 0) ReadString(TitleAlignment, titlealign, ';'); if (columnwidth.length() > 0) ReadString(ColumnWidth, columnwidth, ';'); if (columnalign.length() > 0) ReadString(ColumnAlignment, columnalign, ';');
写好sql语句,直接把要选记录排序,定位,传出来<%sSql = "select MOBILE,SENDTIME,sendmsg,rettime,retmsg from (select rownum n,MOBILE,SENDTIME,sendmsg,rettime,retmsg from (select MOBILE,SENDTIME,sendmsg,rettime,retmsg from king_bbs order by sendtime desc)) where n>'"+iFrom+"' and n<='"+iTo+"' order by sendtime desc";
%>
看我这句话是否对你有帮助效率低了点,嘿嘿
查询数据库等类,不在这里公开了,根据自己的情况改改就是了。/*
cxt 2002.8.26
Generate a Table
*/
package com.taglib;import java.io.IOException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.http.*;
import java.sql.*;
import com.nordsan.tfcrm.UserObject;
import java.util.*;
import java.io.InputStream;
import org.xml.sax.SAXException;
import org.apache.struts.digester.*;import com.nordsan.tfcrm.util.*;public final class TableTag extends TagSupport { private String sqlstatement = ""; //required,SQL String, ex."Select * From Proj_info group by ...."
public String getSqlstatement() { return this.sqlstatement; }
public void setSqlstatement(String sqlstatement) { this.sqlstatement = sqlstatement; } private String tabletitle = ""; //required,Table Title, ex."title1;title2;title3"
public String getTabletitle() { return this.tabletitle; }
public void setTabletitle(String tabletitle) { this.tabletitle = tabletitle; } private String haslinenumber = ""; //not required, True(default) or False,If display 序号列
public String getHaslinenumber() { return this.haslinenumber; }
public void setHaslinenumber(String haslinenumber) { this.haslinenumber = haslinenumber; } private String tablewidth = ""; //not required,Width of the Table, ex. "760"
public String getTablewidth() { return this.tablewidth; }
public void setTablewidth(String tablewidth) { this.tablewidth = tablewidth; } private String titlealign = ""; //not required,Alignment of the Title(must equals title element number), ex. "Left;;;Right;;"
public String getTitlealign() { return this.titlealign; }
public void setTitlealign(String titlealign) { this.titlealign = titlealign; } private String columnwidth = ""; //not required,Width of each Column(must equals title element number,exclude linenumber column),ex. "80;150;;;;;"
public String getColumnwidth() { return this.columnwidth; }
public void setColumnwidth(String columnwidth) { this.columnwidth = columnwidth; } private String columnalign = ""; //not required,Alignment of each Column(must equals title element number), ex. "Left;;;Right;;"
public String getColumnalign() { return this.columnalign; }
public void setColumnalign(String columnalign) { this.columnalign = columnalign; } private String currentpage = ""; //not required,Define which page to be shown first(default is page 1), ex. "2"
public String getCurrentpage() { return this.currentpage; }
public void setCurrentpage(String currentpage) { this.currentpage = currentpage; } private String linesperpage = "15"; //not required,Line Number per Page default is 15, ex "20"
public String getLinesperpage() { return this.linesperpage; }
public void setLinesperpage(String linesperpage) { this.linesperpage = linesperpage; } private String linkcolumn = ""; //not required,Define which Column Used to be the link Col(now you can only set one link col), ex."title1;Project_id;AddProject.jsp"
public String getLinkcolumn() { return this.linkcolumn; }
public void setLinkcolumn(String linkcolumn) { this.linkcolumn = linkcolumn; } private String operationcolumn = ""; //not required,Define which Column Used to be the operation Col(now you can only set 1 col with max 5 items), ex."title3;Project_id;删除;delete.jsp;添加;add.jsp"
public String getOperationcolumn() { return this.operationcolumn; }
public void setOperationcolumn(String operationcolumn) { this.operationcolumn = operationcolumn; } public void ReadString(String [] destination, String source, char separator)
{
for (int i = 0,j = 0,StartPosition = 0;i < source.length();i++ )
{
if (source.charAt(i) == separator && i == source.length() - 1)
{
destination[j++] = source.substring(StartPosition,i);
destination[j] = "";
}
else if (source.charAt(i) == separator || i == source.length() - 1)
{
if (i != source.length()-1)
{
destination[j++] = source.substring(StartPosition,i);
StartPosition = i + 1;
}
else
{
destination[j++] = source.substring(StartPosition,i + 1);
StartPosition = i;
}
}
}
} public int doStartTag() throws JspException {
try
{ String TableHtmlString = "";
String Title[];
String TitleAlignment[];
String ColumnWidth[];
String ColumnAlignment[];
String LinkColumn[];
String OperationColumn[];
int OperationNumber;
int CurrentPageNo;
int LinesPerPage;
int TotalLineNo;
int TotalPage; TotalLineNo = (new TableDAO()).GetRecordNumber(sqlstatement);
LinesPerPage = Integer.parseInt(linesperpage);
TotalPage = TotalLineNo/LinesPerPage + ((TotalLineNo % LinesPerPage == 0)?0:1); if(pageContext.getRequest().getParameter("PageNo") == null)
CurrentPageNo = (currentpage.length() > 0)?Integer.parseInt(currentpage):1;
else
CurrentPageNo = (pageContext.getRequest().getParameter("PageNo").length()>0)?Integer.parseInt(pageContext.getRequest().getParameter("PageNo")):1; if (CurrentPageNo < 1)
CurrentPageNo = 1;
if (CurrentPageNo > TotalPage)
CurrentPageNo = TotalPage;
int ItemNumber = 1;
for (int i = 0;i < tabletitle.length();i++ )
if (tabletitle.charAt(i) == ';')
ItemNumber ++; Title = new String[ItemNumber];
TitleAlignment = new String[ItemNumber];
ColumnWidth = new String[ItemNumber];
ColumnAlignment = new String[ItemNumber];
LinkColumn = new String[4];
OperationColumn = new String[12]; //Max 5 Operations ,change this number to support more opertaions ReadString(Title, tabletitle, ';'); if (titlealign.length() > 0)
ReadString(TitleAlignment, titlealign, ';'); if (columnwidth.length() > 0)
ReadString(ColumnWidth, columnwidth, ';'); if (columnalign.length() > 0)
ReadString(ColumnAlignment, columnalign, ';');
if (linkcolumn.length() > 0)
ReadString(LinkColumn, linkcolumn, ';'); if (operationcolumn.length() > 0)
{
ItemNumber = 1;
for (int i = 0;i < operationcolumn.length();i++ )
if (operationcolumn.charAt(i) == ';')
ItemNumber ++;
if (ItemNumber < 4 || (ItemNumber % 2)!=0 || ItemNumber > 12)
{
operationcolumn = "";
OperationNumber = 0;
}
else
{
ReadString(OperationColumn, operationcolumn, ';');
OperationNumber = ItemNumber/2 - 1;
}
}
else
OperationNumber = 0;
if (tablewidth != "")
TableHtmlString += "width='" + tablewidth + "'";
TableHtmlString += "><TR>"; if (haslinenumber != "False")
TableHtmlString +="<TD class='tabletag_title' width='40' align='center' >序号</TD>"; for (int i = 0;i < Title.length ;i++ ) //Write Table Title
{
TableHtmlString += "<TD class='tabletag_title' ";
if(titlealign != "" && TitleAlignment[i] != "")
TableHtmlString += "align='" + TitleAlignment[i] + "'";
else
TableHtmlString += "align='center' "; if(columnwidth.length() > 0 && ColumnWidth[i].length() > 0)
TableHtmlString += "width='" + ColumnWidth[i] + "'"; TableHtmlString += ">" + Title[i] + "</TD>";
}
TableHtmlString += "</TR>"; //for (int i = 0;i < (CurrentPageNo-1)*LinesPerPage; i++)
// TableRs.next(); ResultSet TableRs = (new TableDAO()).GetResultSet(sqlstatement,(CurrentPageNo-1)*LinesPerPage,LinesPerPage); int LineNumber = (CurrentPageNo-1)*LinesPerPage + 1;
int CurrentPageLineNumber = 1;
while (TableRs.next() && CurrentPageLineNumber <= LinesPerPage)
{
TableHtmlString += "<TR>";
if (!haslinenumber.equals("False")) //Write Line Number
if (LineNumber % 2 == 0)
TableHtmlString += "<TD class='tabletag_content_td_even' width='40' align='center'>" + LineNumber + "</TD>";
else
TableHtmlString += "<TD class='tabletag_content_td_odd' width='40' align='center'>" + LineNumber + "</TD>";
for (int i = 1;i <= Title.length ;i++ ) //Write Table Content
{
if (LineNumber % 2 == 0) //even line & odd line use different css object
TableHtmlString += "<TD class='tabletag_content_td_even' ";
else
TableHtmlString += "<TD class='tabletag_content_td_odd' "; if(columnwidth.length() > 0 && ColumnWidth[i-1].length() > 0) //Write Column Width
TableHtmlString += "width='" + ColumnWidth[i-1] + "' "; if(columnalign.length() > 0 && ColumnAlignment[i-1].length() > 0) //Write Column Alignment
TableHtmlString += "align='" + ColumnAlignment[i-1] + "' "; if (linkcolumn.length() > 0 && Title[i-1].equals(LinkColumn[0]))
{
TableHtmlString += " onMouseOver=\"this.style.background=" + "'#BECFF1'" + "\" "; //Write Link Column
if (LineNumber % 2 == 0)
TableHtmlString += " onMouseOut=\"this.style.background=" + "'#F3F3F3'" + "\">";
else
TableHtmlString += " onMouseOut=\"this.style.background=" + "'#E6E6E6'" + "\">";
TableHtmlString += "<A class='tabletag_a' href=" + LinkColumn[2] + TableRs.getString(LinkColumn[1]);
if (LinkColumn[3] != null)
TableHtmlString += " target='" + LinkColumn[3] + "'>";
else
TableHtmlString += ">";
}
else
TableHtmlString += ">"; if (OperationNumber > 0 && Title[i-1].equals(OperationColumn[0])) //Write Opertaion Column
{
for (int j = 0;j < OperationNumber ;j++ )
{
TableHtmlString += "<A class='tabletag_a' href=" + OperationColumn[2*j+3] + TableRs.getString(OperationColumn[1]) + ">";
TableHtmlString += OperationColumn[2*j+2] + "</A>";
if (j != OperationNumber - 1)
TableHtmlString += " ";
}
}
else
TableHtmlString += TableRs.getString(i); //Write Commom Column if (linkcolumn.length() > 0 && Title[i-1].equals(LinkColumn[0]))
TableHtmlString += "</A>"; TableHtmlString += "</TD>";
}
TableHtmlString += "</TR>";
LineNumber ++;
CurrentPageLineNumber ++;
} if (LineNumber % 2 == 0) //even line & odd line use different css object
TableHtmlString += "<TR><TD class='tabletag_content_td_even' align='right' colspan='" + (haslinenumber.equals("False")?Title.length:Title.length + 1) + "'>";
else
TableHtmlString += "<TR><TD class='tabletag_content_td_odd' align='right' colspan='" + (haslinenumber.equals("False")?Title.length:Title.length + 1) + "'>"; TableHtmlString += "当前是第" + CurrentPageNo + "页 ";
TableHtmlString += "共" + TotalPage + "页 | "; if(CurrentPageNo > 1)
TableHtmlString += "<A class='tabletag_a' href='?PageNo=1'>";
TableHtmlString += "首页";
if(CurrentPageNo > 1)
TableHtmlString += "</A>";
TableHtmlString += "<IMG SRC='../images/arrow-top.gif'/> "; if(CurrentPageNo > 1)
TableHtmlString += "<A class='tabletag_a' href='?PageNo=" + (CurrentPageNo-1) +"'>";
TableHtmlString += "上页";
if(CurrentPageNo > 1)
TableHtmlString += "</A>";
TableHtmlString += "<IMG SRC='../images/arrow-left.gif'/> "; if(CurrentPageNo < TotalPage)
TableHtmlString += "<A class='tabletag_a' href='?PageNo=" + (CurrentPageNo+1) +"'>";
TableHtmlString += "下页";
if(CurrentPageNo < TotalPage)
TableHtmlString += "</A>";
TableHtmlString += "<IMG SRC='../images/arrow-right.gif'/> "; if(CurrentPageNo < TotalPage)
TableHtmlString += "<A class='tabletag_a' href='?PageNo=" + TotalPage +"'>";
TableHtmlString += "末页";
if(CurrentPageNo < TotalPage)
TableHtmlString += "</A>";
TableHtmlString += "<IMG SRC='../images/arrow-down.gif'/> "; if(TotalPage > 1)
TableHtmlString += "<A class='tabletag_a' onClick='tabletag_gotothepage();'>";
TableHtmlString += "跳到页码";
if(TotalPage > 1)
TableHtmlString += "</A>";
TableHtmlString +="<IMG SRC='../images/arrow-jump.gif'/><Input id='tabletag_pageno' size='2' class='tabletag_input'> ";
TableHtmlString += "</TD></TR>";
TableHtmlString += "</Table>"; JspWriter writer = pageContext.getOut();
writer.println(TableHtmlString); String ScriptString = "";
ScriptString += "<Script language=javascript>";
ScriptString += "function tabletag_gotothepage(){if(isNaN(parseInt(tabletag_pageno.value)) || parseInt(tabletag_pageno.value)==0) alert('页码只能是大于0的数字'); else { var hrefstring = 'http://' + window.location.hostname + ':' + window.location.port + window.location.pathname + '?PageNo=' + tabletag_pageno.value;";
ScriptString += "window.location=hrefstring; }}";
ScriptString += "</Script>";
writer.println(ScriptString);
//writer.print("<Script language=JavaScript>");
//writer.print("alert('");
//writer.print(SelectTagScript);
//writer.print("');</Script>");
}
catch (Exception e)
{
throw new JspException ("TableTag - " + e.getMessage());
}
return (EVAL_BODY_INCLUDE);
} public int doEndTag() throws JspException {
return (EVAL_PAGE);
}
}