日期:2014-05-18  浏览次数:20710 次

关于JSP分页的问题
------自定义标签类

package   com.item.tag;

import   java.sql.Connection;
import   java.sql.ResultSet;
import   java.sql.ResultSetMetaData;
import   java.sql.Statement;

import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.jsp.JspException;
import   javax.servlet.jsp.JspWriter;
import   javax.servlet.jsp.PageContext;
import   javax.servlet.jsp.tagext.BodyContent;
import   javax.servlet.jsp.tagext.BodyTag;
import   javax.servlet.jsp.tagext.Tag;

import   com.item.bean.ConnectionDatabase;

public   class   page   implements   BodyTag
{
HttpServletRequest   request   ;
PageContext     context   ;
BodyContent   body   ;

public   void   getPage()
{
Connection   con   =   null;
Statement   st   =   null;
ResultSet   rs   =   null;
ResultSetMetaData   rms   =   null;
JspWriter   out   =   context.getOut();
int   j   =   0;

int   pagesize   =   6;//每页显示的行数
int   pagesum   =   0;//总页数
int   dangqianpage   =   0;
int   sumrow   =   0;//总行数
con   =   ConnectionDatabase.conn();

String   dq   =   request.getParameter( "page ");

if(dq   ==   null)
dangqianpage   =   1;
else
dangqianpage   =   Integer.parseInt(dq);
if(dangqianpage   <   1)
dangqianpage   =   1;

String   sql   =   "select   *   from   [firstItem].[dbo].[userManager] ";
try
{
st   =   con.createStatement();
rs   =   st.executeQuery(sql);
int   cols   =   rms.getColumnCount();

sumrow   =   rs.getRow();//huo   qu   zong   hang   shu  

pagesum   =   (sumrow   +   pagesize   -   1   )/   pagesize   ;//huo   qu   zong   ye   shu  

//如过当前显示的页大于总页数就把当前页设为总页数
if   (dangqianpage   >   pagesum)
dangqianpage   =   pagesum;

out.print( " <table   border   =   2   <tr> ");
for   (int   i   =   1;   i   <=   cols;   i++)
{
out.print( " <th> "   +   rms.getCatalogName(i)   +   " </th> ");

}
out.print( " </tr> ");

if   (dangqianpage   <   0)

//将记录指针定位到待显示页的第一条记录上  
rs.absolute((dangqianpage   -   1   *   dangqianpage   +   1));

while(j   <   dangqianpage   &&   !rs.isAfterLast())
{
out.print( " <tr> ");
for   (int   i   =   1;   i   <   cols   +   1;   i++)
{
out.print( " <td> "   +   rs.getString(i)   +   " </td> ");
}
out.print( " </tr> ");
}