環(huán)境是JDK1.6+mysql5.0+jboss4.0+struts
主要有三個(gè)東西:show.jsp、ShowAction.java、PageInformation.java
還需完善的地方:如果沒(méi)有前一頁(yè)、后一頁(yè),直接把這個(gè)鏈接在頁(yè)面上屏蔽掉
具體代碼如下:
package com.ClockWise.ray.jsp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
/*
* 此類用來(lái)完成數(shù)據(jù)庫(kù)操作
* */
public class PageInformation {
private int pageSize;//每一頁(yè)包含的條目個(gè)數(shù)
private int totalRows;//一共有多少行
private int totalPages;//一共有多少頁(yè)
private int currentPage=1;//初始化當(dāng)前頁(yè)為第一頁(yè)
private boolean hasPrevious = false;//是否有前頁(yè),尚未使用,有待完善
private boolean hasNext = false;//是否有后頁(yè),尚未使用,有待完善
private ArrayList<User> list = new ArrayList<User>();//存放結(jié)果的列表
private DatabaseGeneralServices dgs;//自己寫(xiě)的獲得connection的類,可以自己實(shí)現(xiàn)
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
public PageInformation(){
dgs = DatabaseGeneralServices.getInstance();
pageSize = 20;//每頁(yè)設(shè)置為20條
totalRows = initRowCount();//得到總行數(shù),比較粗獷,不知有什么靈巧的方法,比如select count(*)...
totalPages =((totalRows+pageSize)-1)/pageSize;//獲得總頁(yè)數(shù)
initList(1);
}
/*
* 每次查詢只取20條,封裝為一個(gè)列表,返回。參數(shù)是當(dāng)前頁(yè)號(hào),在構(gòu)造函數(shù)中默認(rèn)為第一頁(yè)
* */
private void initList(int currentPage){
list.removeAll(list);
conn = dgs.getConnection();
try{
ps = conn.prepareStatement("SELECT * FROM jsptest LIMIT ?,20");
int temp = (currentPage-1)*20;
ps.setInt(1, temp);
rs = ps.executeQuery();
while (rs.next()){
User user = new User();
user.setId(rs.getString(1));
user.setName(rs.getString(2));
list.add(user);
}
}catch(SQLException e){
e.printStackTrace();
}finally{
dgs.closeConnection(rs, ps, conn);
}
}
//粗獷的得到行數(shù)
private int initRowCount(){
conn = dgs.getConnection();
try{
ps = conn.prepareStatement("SELECT * FROM jsptest");
rs = ps.executeQuery();
rs.last();
int result = rs.getRow();
rs.first();
return result;
}catch(SQLException e){
e.printStackTrace();
}finally{
dgs.closeConnection(rs, ps, conn);
}
return 0;
}
//頁(yè)面調(diào)數(shù)據(jù)的時(shí)候,重新發(fā)出查詢,初始化結(jié)果列表
public ArrayList<User> getList(int cp) {
initList(cp);
ArrayList<User> temp = new ArrayList<User>();
for(int i =0;i<20;i++){
temp.add(list.get(i));
}
return temp;
}
//other setters and getters
……
}
緊接著是Action代碼
package com.ClockWise.ray.jsp;
import java.util.ArrayList;
import com.opensymphony.xwork2.ActionSupport;
public class ShowAction extends ActionSupport {
private int totalPages;
private boolean hasPre;
private boolean hasNext;
private int currentPage=1;
private ArrayList<User> list;
//以上幾個(gè)和PageInformation里面的一一對(duì)應(yīng)
private String username ="ray";
private ArrayList list2= new ArrayList();//這個(gè)list用來(lái)存放下面的頁(yè)碼起始位置
private PageInformation pi;
public String execute(){
init(currentPage);
return SUCCESS;
}
private void init(int currentPage){
pi = new PageInformation();
this.setTotalPages(pi.getTotalPages());
this.setHasPre(pi.isHasPrevious());
this.setHasNext(pi.isHasNext());
this.setList(pi.getList(currentPage));
for(int i =currentPage;i<=currentPage+20;i++){
Current c = new Current();
c.setCurrentPage(i);
list2.add(c);
}
}
public ArrayList<User> getList() {
this.setList(pi.getList(currentPage));
return list;
}
//other setters and getters
…….
}
最后是JSP代碼
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="ShowAction" method="GET">
<h1>Welcome <s:property value="username"/></h1><BR>
<h1>CurrentPage <s:property value="currentPage"/></h1>
<!--show items of this page-->
<s:iterator value="list" status="status">
<s:property value="id"/>
<s:property value="name"/>
<BR>
</s:iterator>
<!--define the url of the previous page and next page-->
<s:url id="url_pre" value="ShowAction.action">
<s:param name="currentPage" value="currentPage-1"></s:param>
</s:url>
<s:url id="url_next" value="ShowAction.action">
<s:param name="currentPage" value="currentPage+1"></s:param>
</s:url>
<!-- use url defined above -->
<s:a href="%{url_pre}">Pre</s:a>
<s:iterator value="list2" status="status">
<s:url id="url" value="ShowAction.action">
<!-- pass the currentPage parameter -->
<s:param name="currentPage" value="currentPage"></s:param>
</s:url>
<s:a href="%{url}"><s:property value="currentPage"/> </s:a>
</s:iterator>
<s:a href="%{url_next}">Next</s:a>
</s:form>
</body>
</html>
聯(lián)系客服