TurnPage.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<table align="left" width="100%" border="0" class="midTable1"
cellpadding="3" cellspacing="0">
<tr>
<td width="45%" class="midTable1td1">
每页显示十条记录,总页数:<font color="red">${totalPage}</font>
当前页: <font color="red">${pageNo }</font>
<input type="hidden" name="pageNo" value="${pageNo }">
</td>
<td class="midTable1td1" align="center">
<INPUT type="Button" class=button value="首页"
onclick="first(${pageNo})" />
<INPUT type="Button" class=button value="上一页"
onclick="previous(${pageNo })" />
<INPUT type="Button" class=button value="下一页"
onclick="next(${pageNo },${totalPage})" />
<INPUT type="Button" class=button value="末页"
onclick="last(${pageNo },${totalPage})" />
</td>
</tr>
</table>
=====================================================================================
要用到分页的JSP页面:
<script language="JavaScript">
//分页 第一页
function first(pageNo){
if(pageNo!=1){
document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo=1";
document.fm_msgInfo.submit();
}else{
alert("这已经是第一页");
}
}
//分页 前一页
function previous(pageNo){
if(pageNo>1){
pageNo-=1;
document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo="+pageNo;
document.fm_msgInfo.submit();
}else{
alert("这已经是第一页");
}
}
//分页 下一页
function next(pageNo,totalPage){
if(pageNo<totalPage){
pageNo+=1;
document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo="+pageNo;
document.fm_msgInfo.submit();
}else{
alert("这已经是最后一页");
}
}
//分页 最后一页
function last(pageNo,totalPage){
if(pageNo!=totalPage){
document.fm_msgInfo.action="menu.do?parm=msgInfo&pageNo="+totalPage;
document.fm_msgInfo.submit();
}else{
alert("这已经是最后一页");
}
}
</script>
<body>
在这里引入TurnPage.jsp
<tr>
<jsp:include page="../frame/TurnPage.jsp"/>
</tr>
</body>
===================================================================================
String temp_pageNo = request.getParameter("pageNo");
if(temp_pageNo==null){
temp_pageNo="1";
}
Integer pageNo = 1;
Integer totalPage = 1;
try {
pageNo = Integer.parseInt(temp_pageNo);
} catch (Exception e) {
//
}
try {
MsgDao dao = new MsgDao();
String userID=(String) httpSession.getAttribute("userID");
//设置每页显示的记录数为 10
int page_record=10;
//获取总页数
totalPage = (dao.getRows(userID)+ page_record-1)/page_record;
//System.out.println(totalPage);
//设置总页数到session
httpSession.setAttribute("totalPage", totalPage);
// 删除的是哪一页就显示哪一页 如被删页只有一条数据 就显示最后一页的数据
if (totalPage < pageNo) {
pageNo = totalPage;
}
//查询第 pageNo 页,每页 page_record 条记录 的所有 userID 的短信列表
List msgList=dao.getAllMsg(pageNo,page_record,userID);
httpSession.setAttribute("pageNo", pageNo);
httpSession.setAttribute("msgList", msgList);
return mapping.findForward("msgInfo");
===================================================================================
DAO:
//要查的第pageNo不能为负数
if(pageNo<1){
pageNo=1;
}
Integer notInTop=(pageNo-1)*page_record;
String sql="";
sql="select top "+page_record+" * from msgInfo where msgID not in(select top "+notInTop+" msgID from msgInfo where userID=? order by msgID desc) and userID=? order by msgID desc";
//System.out.println(sql);
ps=con.prepareStatement(sql);
ps.setString(1,userID);
ps.setString(2,userID);
ResultSet rs=ps.executeQuery();
//查出来之后把它放到bean里,再把bean放到一个List里,最后把一个List返回就行了
while(rs.next()){
Msg bean=new Msg();
bean.setMsgID(Integer.toString(rs.getInt(1)));
bean.setPrivyName(rs.getString(2));
al.add(bean);
}