邊查資料邊摸索,終于把struts2+ JSP分頁搞定了。
環(huán)境是JDK1.6+mysql5.0+jboss4.0+struts 主要有三個東西:show.jsp,、ShowAction.java,、PageInformation.java 還需完善的地方:如果沒有前一頁,、后一頁,,直接把這個鏈接在頁面上屏蔽掉 具體代碼如下: 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; /* * 此類用來完成數(shù)據(jù)庫操作 * */ public class PageInformation { private int pageSize;//每一頁包含的條目個數(shù) private int totalRows;//一共有多少行 private int totalPages;//一共有多少頁 private int currentPage=1;//初始化當前頁為第一頁 private boolean hasPrevious = false;//是否有前頁,尚未使用,,有待完善 private boolean hasNext = false;//是否有后頁,,尚未使用,有待完善 private ArrayList<User> list = new ArrayList<User>();//存放結(jié)果的列表 private DatabaseGeneralServices dgs;//自己寫的獲得connection的類,,可以自己實現(xiàn) private Connection conn; private PreparedStatement ps; private ResultSet rs; public PageInformation(){ dgs = DatabaseGeneralServices.getInstance(); pageSize = 20;//每頁設置為20條 totalRows = initRowCount();//得到總行數(shù),,比較粗獷,不知有什么靈巧的方法,,比如select count(*)... totalPages =((totalRows+pageSize)-1)/pageSize;//獲得總頁數(shù) initList(1); } /* * 每次查詢只取20條,,封裝為一個列表,返回,。參數(shù)是當前頁號,,在構造函數(shù)中默認為第一頁 * */ 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; } //頁面調(diào)數(shù)據(jù)的時候,重新發(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; //以上幾個和PageInformation里面的一一對應 private String username ="ray"; private ArrayList list2= new ArrayList();//這個list用來存放下面的頁碼起始位置 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> |
|