jsp頁面步驟
1.定義頁面位置input類型為hidden的隱藏域名稱為pageIndex初始值為1
2.定義頁面長度
3.通過請求獲得參數的方法傳入pageIndex得到pageIndex字符串
4.定義當前頁面為1
5.進行頁面位置的非空判斷 (1)為空則值為1 (2)否則值為頁面位置的包裝類
6.定義總新聞數,通過實現類的getNewsCount()獲得
7.創建分頁類的對象
8.設置屬性頁面長度,當前頁面,總新聞數,再通過分頁對象的自定義方法得到總頁數
9.進行判斷:當前頁為1以下時,或者大於總頁數
10.定義總頁數input類型為hidden的隱藏域名稱為totalpagecount值為totalpagecont
11.通過c標簽導入頁面,同時用c標簽的name,value屬性傳遞三個參數,總新聞數,當前頁碼,總頁數
接口
//獲取新聞信息總數
public int getNewsCount();
//得到分頁列表
public List<News> getPageNewsList(int pageNum,int pageSize);
實現類
@Override
public int getNewsCount() {
// TODO Auto-generated method stub
int count=0;
String sql="select count(*)as count from news_detail";
Object[] obj={};
if(this.getConnection()){
ResultSet rs=this.executeSQL(sql, obj);
try {
if(rs.next()){
count=rs.getInt("count");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//釋放資源
this.closeResource();
}
}
return count;
}
@Override
public List<News> getPageNewsList(int pageNum, int pageSize) {
// TODO Auto-generated method stub
List<News> newsList=new ArrayList<News>();
String sql="SELECT id,title,author,createDate FROM news_detail ORDER BY id ASC LIMIT ?,?";
pageNum=(pageNum-1)*pageSize;
Object[] params={pageNum,pageSize};
if(this.getConnection()){
ResultSet rs=executeSQL(sql, params);
try {
while(rs.next()){
News news=new News();
news.setId(rs.getInt("id"));
news.setTitle(rs.getString("title"));
news.setAuthor(rs.getString("author"));
news.setCreateDate(rs.getTimestamp("createDate"));
newsList.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//釋放資源
this.closeResource();
}
}
return newsList;
}
對象類
//總頁數
private int totalpageCount=1;
//頁面容量
private int pageSize=0;
//總記錄數
private int totalCount=0;
//當前頁碼
private int currentPageNo=1;
public int getTotalpageCount() {
return totalpageCount;
}
public void setTotalpageCount(int totalpageCount) {
this.totalpageCount = totalpageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
if(totalCount>0){
this.totalCount = totalCount;
this.setTotalPageCountByRs();
}
}
public int getCurrentPageNo() {
return currentPageNo;
}
public void setCurrentPageNo(int currentPageNo) {
if(currentPageNo>0)
this.currentPageNo = currentPageNo;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
if(pageSize>0)
this.pageSize = pageSize;
}
public void setTotalPageCountByRs(){
if(this.totalCount%this.pageSize==0){
this.totalpageCount=this.totalCount/this.pageSize;
}else if(this.totalCount%this.pageSize>0){
this.totalpageCount=this.totalCount/this.pageSize+1;
}else{
this.totalpageCount=0;
}
}
jsp頁面
<%@page import="com.pb.news.entity.News"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.pb.news.util.PageSupport"%>
<%@include file="../../common/common.jsp" %>
<script type="text/javascript">
function addNews(){
window.location="newsDetailCreateSimple.jsp";
}
function page_nav(frm,num){
frm.pageIndex.value=num;
frm.submit();
}
function jump_to(frm,num){
var regexp=/^[1-9]\d*$/;
var totalpagecount= document.getElementById("totalpagecount");
if(!regexp.test(num)){
alert("輸入正整數");
return false;
}else if(num>totalpagecount){
alert("超過總頁數");
return false;
}else{
page_nav(frm,num);
}
}
</script>
<input type="hidden" name="pageIndex" value="1"/>
<%
//頁面容量
int pageSize=5;
//當前頁碼
String pageIndex=request.getParameter("pageIndex");
int currentPageNo=1;
if(pageIndex==null){
currentPageNo=1;
}else{
try{
currentPageNo=Integer.parseInt(pageIndex);
}catch(NumberFormatException e){
response.sendRedirect("error.jsp");
}
}
//總記錄數
int totalCount=newsService.getNewsCount();
//總頁數
PageSupport pageSupport=new PageSupport();
pageSupport.setPageSize(pageSize);
pageSupport.setCurrentPageNo(currentPageNo);
pageSupport.setTotalCount(totalCount);
int totalpagecount=pageSupport.getTotalpageCount();
if(currentPageNo<1){
currentPageNo=1;
}
if(currentPageNo>totalpagecount){
currentPageNo=totalpagecount;
}
List<News> newsList=newsService.getPageNewsList(currentPageNo,pageSize);
int i=0;
for(News news:newsList){
i++;
%>
<tbody>
<input type="hidden" id="totalpagecount" name="totalpagecount" value=<%=totalpagecount %>/>
<tr <%if(i%2!=0){%>class="admin-list-td-h2"<%} %>>
<td><a href='adminNewsView.jsp?id=<%=news.getId() %>'><%=news.getTitle() %></a></td>
<td><%=news.getAuthor()%></td>
<td><%=news.getCreateDate() %></td>
<td><a href='adminNewsCreate.jsp?id=<%=news.getId() %>'>修改</a>
<a href="javascript:if(confirm('確認是否刪除此新聞?')) location='adminNewsDel.jsp?id=<%=news.getId() %>'">刪除</a>
</td>
</tr>
</tbody>
<%
}
%>
</table>
<div class="page-bar">
<ul class="page-num-ul clearfix">
<li>共<%=totalCount %></>條記錄 <%=currentPageNo%>/<%=totalpagecount %>頁</li>
<%if(currentPageNo>1){%>
<a href="javascript:page_nav(document.forms[0],1)">首頁</a>
<a href="javascript:page_nav(document.forms[0],<%=currentPageNo-1%>)">上一頁</a>
<%}if(currentPageNo<totalpagecount){ %>
<a href="javascript:page_nav(document.forms[0],<%=currentPageNo+1%>)">下一頁</a>
<a href="javascript:page_nav(document.forms[0],<%=totalpagecount%>)">最后一頁</a>
<%} %>
</ul>
<span class="page-go-form"><label>跳轉至</label>
<input type="text" name="inputPage" id="inputPage" class="page-key" />頁
<button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
</span>
</div>
</div>