Java交流空间

共同梦想,一起飞翔,明天更美好!

 

struts实现分页管理

这近做个学校网站,用struts写的,所以把其中分页讨论下,在做之前我们的头脑中要有一个大概的框架:也就是我们怎么样来划分各个功能模块。一般来说一个分页功能框架至少要包含如下几个部分(java class)(括号注释对应本实例中的类):
1:页面元素(Order.java)。也就是一个页面要显示的一条条的记录
2:数据(DataCollection.java)。对应一个javaBean,我看有不少前辈将获取数据与页面控制放在一起,
我本人认为这样做不好。一来逻辑不清楚,二来对于一个控制来说应是可重用的,但放在一起就不能重用了。
3:页面页面控制(PageController.java)。也就是对一个页面的定义
4:页面跳转的Action(PageAction.java).因为这个页面跳转相对来说比较简单我们可以直接继承ActionForward
来达到这个目的。
5:用来显示的View.(page.jsp)
*********************************
注:本实例是从一个客户定单中取数据
*********************************

下面的我就各个部分作一个简单的介绍
1:页面元素。也就是一个页面要显示的一条条的记录(Order.java)
这个Order.java没有多大意思它只是一个“定单”的定义
        private int orderID;           定单编号
        private String customerID;        客户编号
        private Date orderDate;                下单日期
***********************************************************
/*
* Created on 2004-9-14
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.toad.pub;
import java.util.Date;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Order {

        private int orderID;                
        private String customerID;
        private Date orderDate;
        /**
         *
         */
        public Order() {
                // TODO Auto-generated constructor stub
        }
        /**
         * @param orderID
         * @param customerID
         * @param orderDate
         */
        public Order(int orderID, String customerID, Date orderDate) {
                this.orderID = orderID;
                this.customerID = customerID;
                this.orderDate = orderDate;
        }
        /**
         * @return Returns the customerID.
         */
        public String getCustomerID() {
                return customerID;
        }
        /**
         * @param customerID The customerID to set.
         */
        public void setCustomerID(String customerID) {
                this.customerID = customerID;
        }
        /**
         * @return Returns the orderDate.
         */
        public Date getOrderDate() {
                return orderDate;
        }
        /**
         * @param orderDate The orderDate to set.
         */
        public void setOrderDate(Date orderDate) {
                this.orderDate = orderDate;
        }
        /**
         * @return Returns the orderID.
         */
        public int getOrderID() {
                return orderID;
        }
        /**
         * @param orderID The orderID to set.
         */
        public void setOrderID(int orderID) {
                this.orderID = orderID;
        }
***********************************************************
2:数据(DataCollection.java).这个部分有您可以依据个人情况来进行
自定义。我已有的环境中是用的连接池.
        private ArrayList list;  用来存放所查询到的数据
        private Connection con; 代表一个数据库连接。这个连接
是通过<jsp:setProperty../>传过来的。在接下来的view中我会作介绍
***********************************************************
/*
* Created on 2004-9-15
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.toad.pub;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class DataCollection {

        private ArrayList list;
        private Connection con;
        /**
         *
         */
        public DataCollection() {
                list=new ArrayList();
        }
        /**
         * @return Returns the con.
         */
        public Connection getCon() {
                return con;
        }
        /**
         * @param con The con to set.
         */
        public void setCon(Connection con) {
                this.con = con;
        }
        /**
         * @return Returns the list.
         */
        public ArrayList getList() {
                return list;
        }
        /**
         * @param list The list to set.
         */
        public void setList() {
                
                if(this.con==null){
                        System.out.println("con is null!");
                        return;
                }
                String syntax="select orderid,customerid,orderdate from orders order by orderid asc";
                int orderID=0;
                String customerID=null;
                Date orderDate=null;
                ResultSet rs=null;
                PreparedStatement pst=null;
                try{
                        pst=con.prepareStatement(syntax);
                        rs=pst.executeQuery();
                        while(rs.next()){
                                orderID=rs.getInt(1);
                                customerID=rs.getString(2);
                                orderDate=rs.getDate(3);
                                list.add(new Order(orderID,customerID,orderDate));
                        }
                }catch(SQLException e){
                        System.out.println("SQLException occur at fetch datas !");
                }finally{
                        try{
                                rs.close();
                                con.close();
                        }catch(SQLException e){
                                System.out.println("SQLException occur at rs and con close() !");
                        }
                }
        }
}
***********************************************************
3:页面页面控制(PageController.java)
        private ArrayList allItems;        保存DataCollection中的list,也就是所有的数据
        private int pageNumber;                当前页的页号
        private int lastIndexOfPage;        当前页的最后一个item(Order)在allItems中的index
        private int itemsPerPage;        每一页的容量
        private int itemsInPage;        当前页的实际items(orders)
        private int lastPageNumber;        为allItems/iemsPerPage or allItems/iemsPerPage+1
        private boolean hasPrevious;        是否为第一页
        private boolean hasNext;        是来为最后一页
***********************************************************
/*
* Created on 2004-9-14
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.toad.pub;
import java.util.ArrayList;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PageController {
        
        private int pageNumber;
        private int lastIndexOfPage;
        private int itemsPerPage;
        private int itemsInPage;
        private int lastPageNumber;
        private boolean hasPrevious;
        private boolean hasNext;
        private ArrayList allItems;
        /**
         * @return Returns the allItems.
         */
        public ArrayList getAllItems() {
                return allItems;
        }
        /**
         * @param allItems The allItems to set.
         */
        public void setAllItems(ArrayList allItems) {
                this.allItems = allItems;
        }
        /**
         * @return Returns the hasNext.
         */
        public boolean isHasNext() {
                return hasNext;
        }
        /**
         * @param hasNext The hasNext to set.
         */
        public void setHasNext() {
                int items=pageNumber*itemsPerPage;
                if(items>=allItems.size()){
                        this.hasNext =false;
                }else{
                        this.hasNext=true;
                }
        }
        /**
         * @return Returns the hasPrevious.
         */
        public boolean isHasPrevious() {
                return hasPrevious;
        }
        /**
         * @param hasPrevious The hasPrevious to set.
         */
        public void setHasPrevious() {
                if(pageNumber==1){
                        this.hasPrevious=false;
                }else{
                        this.hasPrevious=true;
                }
        }
        /**
         * @return Returns the itemsInPage.
         */
        public int getItemsInPage() {
                return this.itemsInPage;
        }
        /**
         * @param itemsInPage The itemsInPage to set.
         */
        public void setItemsInPage() {
                int temp=pageNumber*itemsPerPage;
                if(temp<=allItems.size()){
                        this.itemsInPage=itemsPerPage;
                }else{
                        this.itemsInPage=( allItems.size() - ((pageNumber-1)*itemsPerPage ));
                }
        }
        /**
         * @return Returns the itemsPerPage.
         */
        public int getItemsPerPage() {
                return itemsPerPage;
        }
        /**
         * @param itemsPerPage The itemsPerPage to set.
         */
        public void setItemsPerPage(int itemsPerPage) {
                this.itemsPerPage = itemsPerPage;
        }
        /**
         * @return Returns the pageNumber.
         */
        public int getPageNumber() {
                return pageNumber;
        }
        /**
         * @param pageNumber The pageNumber to set.
         */
        public void setPageNumber(int pageNumber) {
                this.pageNumber = pageNumber;
        }
        
        /**
         * @return Returns the lastIndexOfPage.
         */
        public int getLastIndexOfPage() {
                return lastIndexOfPage;
        }
        /**
         * @param lastIndexOfPage The lastIndexOfPage to set.
         */
        public void setLastIndexOfPage() {
                this.lastIndexOfPage =(pageNumber -1)*itemsPerPage;
        }
        
        /**
         * @return Returns the lastPageNumber.
         */
        public int getLastPageNumber() {
                return lastPageNumber;
        }
        /**
         * @param lastPageNumber The lastPageNumber to set.
         */
        public void setLastPageNumber() {
                if(allItems.size()%itemsPerPage==0){
                        this.lastPageNumber =allItems.size()/itemsPerPage;
                }else{
                        this.lastPageNumber =allItems.size()/itemsPerPage+1;
                }
        }
}
***********************************************************
4:页面跳转的Action(PageAction.java).
请注意PageAction是直接继承于ActionForward
很简单:就是根据action=arg2.getParameter("action").trim();
的返回值来设定pageNubmer和与之相就的设定。
一定不能忘了调用 return super.execute(arg0, arg1, arg2, arg3);
***********************************************************
/*
* Created on 2004-9-14
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.toad.pub;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.ForwardAction;
import com.toad.pub.PageController;

/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class PageAction extends ForwardAction {
        
        /* (non-Javadoc)
         * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
         */
        public ActionForward execute(ActionMapping arg0, ActionForm arg1,
                        HttpServletRequest arg2, HttpServletResponse arg3) throws Exception {
                // TODO Auto-generated method stub
                String action=null;
                HttpSession session=arg2.getSession(true);
                action=arg2.getParameter("action").trim();
                PageController controller=(PageController)session.getAttribute("controller");
                int pageNumber=controller.getPageNumber();
                if(action.compareToIgnoreCase("next")==0){
                        ++pageNumber;
                }else if(action.compareToIgnoreCase("pervious")==0){
                        --pageNumber;
                }else if(action.compareToIgnoreCase("first")==0){
                        pageNumber=1;
                }else if(action.compareToIgnoreCase("last")==0){
                        pageNumber=controller.getLastPageNumber();
                }
                controller.setPageNumber(pageNumber);
                controller.setHasNext();
                controller.setHasPrevious();
                controller.setItemsInPage();
                controller.setLastIndexOfPage();
                return super.execute(arg0, arg1, arg2, arg3);
        }
}
***********************************************************
5:用来显示的View.(page.jsp)
***********************************************************
<%@ page language="java" %>
<%@ taglib prefix="html" uri="/WEB-INF/struts-html.tld"%>
<%@ taglib prefix="bean" uri="/WEB-INF/struts-bean.tld"%>
<%@ taglib prefix="logic" uri="/WEB-INF/struts-logic.tld"%>
//得到连接池对象
<bean:define id="conpool" name="pool" type="com.toad.util.ConnectionPool"></bean:define>
//定义一个javaBean同时设定数据(<% Data.setList();%>)
<jsp:useBean id="Data" class="com.toad.pub.DataCollection" scope="session">
        <jsp:setProperty name="Data" property="con" value="<%=conpool.getcon()%>"/>
        <% Data.setList();%>
</jsp:useBean>
//定义一个PageController同时进行初始化请注意调用顺序!
<logic:notEmpty name="Data" property="list">
        <jsp:useBean id="controller" class="com.toad.pub.PageController" scope="session">
            <jsp:setProperty name="controller" property="allItems" value="<%=Data.getList()%>"/>
                <jsp:setProperty name="controller" property="itemsPerPage" value="20"/>
                <jsp:setProperty name="controller" property="pageNumber" value="1"/>
                <%
                        controller.setHasNext();
                        controller.setHasPrevious();
                        controller.setItemsInPage();
                        controller.setLastIndexOfPage();
                        controller.setLastPageNumber();
                %>
        </jsp:useBean>
</logic:notEmpty>

<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<%int i=0;%>
Page Number:<bean:write name="controller" property="pageNumber" />
<table width="100%">
<tr>
<th align='left'>Row Number</th>
<th align='left'>Order ID</th>
<th align='left'>Customer ID</th>
<th align='left'>Order Date</th>
</tr>
<logic:present name="controller" scope="session">
        <bean:define id="offset" name="controller" property="lastIndexOfPage" type="Integer"></bean:define>
        <bean:define id="length" name="controller" property="itemsInPage" type="Integer"></bean:define>
        <logic:iterate id="order" name="controller" property="allItems" offset="<%=offset.toString()%>" length="<%=length.toString()%>" type="com.toad.pub.Order">
                <tr>
                <td><%=++i%></td>
                <td><bean:write name="order" property="orderID" /></td>
                <td><bean:write name="order" property="customerID" /></td>
                <td><bean:write name="order" property="orderDate" /></td>
                </tr>
        </logic:iterate>
        
        <tr>
        <logic:notEqual name="controller" value="1" property="pageNumber" >
        <td><html:link page="/pageaction.do?action=first">First</html:link></td>
        </logic:notEqual>
        <logic:equal name="controller" property="hasPrevious" value="true">
        <td><html:link page="/pageaction.do?action=pervious">Previous</html:link></td>
        </logic:equal>

        <logic:equal name="controller" property="hasNext" value="true">
        <td><html:link page="/pageaction.do?action=next">Next</html:link></td>
        </logic:equal>

        <bean:define id="lastpagenumber" name="controller" property="lastPageNumber" type="Integer"></bean:define>
        <logic:notEqual name="controller" property="pageNumber" value="<%=lastpagenumber.toString()%>"  >
        <td><html:link page="/pageaction.do?action=last">Last</html:link></td>
        </logic:notEqual>
        
        </tr>
</logic:present>
</table>
</body>
</html>
**************************************************************

posted on 2008-10-15 18:49 郭旺平 阅读(125) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(1)

随笔档案(2)

文章档案(2)

搜索

最新评论

阅读排行榜

评论排行榜