定义数据库映射对象
把数据全查出来,每条数据一个数据库映射对象,再把这些对象放入ArrayList
ArrayList要在form里面定义然后写一个标签,对form里面的ArrayList里面的数据进行分页显示

解决方案 »

  1.   

    http://jsptags.com/tags/navigation/pager/index.jsp
      

  2.   

    《如何在Struts 数据库应用程序中实现分页显示》:http://www.csdn.net/Develop/list_article.asp?author=kui&myclassid=0&myclassName=未归类http://dev.csdn.net/develop/article/29/29139.shtm
      

  3.   

    <%@ page import="com.astar.cop.vo.Page,
                     com.astar.cop.web.WebUtil,
                     com.astar.cop.util.StringUtil"%>
    <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
    <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
    <%@ taglib uri="/WEB-INF/cop.tld" prefix="cop" %> <%if(true){%>
    <%
        //get parameter, initial some variable
        Page resultPage_navigation = (Page)request.getAttribute("searchResultPage");
        int totalRecordNum_navigation = resultPage_navigation.getTotalRecordNum();
        int totalPageNum_navigation = resultPage_navigation.getTotalPageNum();
        int currentPageNo_navigation = resultPage_navigation.getCurrentPageNo();    String errMsg = WebUtil.getResource("common.search.error.page");    //set a flog, indicate how many times this page have bean included. and use this to generate page txt box value.
        String indicate = (String)request.getAttribute("INDICATE_FLAG");
        if(StringUtil.isEmpty(indicate))
        {
            indicate="0";
        }
        int intInd = Integer.parseInt(indicate);
        intInd++;
        indicate = String.valueOf(intInd);
        indicate = indicate.trim();
        request.setAttribute("INDICATE_FLAG",indicate);%>
    <table border="0" cellspacing="1" cellpadding="0" width="98%" align="center" class="pageTable">
      <tr>
        <td>
          <table width="100%" border="0" cellspacing="1" cellpadding="1" align="center" class="defaultBGColor">
              <tr class="pageTableTRBGColor">
                <td width="15%" align="center">
                  <p><bean:message key="common.search.label.total"/><span class="hot"><%=totalRecordNum_navigation%></span><bean:message key="common.search.label.row"/><bean:message key="common.search.label.record"/></p>
                </td>
                <td width="10%" align="center"><bean:message key="common.search.label.total"/><span class="hot"><%=totalPageNum_navigation%></span><bean:message key="common.search.label.page"/></td>
                <td width="10%" align="center"><bean:message key="common.search.label.current"/><span class="hot"><%=currentPageNo_navigation%></span><bean:message key="common.search.label.page"/></td>
                <td width="10%" align="center"><input type="button" value=' <bean:message key="common.search.label.tofirstpage"/> ' onclick="gotoPage('1','<%=errMsg%>');" onmouseover="this.style.cursor='hand'" class="editButton"></td>
                <td width="10%" align="center">
                    <logic:lessEqual name="searchResultPage" property="currentPageNo" value="1" >
    <input type="button"  value=' <bean:message key="common.search.label.tobeforepage"/> ' class="noeditButton">
                    </logic:lessEqual>
                    <logic:greaterThan name="searchResultPage" property="currentPageNo" value="1" > 
                        <input type="button" value=' <bean:message key="common.search.label.tobeforepage"/> ' onclick="gotoPage('<%=currentPageNo_navigation-1%>','<%=errMsg%>');" onmouseover="this.style.cursor='hand'" class="editButton">
                    </logic:greaterThan>
                </td>
                <td width="10%" align="center">
                    <logic:greaterEqual name="searchResultPage" property="currentPageNo" value="<%=String.valueOf(totalPageNum_navigation)%>">
    <input type="button"  value=' <bean:message key="common.search.label.tonextpage"/> ' class="noeditButton">
                    </logic:greaterEqual>
                    <logic:lessThan name="searchResultPage" property="currentPageNo" value="<%=String.valueOf(totalPageNum_navigation)%>">
            <input type="button"  value=' <bean:message key="common.search.label.tonextpage"/> ' onclick="gotoPage('<%=currentPageNo_navigation+1%>','<%=errMsg%>');" onmouseover="this.style.cursor='hand'"  class="editButton">
                    </logic:lessThan>
                </td>
                <td width="10%" align="center"><input type="button"  value=' <bean:message key="common.search.label.tolastpage"/> ' onclick="gotoPage('<%=totalPageNum_navigation%>','<%=errMsg%>');" onmouseover="this.style.cursor='hand'" class="editButton"></td>
                <td><bean:message key="common.search.label.topage"/>:<input type="text" name="current_<%=indicate%>" size="4" class="editText">
    <input type="button"  value=' <bean:message key="common.search.label.gotopage"/> '  align="absmiddle"  onclick="gotoPage(document.forms[0].current_<%=indicate%>.value,'<%=errMsg%>');" onmouseover="this.style.cursor='hand'" class="editButton"></td>
              </tr>
          </table>
        </td>
      </tr>
    </table>
    <%}%>
      

  4.   

    <logic:iterate id="allPeople" name="peoplelist" property="list"  type="com.tsf.project.form.PeopleForm">.....
    </logic:iterate> 
    <br>
    <table width="90%" align="center">
    <tr>    
    <td > 
      <div align="center"> 
           <html:button styleClass="button" property="addnew" onclick="submitForm(this.form, this)"><bean:message key="ui.new"/>
       </html:button>
           <html:button styleClass="button" property="edit" onclick="submitForm2(this.form, this)"><bean:message key="ui.detai"/>
       </html:button>
       <html:button styleClass="button" property="close" onclick="OKButton()"><bean:message key="ui.ok"/>
       </html:button>
       <%@ include file="Pages.js" %>
      </div>
    </td>
    </tr>
    </table>
    pages.js
    <SCRIPT language="JavaScript">
    function goPage( formobj , btn){
        var action='<%=request.getParameter("action")%>';
       
        if((action!="list")&&(action!="edit")){
         action="edit"
        }     
    if (btn.name=="firstpage")
    formobj.destinationpage.value=1
    else if (btn.name=="prevpage")
    {
    formobj.destinationpage.value=parseInt(formobj.currentpage.value)-1
    if(parseInt(formobj.destinationpage.value)<1)
      formobj.destinationpage.value=1
    }
    else if (btn.name=="nextpage")
    {
    formobj.destinationpage.value=parseInt(formobj.currentpage.value)+1
    if(parseInt(formobj.destinationpage.value)>parseInt(formobj.totalpage.value))
    formobj.destinationpage.value=parseInt(formobj.totalpage.value)
    }
    else
        formobj.destinationpage.value=parseInt(formobj.totalpage.value)
        
    formobj.action='<%=(request.getContextPath()+request.getAttribute("Path"))%>'+"?action="+action
    var noshow=window.dialogArguments

            if (noshow){
            window.returnValue="destinationpage=" + formobj.destinationpage.value
            window.parent.close()
            }else
            formobj.submit()
    }

    </SCRIPT>

    <html:hidden property="currentpage"/>
    <html:hidden property="destinationpage"/>
    <html:hidden property="totalpage"/>
    <p>
       <html:button styleClass="button" property="firstpage" onclick="goPage(this.form, this)"><bean:message key="firstpage"/>
       </html:button>
       <html:button styleClass="button" property="prevpage" onclick="goPage(this.form, this)"><bean:message key="prevpage"/>
       </html:button>
       <html:button styleClass="button" property="nextpage" onclick="goPage(this.form, this)"><bean:message key="nextpage"/>
       </html:button>
       <html:button styleClass="button" property="lastpage" onclick="goPage(this.form, this)"><bean:message key="lastpage"/>
       </html:button>
      
         </p>
      <SCRIPT language="JavaScript">
         var currentpage= parseInt(document.all("currentpage").value)
       var totalpage= parseInt(document.all("totalpage").value)
       if(totalpage<=1)
          {
       document.all("firstpage").disabled=true
     document.all("nextpage").disabled=true
     document.all("prevpage").disabled=true
       document.all("lastpage").disabled=true
          }
        else if(currentpage==1)
         {
       document.all("firstpage").disabled=true
     document.all("prevpage").disabled=true
          }
          else if(currentpage==totalpage)
          {    
       document.all("nextpage").disabled=true
     document.all("lastpage").disabled=true
         }
        
    </SCRIPT>
    后台:你可以根據currentpage,destinationpage,totalpage這三個參數的值,來確定到數據庫裏讀哪些紀錄.如下寫法:
    private int currentpage = 1;
    private int destinationpage = 1;
    //每页显示的个数
    private int recordCountPerPage = 5;
    private int totalpage = 0;
    /* (non-Javadoc)
     * @see com.tsf.project.form.design.ProjectListFormDesign#listAll(java.sql.Connection)
     */
    public void listAll(Connection con) throws SQLException {
    PreparedStatement pstmt = null;
    ResultSet rst = null;
    ProjectForm pfd = null;
    ArrayList list = new ArrayList();   
    String sql =
    "SELECT p.id as ID,p.projectid as PID,p.name as NAME,p.simpleName as SN"
    + ",p1.name as PMNAME,ps.name as PStatus,p.status as PStatusID"
    + " FROM project p,people p1,projectStatus ps"
    + " WHERE p.PM=p1.TSFID and p.status=ps.id order by p.id desc";
    try {
    pstmt = con.prepareStatement(sql,java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,
    java.sql.ResultSet.CONCUR_READ_ONLY);
    rst = pstmt.executeQuery();
    this.recordCountPerPage = PeopleForm.recordcountperpage;
    rst.last();
    int intRowCount = rst.getRow();
    if (intRowCount % recordCountPerPage == 0)
    totalpage = intRowCount / recordCountPerPage;
    else
    totalpage = intRowCount / recordCountPerPage + 1; if (this.destinationpage == 1)
    rst.beforeFirst();
    else
    rst.absolute((this.destinationpage - 1) * recordCountPerPage);
    int j = 0;
    while (j++ < recordCountPerPage &&rst.next()) {
    pfd = new ProjectForm();
    pfd.setId(rst.getLong("ID"));
    pfd.setProjectId(rst.getString("PID"));
    pfd.setName(rst.getString("NAME"));
    pfd.setSimpleName(rst.getString("SN"));
    pfd.setPM(rst.getString("PMNAME"));
    pfd.setStatusName(rst.getString("PStatus"));
    pfd.setStatus(Integer.parseInt(rst.getString("PStatusID").trim()));
    list.add(pfd);
    }
    super.setList(list);
    this.currentpage = this.destinationpage;
    } finally {
    if (rst != null)
    rst.close();
    if (pstmt != null)
    pstmt.close();
    }
    }