现在的位置: 首页 > 综合 > 正文

一个刚刚测试通过的struts分页方法

2014年01月21日 ⁄ 综合 ⁄ 共 11962字 ⁄ 字号 评论关闭

以前直接用的jsp,现在用struts做项目,必需结合struts分页,网上找了下,结合别人的劳动成果,完成了符合自己需要的struts分页。下面的代码:

首先是dao    BbsContent

/**
 *
 */
package vo;

/**
 * @author Administrator
 *
 */
public class BbsContent {
 private int id;
 private String title;
 private String content;
 private String author;
 private String publishTime;
 private String childrenTitle;
 private String superTitle;
 private String click;
 private String type;
 /**
  * @return author
  */
 public BbsContent() {
  
 }
 public BbsContent(int id,String title,String content,String author,String publishtime,String childrenTitle,String superTitle,String type,String click) {
  this.id=id;
  this.title=title;
  this.content=content;
  this.author=author;
  this.publishTime=publishtime;
  this.childrenTitle=childrenTitle;
  this.superTitle=superTitle;
  this.click=click;
  this.type=type;
  
 }
 public String getAuthor() {
  return author;
 }
 /**
  * @param author 要设置的 author
  */
 public void setAuthor(String author) {
  this.author = author;
 }
 /**
  * @return childrenTitle
  */
 public String getChildrenTitle() {
  return childrenTitle;
 }
 /**
  * @param childrenTitle 要设置的 childrenTitle
  */
 public void setChildrenTitle(String childrenTitle) {
  this.childrenTitle = childrenTitle;
 }
 /**
  * @return click
  */
 public String getClick() {
  return click;
 }
 /**
  * @param click 要设置的 click
  */
 public void setClick(String click) {
  this.click = click;
 }
 /**
  * @return content
  */
 public String getContent() {
  return content;
 }
 /**
  * @param content 要设置的 content
  */
 public void setContent(String content) {
  this.content = content;
 }
 /**
  * @return id
  */
 public int getId() {
  return id;
 }
 /**
  * @param id 要设置的 id
  */
 public void setId(int id) {
  this.id = id;
 }
 /**
  * @return publishTime
  */
 public String getPublishTime() {
  return publishTime;
 }
 /**
  * @param publishTime 要设置的 publishTime
  */
 public void setPublishTime(String publishTime) {
  this.publishTime = publishTime;
 }
 /**
  * @return superTitle
  */
 public String getSuperTitle() {
  return superTitle;
 }
 /**
  * @param superTitle 要设置的 superTitle
  */
 public void setSuperTitle(String superTitle) {
  this.superTitle = superTitle;
 }
 /**
  * @return title
  */
 public String getTitle() {
  return title;
 }
 /**
  * @param title 要设置的 title
  */
 public void setTitle(String title) {
  this.title = title;
 }
 /**
  * @return type
  */
 public String getType() {
  return type;
 }
 /**
  * @param type 要设置的 type
  */
 public void setType(String type) {
  this.type = type;
 }

}

下面是分页的主要bean                 BbsThemePageBean

 package util;
/*
 * @author exceljava   xjj
 *
 */
import java.util.*;
import vo.BbsContent;

public class BbsThemePageBean{
    int currentPage=1;//当前页数
    public int totalPages=0;//总页数

    int pageRecorders=2;//每页显示数

    int totalRows=0;//总数据数

    int pageStartRow=0;//每页的起始数

    int pageEndRow=0;//每页的终止数

    boolean hasNextPage=false;//是否有下一页

    boolean hasPreviousPage=false;//是否有前一页

    ArrayList arrayList;

    Iterator it;
    public BbsThemePageBean(ArrayList arrayList){
     System.out.println("page初始化");
        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;// 每页起始数0

            this.pageEndRow=totalRows;// 每页终止数为总数据数

        }

        else {

            this.pageStartRow=0;

            this.pageEndRow=pageRecorders;// 每页终止数为每页显示数

        }
        System.out.println("page初始化结束");

    }

   

    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 hasPreviousPage) {

        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 BbsContent[] getNextPage() {
    System.out.println("getNextPage被调用");
        currentPage=currentPage+1;

        if((currentPage-1)>0) {//是否为第一页

            hasPreviousPage=true;

        }

        else {

            hasPreviousPage=false;

        }

        if(currentPage>=totalPages) {//是否为最后一页

            hasNextPage=false;

        }

        else {

            hasNextPage=true;

        }

        BbsContent[] bbscontent=this.getBbsContent();
        System.out.println("getNextPage被调用结束");
        return bbscontent;
       

    }

   

    public BbsContent[] getPreviousPage() {
     System.out.println("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;

        }

        BbsContent[] bbscontent=this.getBbsContent();
        System.out.println("getPreviousPage()被调用结束");
        return bbscontent;

    }

   

    public BbsContent[] getBbsContent() {
     System.out.println("getBbsContent()被调用");
        if(currentPage*pageRecorders<totalRows) {

            pageEndRow=currentPage*pageRecorders;

            pageStartRow=pageEndRow-pageRecorders;

        }

        else {

            pageEndRow=totalRows;

            pageStartRow=pageRecorders*(totalPages-1);

        }

        BbsContent[] bbscontent=new BbsContent[pageEndRow-pageStartRow+1];

        int j=0;

        for(int i=pageStartRow;i<pageEndRow;i++) {

         BbsContent BbsContent=(BbsContent)arrayList.get(i);

            bbscontent[j++]=BbsContent;

        }
        System.out.println("getBbsContent()被调用结束");
        return bbscontent;

    }
    public String toString(int temp) {

        String str=Integer.toString(temp);

        return str;

    }

 

 /**
  * @return totalRows
  */
 public int getTotalRows() {
  return totalRows;
 }

 

 /**
  * @param totalRows 要设置的 totalRows
  */
 public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
 }

}

下面action

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

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

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 util.BbsThemePageBean;
import util.DataConnection;
import vo.BbsContent;

/**
 * MyEclipse Struts
 * Creation date: 08-01-2007
 *
 * XDoclet definition:
 * @struts.action validate="true"
 * @struts.action-forward name="success" path="/bbs/themesDetail.jsp"
 */
public class ForwardThemeDetailAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 ArrayList arraylist=new ArrayList();
 BbsThemePageBean pb;
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  String action=request.getParameter("action");
  if(action==null||action.equals("")) {//如果为空则有title传过来,取得title
  String title="";//得到穿过来的title值作为参数
  try {
  title=new String(request.getParameter("title").getBytes("ISO8859_1"),"UTF-8");//解决乱码
  }catch(UnsupportedEncodingException e){
   e.printStackTrace();
  }
  //System.out.println(title);
  ArrayList list=new ArrayList();
  String sql="select * from bbscontent where childrenTitle='"+title+"'";//条件查询
  DataConnection dc=new DataConnection();
  try {
   ResultSet rs=dc.executeQuery(sql);
   while(rs.next()) {
    BbsContent bbc=new BbsContent();
    bbc.setTitle(rs.getString("title"));
    bbc.setAuthor(rs.getString("author"));
    bbc.setPublishTime(rs.getString("publishTime"));
    //System.out.println(rs.getString("title"));
    //System.out.println(rs.getString("author"));
    //System.out.println(rs.getString("publishTime"));
    list.add(bbc);//得到list
    request.getSession().setAttribute("storeList",list);//保存该list,以便下面翻页的时候使用
   }
   //下面是没有翻页时取得内容集合
   if(list.size()==0) {//解决如果不存在记录,会出现的数组越界问题
    request.setAttribute("detail",list);
    return mapping.findForward("success");
   }
      //如果至少存在一条记录则启用分页方法
   arraylist=list;
         pb=new BbsThemePageBean(arraylist);
      BbsContent[] bc=pb.getBbsContent();
      request.setAttribute("detail",bc);
      request.setAttribute("page",pb);
       
      
      }catch(SQLException e) {
    e.printStackTrace();
   }
  
     //下面实现分页
     //System.out.println("i="+list.size());
    
  }
    else {

            if(action=="nextPage"||action.equals("nextPage")) {
               arraylist=(ArrayList)request.getSession().getAttribute("storeList");//直接从上面设置的session中取得list,对pb进行初始化
            pb=new BbsThemePageBean(arraylist);
               BbsContent[] books=pb.getNextPage();
               request.setAttribute("detail",books);
               request.setAttribute("page",pb);
               return mapping.findForward("success");

            }
            if(action=="previousPage"||action.equals("previousPage")) {
             arraylist=(ArrayList)request.getSession().getAttribute("storeList");//直接从上面设置的session中取得list,对pb进行初始化
               pb=new BbsThemePageBean(arraylist);
                BbsContent[] books=pb.getPreviousPage();
                request.setAttribute("detail",books);
                request.setAttribute("page",pb);
                return mapping.findForward("success");

            }

        }
     return mapping.findForward("success");
 }
}

下面的jsp页面的主要部分                ThemeDetail.jsp

 <logic:present name="detail" scope="request">
                         <logic:iterate id="de" name="detail">
                         <logic:present name="de">
                        <tr>
                          <td width="4%" height="22" align="center" valign="middle" bgcolor="#FFFFFF"><img src="images/em01.gif" width="19" height="19" /></td>
                          <td height="22" align="left" valign="middle" bgcolor="#FFFFFF"> <html:link page="/forwardTitleDetail.do" paramId="title" paramName="de" paramProperty="title"><bean:write name="de" property="title" filter="false"/></html:link></td>
                          <td width="11%" height="22" align="center" valign="middle" bgcolor="#FFFFFF"><bean:write name="de" property="author"/></td>
                          <td width="21%" height="22" align="center" valign="middle" bgcolor="#FFFFFF">暂时无回复</td>
                          <td width="11%" height="22" align="center" valign="middle" bgcolor="#FFFFFF"><bean:write name="de" property="publishTime"/></td>
                        </tr>
                        </logic:present>
                         </logic:iterate>
                        </logic:present>
                    </table>
                      <table width="978" border="0" align="center" cellpadding="0" cellspacing="0">
                        <tr>
                          <td>&nbsp;</td>
                        </tr>
                        <tr>
                         <logic:present name="page" scope="request"> <td height="25" align="center">共有主题<bean:write name="page" property="totalRows"/>  共分<bean:write name="page" property="totalPages"/> 页  当前是第<bean:write name="page" property="currentPage"/> 页  <logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/forwardThemeDetail.do?action=nextPage">下一页</html:link>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/forwardThemeDetail.do?action=previousPage">上一页</html:link>
</logic:equal>
</logic:present>

注:该分页与网上其他的不同之出是,进入的页面的传了参数才进入的,所以就有通过判断传的action,判断是否需要取得参数title,并且取得的集合只在第一次取得,然后保存在session中,供翻页的时候调用,否则,需要在翻页的时候传两个参数,一个翻页的action和一个供查询用的title,那样的话比较麻烦。

该分页有很大的通用行,对无参进入页面的话只许删除少许代码即可实现。

抱歉!评论已关闭.