說道Struts自然是不能離開MVC模式,分頁顯示也是如此.我不知道我的算法是不是算好的,也希望看過的朋友能發(fā)表一下自己的看法,下面簡單闡述一下主要的開發(fā)思路: 1)建立適當(dāng)?shù)哪P徒M件,對應(yīng)你要查詢數(shù)據(jù)庫中的表,這部分由熟悉的JavaBean來充當(dāng).并在其中建立數(shù)據(jù)庫查詢方法,該方法需要一個java.sql.Conntection類型的參數(shù),并返回一個ArrayList,在本例中為Book.java,另外還有一個數(shù)據(jù)庫連接的Bean是SqlBean.java. 2)建立分頁所需要的模型組件,也是用JavaBean,通過Book提供的ArrayList來構(gòu)造,這里用的是PageBean.java. 3)建立控制器組件,這部分由Struts的Action來實現(xiàn),主要負責(zé)實例化Book,并利用返回的ArrayList對象,構(gòu)造PageBean,以及接收由視圖傳遞來的action參數(shù),從而在PageBean對象中調(diào)用不同的方法,該方法返回Book[]對象,最后將Book[]和PageBean放入到request中.本Action為PageListAction.java. 4)建立視圖組件,這部分JSP來實現(xiàn),為了不出現(xiàn)JAVA代碼,使用Struts提供的標(biāo)簽庫,主要負責(zé)從Request中取出剛剛放入的對象,通過反復(fù)調(diào)用Action以及action參數(shù),而實現(xiàn)分頁顯示,是pagetest.jsp. 5)建立并配置struts-config.xml和web.xml文件. 6)建立數(shù)據(jù)庫. 我對代碼進行了實現(xiàn),調(diào)試通過.如果大家有更好的用Struts實現(xiàn)分頁顯示的算法可以給我留言,代碼如下: struts-config.xml文件如下,這里配置了一個接收haha的請求提交給PageListAction <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts./dtds/struts-config_1_2.dtd"> <struts-config> <action-mappings> <action path="/haha" type="page.PageListAction" scope="request"> <forward name="success" path="/pagetest.jsp"/> </action> </action-mappings> <message-resources parameter="ApplicationResources" /> </struts-config> ………………………………………………………………………………. Web.xml文件如下,這里配置了Web的信息,基本都是由Eclipse完成的 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java./xml/ns/j2ee" xmlns:xsi="http://www./2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java./xml/ns/j2ee http://java./xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app> ………………………………………………………………………… 這個是控制器Action類 package page; import org.apache.struts.action.*; import javax.servlet.http.*; import bean.Book; import java.util.*; import javax.sql.DataSource; public class PageListAction extends Action { ArrayList arrayList=new ArrayList(); PageBean pd; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String action; action=request.getParameter("action"); if(action==null||action.equals("null")) { try { arrayList=Book.getAllBook(); } catch(Exception e) { e.printStackTrace(); } pd=new PageBean(arrayList); Book[] books=pd.getBooks(); request.setAttribute("result",books); request.setAttribute("page",pd); } else { if(action=="nextPage"||action.equals("nextPage")) { Book[] books=pd.getNextPage(); request.setAttribute("result",books); request.setAttribute("page",pd); } if(action=="previousPage"||action.equals("previousPage")) { Book[] books=pd.getPreviousPage(); request.setAttribute("resule",books); request.setAttribute("page",pd); } } return mapping.findForward("success"); } } …………………………………………………………………………………. 這個是pagebean類,主要負責(zé)分頁算法和邏輯處理 package page; import bean.Book; import java.util.*; public class PageBean { int currentPage=1;//當(dāng)前頁數(shù) public int totalPages=0;//總頁數(shù) int pageRecorders=2;//每頁顯示數(shù) int totalRows=0;//總數(shù)據(jù)數(shù) int pageStartRow=0;//每頁的起始數(shù) int pageEndRow;//每頁的終止數(shù) boolean hasNextPage=false;//是否有下一頁 boolean hasPreviousPage=false;//是否有前一頁 ArrayList arrayList; Iterator it; public PageBean(ArrayList arrayList) { this.arrayList=arrayList; totalRows=arrayList.size(); it=arrayList.iterator(); hasPreviousPage=false; currentPage=1; if((totalRows%pageRecorders)==0) { totalPages=totalRows/pageRecorders; } else { totalPages=totalRows/pageRecorders+1; } if(currentPage>=totalPages) { hasNextPage=false; } else { hasNextPage=true; } if(totalRows<pageRecorders) { this.pageStartRow=0; this.pageEndRow=totalRows; } else { this.pageStartRow=0; this.pageEndRow=pageRecorders; } } public void setCurrentPage(int currentPage) { this.currentPage=currentPage; } public void setPageRecorders(int pageRecorders) { this.pageRecorders=pageRecorders; } public void setHasNextPage(boolean hasNextPage) { this.hasNextPage=hasNextPage; } public void setHasPreviosPage(boolean hasPreviosPage) { this.hasPreviousPage=hasPreviousPage; } public String getCurrentPage() { return this.toString(currentPage); } public String getTotalPages() { return this.toString(totalPages); } public String getTotalRow() { return this.toString(totalRows); } public int getPageRecorders() { return pageRecorders; } public int getPageEndRow() { return pageEndRow; } public int getPageStartRow() { return pageStartRow; } public boolean isHasNextPage() { return hasNextPage; } public boolean isHasPreviousPage() { return hasPreviousPage; } public Book[] getNextPage() { currentPage=currentPage+1; if((currentPage-1)>0) { hasPreviousPage=true; } else { hasPreviousPage=false; } if(currentPage>=totalPages) { hasNextPage=false; } else { hasNextPage=true; } Book[] books=getBooks(); return books; } public Book[] getPreviousPage() { currentPage=currentPage-1; if(currentPage==0) { currentPage=1; } if(currentPage>=totalPages) { hasNextPage=false; } else { hasNextPage=true; } if((currentPage-1)>0) { hasPreviousPage=true; } else { hasPreviousPage=false; } Book[] books=getBooks(); return books; } public Book[] getBooks() { if(currentPage*pageRecorders<totalRows) { pageEndRow=currentPage*pageRecorders; pageStartRow=pageEndRow-pageRecorders; } else { pageEndRow=totalRows; pageStartRow=pageRecorders*(totalPages-1); } Book[] books=new Book[pageEndRow-pageStartRow+1]; int j=0; for(int i=pageStartRow;i<pageEndRow;i++) { Book book=(Book)arrayList.get(i); books[j++]=book; } return books; } public String toString(int temp) { String str=Integer.toString(temp); return str; } } ………………………………………………………………………………. Book類,負責(zé)查詢數(shù)據(jù)庫,把結(jié)果放到一個ArrayList中 package bean; import java.sql.*; import java.util.ArrayList; public class Book { private String bookname; private String author; private String price; public Book(String name,String author,String price) { this.bookname=name; this.author=author; this.price=price; } public void setBookname(String bookname) { this.bookname=bookname; } public void setAuthor(String Author) { this.author=author; } public void setPrice(String price) { this.price=price; } public String getBookname() { return bookname; } public String getAuthor() { return author; } public String getPrice() { return price; } public static ArrayList getAllBook() throws Exception { String sql="select * from book"; SqlBean sq=new SqlBean(); ArrayList arrayList=new ArrayList(); try { ResultSet resultSet=sq.select(sql); while(resultSet.next()) { String name=resultSet.getString("name"); String author=resultSet.getString("author"); String price=resultSet.getString("price"); Book book=new Book(name,author,price); arrayList.add(book); } resultSet.close(); } catch(SQLException e) { System.out.println("數(shù)據(jù)庫錯誤"+e.toString()); } return arrayList; } } ……………………………………………………………………………….. 這個是SqlBook,負責(zé)和數(shù)據(jù)庫建立一個連接的Bean package bean; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class SqlBean { String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=eBookStore"; Connection con=null; Statement sta=null; public SqlBean() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); con=DriverManager.getConnection(url,"sa",""); sta=con.createStatement(); } catch(Exception e) { System.out.println("連接錯誤"+e.toString()); } } public ResultSet select(String selects) throws Exception { return sta.executeQuery(selects); } } ………………………………………………………………………… 這個是負責(zé)顯示分頁的JSP頁.pagetest.jsp <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%> <%@ page contentType="text/html;charset=gb2312"%> <html:html locale="true"> <head> </head> <body> <table border="1"> <tr><th>書名</th><th>作者</th><th>價格</th></tr> <logic:present name="result"> <logic:iterate id="book" name="result" type="bean.Book"> <logic:present name="book"> <tr> <td><bean:write name="book" property="bookname"/></td> <td><bean:write name="book" property="author"/></td> <td><bean:write name="book" property="price"/></td> </tr> </logic:present> </logic:iterate> </logic:present> </table> <logic:present name="page"> <logic:equal name="page" property="hasNextPage" value="true"> <html:link page="/haha.do?action=nextPage">nextPage</html:link> </logic:equal> <logic:equal name="page" property="hasPreviousPage" value="true"> <html:link page="/haha.do?action=previousPage">previousPage</html:link> </logic:equal> 共分<bean:write name="page" property="totalPages"/>頁顯示,當(dāng)前是 <bean:write name="page" property="currentPage"/>頁. </logic:present> </body> </html:html> ………………………………………………………………………………… 這個是首頁的JSP頁面,只有一個連接.提交一個haha.do的請求 <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ page contentType="text/html;charset=gb2312" language="java"%> <html:html> <head> </head> <body> <html:link action="haha.do">GotoPage</html:link> </body> </html:html> |
|