struts+displaytag外部分页


pageList 此类现实paginatedList接口,是display支持的外部分页接口 Java code package com.yourcompany.proxy; import java.util.List; import org.displaytag.pagination.PaginatedList; import org.displaytag.properties.SortOrderEnum; /** */ /** * 分页列表 * *
此类现实paginatedList接口,是display支持的外部分页接口


Java code

package com.yourcompany.proxy;

import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;

/** */
/**
 * 分页列表
 *
 * @author 
 *
 */
public class PageList implements PaginatedList {
    /** */
    /**
     * 每页的列表
     */
    private List list;

    /** */
    /**
     * 当前页码
     */
    private int pageNumber = 1;

    /** */
    /**
     * 每页记录数 page size
     */
    private int objectsPerPage = 15;

    /** */
    /**
     * 总记录数
     */
    private int fullListSize = 0;

    private String sortCriterion;

    private SortOrderEnum sortDirection;

    private String searchId;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getObjectsPerPage() {
        return objectsPerPage;
    }

    public void setObjectsPerPage(int objectsPerPage) {
        this.objectsPerPage = objectsPerPage;
    }

    public int getFullListSize() {
        return fullListSize;
    }

    public void setFullListSize(int fullListSize) {
        this.fullListSize = fullListSize;
    }

    public String getSortCriterion() {
        return sortCriterion;
    }

    public void setSortCriterion(String sortCriterion) {
        this.sortCriterion = sortCriterion;
    }

    public SortOrderEnum getSortDirection() {
        return sortDirection;
    }

    public void setSortDirection(SortOrderEnum sortDirection) {
        this.sortDirection = sortDirection;
    }

    public String getSearchId() {
        return searchId;
    }

    public void setSearchId(String searchId) {
        this.searchId = searchId;
    }

}

 


action


Java code

/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.yourcompany.DBO.UserDBO;
import com.yourcompany.proxy.PageList;

/**
 * MyEclipse Struts
 * Creation date: 03-09-2009
 *
 * XDoclet definition:
 * @struts.action validate="true"
 * @struts.action-forward name="show" path="/test.jsp"
 */
public class PageAction extends Action {
    /*
     * Generated Methods
     */
   
    // 设置每页显示数据数
    private final int PAGE_SIZE = 15;
   
    /**
     * Method execute
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        // TODO Auto-generated method stub
       
        UserDBO dbo=new UserDBO();
       
        // 获取当前页数,displaytag通过参数"page"传递这个值
        int pageNumber;
        if (request.getParameter("page") != null
                && !"".equals(request.getParameter("page"))) {
            pageNumber = Integer.parseInt(request.getParameter("page"));
        } else {
            pageNumber = 1;
        }
       
        PageList pageList = new PageList();
       
        List pages = dbo.getUserList(PAGE_SIZE, (pageNumber-1)*PAGE_SIZE);
        // 设置当前页数
        pageList.setPageNumber(pageNumber);
        // 设置当前页列表
        pageList.setList(pages);
        // 设置page size
        pageList.setObjectsPerPage(PAGE_SIZE);
        // 设置总页数
        pageList.setFullListSize(dbo.getUserSize());
       
        request.setAttribute("pageList", pageList);
       
        return mapping.findForward("show");
    }
}

 

UserDBO

Java code

package com.yourcompany.DBO;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.sql.SqlDBO;
import com.yourcompany.bean.FansBean;

public class UserDBO extends SqlDBO {

    ResultSet rs = null;

    public List getUserList(int start,int end) {

        List userList = new ArrayList();

        this.DBConnection();

        String sql="select top "+start+" * from users where userID not in(select top "+end+" userID from users )";

       
        System.out.println(sql);
       
        long starts=System.currentTimeMillis();
        rs = this.Query(sql);
        long ends=System.currentTimeMillis();
        System.out.println("花费时间:"+(ends-starts)+"毫秒");
        try {
            while (rs.next()) {
                FansBean fans = new FansBean();
                fans.setFans_id(rs.getString("userID").trim());
                fans.setFans_name(rs.getString("userName").trim());
                fans.setPassword(rs.getString("password").trim());
                fans.setFans_sex(rs.getString("sex").trim());
                fans.setFans_date(rs.getString("userDate").trim());
                fans.setFans_city(rs.getString("userCity").trim());
                fans.setFans_Email(rs.getString("userEmail").trim());
                userList.add(fans);
            }
        } catch (Exception e) {
            System.out.println("读取数据出错:" + e);
        }
        this.close();
        return userList;
    }
   
    public int getUserSize(){
        int n=0;
        this.DBConnection();
        long start=System.currentTimeMillis();
        String sql="select count(userID) as sum from users";
        rs=this.Query(sql);
        try{
            while(rs.next()){
                n=Integer.parseInt(rs.getString("sum"));
                break;
            }
        }catch(Exception e){
            System.out.println("取得记录总数出错"+e);
        }
        this.close();
        long end=System.currentTimeMillis();
        System.out.println("花费时间:"+(end-start)+"毫秒");
        System.out.println("记录总数"+n);
        return n;
    }
}

 


jsp页面

HTML code

<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="http://displaytag.sf.net/el" prefix="display"%>
<html>
    <head>
        <title>My JSP 'test.jsp' starting page</title>
        <style type="text/css">
.table {
    border: 1px solid #74B3DC;
    color: #000;
    background: #fff;
    width: 99.7% !important;
    width: 99.5%;
}

.table td,.table th {
    border: 1px solid #e0e0e0;
    border-left: 0px;
    border-top: 0px;
    padding: 0.2em;
}

.table thead th {
    border: 1px solid #e0e0e0;
    border-left: 0px;
    border-top: 0px;
    text-align: left;
    font-size: 1em;
    font-weight: bold;
    background: #d7e9f5;
}
</style>
    </head>

    <body>

        <display:table name="pageList" id="row" class="table" pagesize="20" export="true"
            decorator="org.displaytag.decorator.TotalTableDecorator"
            requestURI="${pageContext.request.contextPath}/page.do">
            <display:column property="fans_id" title="编号" />
            <display:column property="fans_name" title="姓名" />
            <display:column property="fans_sex" title="性别" />
        </display:table>

    </body>
</html> 

posted @ 2010-08-20 15:35  chinaifne  阅读(...)  评论(...编辑  收藏