当前页面: 开发资料首页 → J2EE 专题 → JSP分页问题,急。。。
JSP分页问题,急。。。
摘要: JSP分页问题,急。。。
本人用表单提交查询后将数据给JSP显示,分页使用的是limit语句。如何在下一页链接的URL中添加表单的参数,尽量不使用request.getParameter()方法从表单中获取数据来生成动态URL(提交用的是POST方法)。或者推荐一种更好的分页方法(搜索分页)。不胜感谢!
<%!
public List executeQuery(String sql,String[] cols,int pageSize,int nPage) throws SQLException
{
Connection conn=sfca.common.ConnectionManager.getConnection();
Statement stmt=null;
ResultSet rs=null;
ArrayList AL=new ArrayList();
ArrayList returnList=new ArrayList();
int size=cols.length;
int maxPage=1;
try
{
try
{
stmt=conn.createStatement();
try
{
rs=stmt.executeQuery(sql);
while(rs.next())
{
String[] str=new String[size];
for(int i=0;i
{
str[i]=rs.getString(cols[i]);
}
AL.add(str);
}
}
finally
{
rs.close();
rs=null;
}
}
finally
{
stmt.close();
stmt=null;
}
}
finally
{
conn.close();
conn=null;
}
int maxSize = AL.size();
System.out.println("Exception:maxSize:"+maxSize);
int startIndex=(nPage-1)*pageSize;
int endIndex=startIndex+pageSize;
if(startIndex<0)
startIndex=0;
if(endIndex>maxSize)
endIndex= maxSize;
//System.out.println("Exception:startIndex:"+startIndex);
//System.out.println("Exception:endIndex:"+endIndex);
return(AL.subList(startIndex,endIndex));
}
%>
调用:
List list=executeQuery(sql,new String[]{"productid","userid","title","created"},perPage,nPage);
sql是查询语句,String[]是要取出的字段名, perPage是每页显示多少条记录,nPage是当前是第几页。