我的JSp 分页 只能显示第一页数据 数据库里面一共30条记录 分6页显示
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<%
request.setCharacterEncoding("gb2312");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="queryBook" class="example.QueryBook" scope="session" />
<%@ page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>图书馆书库信息</title>
</head>
<body>
<script type="text/javascript">
</script>
<h2>图库中书</h2>
<table width="500" border="1" align="center" id="tab">
<tr>
<td>ISBN</td>
<td>书名</td>
<td>作者</td>
<td>出版社</td>
<td>余量</td>
<td>库存</td>
<td>分类</td>
</tr>
<%
String s="wjj";
s=request.getParameter("pa");
out.println("s = "+s);
int start =0; //从第几条开始显示
if(s != null){
start=Integer.parseInt(s) ;
}else{
start =1;
}
int al= 1; //第几页
int pageSize = 5;//每页显示 数目queryBook.count() / pageSize
int num =6 ;//总页数
%>
一共 <%=num%> 页 ;
当前第<%=al++ %>页    
<%
ResultSet rs = queryBook.query(start, pageSize);
while (rs.next()) {
%>
<tr>
<td><%=rs.getString("ISBN")%></td>
<td><%=rs.getString("Bname")%></td>
<td><%=rs.getString("Bwriter")%></td>
<td><%=rs.getString("Bpress")%></td>
<td><%=rs.getString("Bsurplus")%></td>
<td><%=rs.getString("Bstock")%></td>
<td><%=rs.getString("Bclass")%></td>
</tr>
<%
//
}
// out.println("<a href='queryBook.jsp? page=7'>下一页告诉你</a>");
%>
<a href="queryBook.jsp? pa=5" > 下一页告诉你</a>
</table>
</body>
</html>
求 哪里错l
------解决方案--------------------------------------------------------
s=request.getParameter("pa");
<a href='queryBook.jsp? page=7
你取得pa 不对吧
应该是request.getParameter("page"); !
------解决方案--------------------------------------------------------
他传过去的就是pa!关键要看你的数据库方法,那个query(start, pageSize),问题在那里。
------解决方案--------------------------------------------------------
- Java code
我用分页不是把页号作为数据记录的一项,是把数据取出来然后根据每页显示的条数计算出来有多少页,我的方法参考下吧,不知道是不是你想要的结果:Java codeListAction.javapackage com.sy.action;import java.util.List;import com.opensymphony.xwork2.ActionSupport;import com.sy.dao.AdminDao;import com.sy.dao.NewsDao;import com.sy.dao.impl.AdminDaoImpl;import com.sy.dao.impl.NewsDaoImpl;import com.sy.vo.Admin;import com.sy.vo.News;public class ListAction extends ActionSupport { private static final long serialVersionUID = 1L; int i=1;//中间变量 private int k;//储存最大页面数 private int pageNow=1; //页码数,初始为1 private int pageSize = 5 ; //页面行数 private int intRowCount;//总行数 private int intPageCount;//总页数 private Admin admin; private List<Admin> Adminss; private News news; @SuppressWarnings("unchecked") private List<News> Newss; private int id; private int aid; public News getNews() { return news; } public void setNews(News news) { this.news = news; } @SuppressWarnings("unchecked") public List<News> getNewss() { return Newss; } public void setNewss(List<News> newss) { Newss = newss; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Admin getAdmin() { return admin; } public void setAdmin(Admin admin) { this.admin = admin; } public List<Admin> getAdminss() { return Adminss; } public void setAdminss(List<Admin> adminss) { Adminss = adminss; } public int getAid() { return aid; } public void setAid(int aid) { this.aid = aid; } public int getPageNow() { return pageNow; } public void setPageNow(int pageNow) { this.pageNow = pageNow; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getIntRowCount() { return intRowCount; } public void setIntRowCount(int intRowCount) { this.intRowCount = intRowCount; } public int getIntPageCount() { return intPageCount; } public void setIntPageCount(int intPageCount) { this.intPageCount = intPageCount; } public int getK() { return k; } public void setK(int k) { this.k = k; }@SuppressWarnings("unchecked") @Override//显示新闻列表 public String execute() throws Exception { NewsDao npage=new NewsDaoImpl(); intRowCount=npage.count(); k=(intRowCount + pageSize - 1) / pageSize; intPageCount = (intRowCount + pageSize - 1) / pageSize;//计算出总页数 if(pageNow<1){ pageNow=1; } if(pageNow > intPageCount) pageNow=intPageCount; i = (pageNow -1)*pageSize; NewsDao nlist=new NewsDaoImpl(); if(null!=nlist.queryByPage(i,pageSize)){ Newss = nlist.queryByPage(i,pageSize); return SUCCESS; }else{ return "failure"; } } ..}listNews.jsp<%@ page language="java" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags"%><% 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>My JSP 'index.jsp' starting page</title> </head> <body>.. <center> 共<s:property value="intRowCount"/>记录 第<s:property value="pageNow"/>页 <s:url id="url_pre" value="list.action"> <s:param name="pageNow" value="pageNow-1"></s:param> </s:url> <s:url id="url_next" value="list.action"> <s:param name="pageNow" value="pageNow+1"></s:param> </s:url> <s:iterator value="Newss" status="status"> <s:url id="url" value="list.action"> <s:param name="pageNow" value="pageNow"/> </s:url> </s:iterator> <s:if test="pageNow==1"> <s:a href="%{url_pre}">最前一页</s:a> </s:if> <s:else> <s:a href="%{url_pre}">上一页</s:a> </s:else> <s:if test="pageNow==k"> <s:a href="%{url_next}">最后一页</s:a> </s:if> <s:else> <s:a href="%{url_next}">下一页</s:a> </s:else> </center> </body></html>NewsDao.javapackage com.sy.dao.impl;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import com.sy.dao.NewsDao;import com.sy.util.DataBaseConnection;import com.sy.util.StringUtil;import com.sy.vo.News;public class NewsDaoImpl implements NewsDao { //获取分页新闻列表 @SuppressWarnings("unchecked") public List<News> queryByPage(int i,int pageSize){ List<News> newss=new ArrayList(); PreparedStatement pstmt = null ; String sql = null ; ResultSet rs = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; sql = "select * from struts2new order by id asc limit " + i + "," + pageSize; try { pstmt = dbc.getConnection().prepareStatement(sql); rs = pstmt.executeQuery() ; while(rs.next()) { News news=new News(); news.setId(rs.getInt("id")); news.setName(rs.getString("name")); news.setTitle(rs.getString("title")); news.setDate(rs.getString("date")); news.setEmail(rs.getString("email")); news.setContent(rs.getString("content")); i++; newss.add(news); } rs.close() ; pstmt.close() ; } catch(Exception e) { System.out.println(e) ; } finally { dbc.close(); } return newss; } //查询总行数 public int count() { int intRowCount = 0;//总行数 PreparedStatement pstmt = null ; String sql = null ; ResultSet rs = null ; DataBaseConnection dbc = null ; dbc = new DataBaseConnection() ; sql = "select count(id) from struts2new order by id asc"; try { pstmt = dbc.getConnection().prepareStatement(sql); rs = pstmt.executeQuery(); rs.next();//游标指向第一行 intRowCount=rs.getInt(1);//取得总行数 rs.close() ; pstmt.close() ; } catch(Exception e) { System.out.println(e) ; } finally { dbc.close(); } return intRowCount; }}