strut中分处理

来源:互联网 发布:知乎数据挖掘 编辑:程序博客网 时间:2024/04/29 15:20

----------------------分页类-------------------

package entity;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者:何桂坤 注释导入快捷键 Shift+alt+j 时间:2011-1-7
 * 电子邮件:heguikun@foxmail.com QQ:307839227
 * MSN:heguikun@msn.com
 */
@SuppressWarnings("unchecked")
public class PageManage {
 // 初始值都为0,因为有时候数据为空
 int currentPage = 1;// 当前页
 int beforePage = 1;// 上一页
 int nextPage = 1;// 下一页
 int totalPages = 0;// 总页数
 int pagesize = 0;// 每页显示的长度
 int allsize = 0;// 数据总长度
 public PageManage() {// 构造方法
 }
 // 构造方法:数据集合 总数据长度,每页显示的长度,当以的页码
 public PageManage( int allsize, int pagesize, int currentPage) {
  this.allsize = allsize;// 数据总长度
  this.pagesize = pagesize;// 每页显示的长度
  this.currentPage = currentPage;// 当前页
  System.out.println("数据总长度:" + allsize + "/n每页显示的长度:" + pagesize
    + "/n当前页:" + currentPage);
  pm();// 调用方法分页
  System.out.println("调用分页bean运行结束!");
 }

 public void pm() {
   double a = allsize;// 设置为daouble 下面计算总页才正确
   totalPages = (int) Math.ceil(a/pagesize);// 计算总页
   if (1<currentPage&&currentPage < totalPages) {// 当前页<总页
      this.nextPage = this.currentPage + 1;// 设置下一页
      this.beforePage = currentPage - 1;// 设置上一页
       System.out.println("已设置上下一页");
   } else {
    if(currentPage<=1){
      this.beforePage=1;//异常就默认为1
      this.nextPage = 2;// 当前页>最大页,则把它定为最后一页
    }
    if(currentPage>totalPages){
     this.nextPage = totalPages;// 当前页>最大页,则把它定为最后一页
     this.beforePage=totalPages-1;
       }
    if(currentPage==totalPages){
      this.nextPage = 1;// 当前页>最大页,则把它定为最后一页
      this.beforePage=totalPages-1;
        }
     }
  } 
 public int getCurrentPage() {
  return currentPage;
 }
 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }
 public int getBeforePage() {
  return beforePage;
 }
 public void setBeforePage(int beforePage) {
  this.beforePage = beforePage;
 }
 public int getNextPage() {
  return nextPage;
 }
 public void setNextPage(int nextPage) {
  this.nextPage = nextPage;
 }
 public int getTotalPages() {
  return totalPages;
 }
 public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
 }
 public int getPagesize() {
  return pagesize;
 }
 public void setPagesize(int pagesize) {
  this.pagesize = pagesize;
 }
 public int getAllsize() {
  return allsize;
 }
 public void setAllsize(int allsize) {
  this.allsize = allsize;
 }
 // 测试
 public static void main(String[] args) {
  List list = new ArrayList();
  list.add("1");
  list.add("1");
  list.add("1");
  list.add("1");
  list.add("1");
  list.add("1");
  PageManage dd = new PageManage( 26, 5, 5);// 共26条,每页显示5条,当前第5页
  System.out.println("上页" + dd.getBeforePage());
  System.out.println("下页" + dd.nextPage);
  System.out.println("当前页" + dd.currentPage);
  System.out.println("总页数" + dd.totalPages);

 }

}
--------------------------------action---------------------------

public ActionForward ToPxxm(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response) {
    String  Page=request.getParameter("page");//取得要查询的页码
    currentpage= Integer.parseInt(Page==null?"1":Page.trim());//等于空就转为1页
    pagesize=5;//每页显示15条
       Maxsize=dao.getTotalCount("Psxm");//查询总共有多少条信息,出入类的字符串形式
   if (currentpage>1) {
    startsize=(currentpage-1)*pagesize;//开始查找的索引=当前页*没也显示的条数
   }else{startsize=0;currentpage=1;}
    List psxmlist=null;
    try {
     System.out.println("====================开始查询数据================");
   psxmlist = dao.Query("from Psxm", startsize, pagesize);//对象,开始索引,查找长度startsize, pagesize
   
    } catch (Exception e) {
     System.out.println("调用dao.Query(from Psxm, startsize, pagesize)时出错!");
     e.printStackTrace();
    }
    //总数据长度,本页显示的长度,当以的页码//
   PageManage pmManage=new PageManage(Maxsize,pagesize,currentpage);//分页bean
   request.setAttribute("psxmlist", psxmlist);
   request.setAttribute("pmManage", pmManage);//把分页的bean放到请求中
   System.out.println("==================================返回页面==============================");
   return mapping.findForward("pxglPxxm");
  }

---------------------------------页面使用分页---------------------------------

 <!-- -------------------脚部分页---------------------------- -->
              <TR style="BACKGROUND-COLOR: #f0f1f0">
                 <td colspan="12" background="${pageContext.request.contextPath }/page/heguikunPxgl/images/dg_head_bg.gif">
                    <A href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=1"  id="First" title="第一页"  style="FONT-SIZE: medium; FONT-FAMILY: webdings"  >7</A>&nbsp;&nbsp;
                    <A href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=${pmManage.beforePage}" id="Prev" title="上一页" style="FONT-SIZE: medium; FONT-FAMILY: webdings"  >3</A>&nbsp;&nbsp;
                    <A href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=${pmManage.nextPage}" id="_Next" title="下一页" style="FONT-SIZE: medium; FONT-FAMILY: webdings"  >4</A>&nbsp;&nbsp;
                    <A  href="http://localhost:8080/HRMS/pxgl.do?operate=ToPxxm&page=${pmManage.totalPages}" id="Last" title="最后一页"  style="FONT-SIZE: medium; FONT-FAMILY: webdings" >8</A>
                     <B>现在是第</B>
                     <FONT color="#ff0000">${requestScope.pmManage.currentPage}</FONT><B>页,一共有</B>
                     <FONT  color="#0000ff">${requestScope.pmManage.totalPages}</FONT><B>页</B>&nbsp;&nbsp;
                     <B>本页显示条${requestScope.pmManage.pagesize}记录&nbsp;&nbsp共有</B>${requestScope.pmManage.allsize}<B>条记录</B>
                      <B>&nbsp;转到:<B>
                      <INPUT onKeyUp="CheckNumBer(this,${requestScope.pmManage.totalPages})"; style="WIDTH: 40px"  maxLength="6" value="4"  onafterpaste="CheckNumBer(this,${requestScope.pmManage.totalPages});" id="pageNum">
                     <a onClick="sbpage()">
                      <INPUT style="WIDTH: 54px; HEIGHT: 22px" type="image" src="${pageContext.request.contextPath}/page/heguikunPxgl/images/gotopage.gif" align="bottom" border="0"  />
                      </a>
                      </B></B>
                 </td>
               </TR>
                <!-- --------------------脚部分页end -------------------------->

原创粉丝点击