<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-57  評(píng)論-202  文章-17  trackbacks-0
          由于篇幅較長(zhǎng),所以分開(kāi)兩篇來(lái)寫(xiě)。下面是我的一個(gè)實(shí)際例子,從數(shù)據(jù)庫(kù)中分頁(yè)獲取管理員的數(shù)據(jù),然后在JSP頁(yè)面上表示出來(lái)。
          我的Action的代碼:

     1import java.util.List;
     2import javax.servlet.http.*;
     3
     4import org.apache.struts.action.*;
     5
     6import xxx.Administrator;
     7import xxx.TurnPageForm;
     8import xxx.PageData;
     9
    10/**
    11 * <p>Title: </p>
    12 *
    13 * <p>Description: </p>
    14 *
    15 * <p>Copyright: Copyright (c) 2003</p>
    16 *
    17 * <p>Company: </p>
    18 *
    19 * @author George Hill
    20 * @version 1.0
    21 */

    22
    23public class AdminListAction extends Action {
    24
    25  private static final int NUMBER = 15;
    26
    27  /**
    28   * 執(zhí)行管理員列表操作
    29   * @param mapping ActionMapping
    30   * @param form ActionForm
    31   * @param request HttpServletRequest
    32   * @param response HttpServletResponse
    33   * @throws Exception
    34   * @return ActionForward
    35   */

    36  public ActionForward execute(ActionMapping mapping, ActionForm form,
    37                               HttpServletRequest request,
    38                               HttpServletResponse response) throws Exception {
    39    HttpSession session = request.getSession();
    40
    41    //獲取頁(yè)碼
    42    String pageStr = request.getParameter("page");
    43    if (pageStr == null)
    44      pageStr = String.valueOf(session.getAttribute("page"));
    45    else
    46      session.setAttribute("page", pageStr);
    47    int page = 1;
    48    try {
    49      page = Integer.parseInt(pageStr);
    50    }
     catch (NumberFormatException nfe) {
    51    }

    52
    53    //獲得總記錄數(shù)
    54    int count = Administrator.countAllAdministrators();
    55    int maxPage = count / NUMBER;
    56    if (count % NUMBER != 0)
    57      maxPage++;
    58
    59    //獲得列表
    60    List list = Administrator.getAdministrators(NUMBER, (page - 1* NUMBER);
    61
    62    if (count != 0 && list == null)
    63      list = Administrator.getAdministrators(NUMBER, 0);
    64
    65    if (list != null{
    66      PageData data = new PageData(list, page, maxPage);
    67
    68      request.setAttribute("admins", data);
    69    }

    70
    71    //分頁(yè)部分
    72    TurnPageForm tform = new TurnPageForm();
    73    tform.setCurrentPage(page);
    74    tform.setMaxPage(maxPage);
    75    request.setAttribute("turnPageForm", tform);
    76
    77    return mapping.findForward("list");
    78  }

    79}

    80

          JSP頁(yè)面部分的代碼片斷:

     1    <logic:present name="admins">
     2      <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" class="table6">
     3        <tr align="center" class="table4">
     4          <td width="10%" height="18" align="center" nowrap class="head1"><bean:message key="list.index"/></td>
     5          <td width="20%" class="head1" align="center" nowrap><bean:message key="admin.account"/></td>
     6          <td width="30%" class="head1" align="center" nowrap><bean:message key="admin.name"/></td>
     7          <td width="10%" class="head1" align="center" nowrap><bean:message key="admin.status"/></td>
     8          <td width="30%" class="head1" align="center" nowrap><bean:message key="list.action"/></td>
     9        </tr>
    10      <logic:iterate name="admins" property="list" id="entry" indexId="index">
    11        <tr class="table5" onmouseover="changeColor(this, '#99CCFF')" onmouseout="changeColor(this, '#F2F3F4')">
    12          <td align="center" nowrap><bean:write name="index"/></td>
    13          <td align="center" nowrap><bean:write name="entry" property="account"/></td>
    14          <td align="center" nowrap><bean:write name="entry" property="name"/></td>
    15        <logic:equal name="entry" property="status" value="true">
    16          <td align="center" nowrap><bean:message key="status.enable"/></td>
    17          <td align="center" nowrap>
    18            <html:link page="/disableAdmin.do?status=false" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDisableMsg()">
    19              <font color="red"><bean:message key="status.disable"/></font>
    20            </html:link>
    21            <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
    22              <bean:message key="action.modify"/>
    23            </html:link>
    24            <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
    25              <font color="red"><bean:message key="action.delete"/></font>
    26            </html:link>
    27          </td>
    28        </logic:equal>
    29        <logic:equal name="entry" property="status" value="false">
    30          <td align="center" nowrap><font color="red"><bean:message key="status.disable"/></font></td>
    31          <td align="center" nowrap>
    32            <html:link page="/enableAdmin.do?status=true" paramId="account" paramName="entry" paramProperty="account">
    33              <bean:message key="status.enable"/>
    34            </html:link>
    35            <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
    36              <bean:message key="action.modify"/>
    37            </html:link>
    38            <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
    39              <font color="red"><bean:message key="action.delete"/></font>
    40            </html:link>
    41          </td>
    42        </logic:equal>
    43        </tr>
    44      </logic:iterate>
    45      </table>
    46      <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0">
    47        <tr>
    48          <td align="left"></td>
    49        <html:form action="/turnPage" method="POST">
    50          <td align="right" nowrap>
    51            <html:hidden property="url" value="/listAdmin.do"/>
    52            <html:hidden property="currentPage"/>
    53            <html:hidden property="maxPage"/>
    54            <bean:message key="page.the"/>
    55            <bean:write name="admins" property="page"/>
    56            <bean:message key="page.page"/>/
    57            <bean:message key="page.all"/><bean:write name="admins" property="maxPage"/><bean:message key="page.page"/>
    58            <bean:message key="page.turn"/>
    59          <logic:equal name="admins" property="maxPage" value="1">
    60            <html:text property="page" styleClass="input_log" styleId="page" size="3" value="" disabled="true"/>
    61            <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO" disabled="true"/>
    62          </logic:equal>
    63          <logic:notEqual name="admins" property="maxPage" value="1">
    64            <html:text property="page" styleClass="input_log" styleId="page" size="3" value=""/>
    65            <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO"/>
    66          </logic:notEqual>
    67          <logic:equal name="admins" property="firstPage" value="true">
    68            <html:submit property="first" styleClass="t_input" styleId="first" disabled="true"><bean:message key="page.first"/></html:submit>
    69            <html:submit property="preview" styleClass="t_input" styleId="preview" disabled="true"><bean:message key="page.previous"/></html:submit>
    70          </logic:equal>
    71          <logic:notEqual name="admins" property="firstPage" value="true">
    72            <html:submit property="first" styleClass="t_input" styleId="first"><bean:message key="page.first"/></html:submit>
    73            <html:submit property="preview" styleClass="t_input" styleId="preview"><bean:message key="page.previous"/></html:submit>
    74          </logic:notEqual>
    75          <logic:equal name="admins" property="lastPage" value="true">
    76            <html:submit property="next" styleClass="t_input" styleId="next" disabled="true"><bean:message key="page.next"/></html:submit>
    77            <html:submit property="last" styleClass="t_input" styleId="last" disabled="true"><bean:message key="page.last"/></html:submit>
    78          </logic:equal>
    79          <logic:notEqual name="admins" property="lastPage" value="true">
    80            <html:submit property="next" styleClass="t_input" styleId="next"><bean:message key="page.next"/></html:submit>
    81            <html:submit property="last" styleClass="t_input" styleId="last"><bean:message key="page.last"/></html:submit>
    82          </logic:notEqual>
    83          </td>
    84        </html:form>
    85        </tr>
    86      </table>
    87    </logic:present>

          實(shí)際的頁(yè)面效果圖:

    sample.JPG

          OK,我的整個(gè)實(shí)現(xiàn)就完成了。我覺(jué)得有許多地方還是需要完善的,例如我的實(shí)現(xiàn)不夠抽象,而且覺(jué)得繁瑣了點(diǎn),不過(guò)我一直都用著,還沒(méi)有碰到過(guò)什么問(wèn)題。請(qǐng)各位看完以后多多指教。
    posted on 2005-05-13 11:33 小米 閱讀(4846) 評(píng)論(25)  編輯  收藏 所屬分類: Struts

    評(píng)論:
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-05-16 15:26 | 平淡是真
    你好!很高興認(rèn)識(shí)你!我是一個(gè)搞IT的女子,可是總感到力不從心,因?yàn)樽约旱幕A(chǔ)好象很差!所以沒(méi)有什么信心!你能給我一些指導(dǎo)嗎?  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-05-16 22:36 | 小米
    指導(dǎo)不敢當(dāng),大家一起交流。信心可以慢慢建立的,畢竟誰(shuí)也不是一生下來(lái)什么都會(huì)。很高興,終于有人回復(fù)了。還以為這里的人全部都喜歡潛水的。:),你坐了我的沙發(fā)哦。  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-06-09 11:29 | emu
    可以看看這個(gè):http://displaytag.sourceforge.net/

    Actually the display tag library can just... display tables! Give it a list of objects and it will handle column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table in a customizable XHTML style

      回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-06-09 17:09 | 小米
    嗯,不錯(cuò),謝謝你提供的資料。  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-07-31 13:05 | kk
    你的代碼沒(méi)有貼完阿  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-07-31 21:52 | 小米
    我已經(jīng)貼完了啊。你是說(shuō)哪部分有問(wèn)題呢?  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-01 00:30 | kk
    我是說(shuō): import xxx.Administrator 你導(dǎo)入的這個(gè)類的Administrator的代碼沒(méi)有貼上,無(wú)法得知你的 List list = Administrator.getAdministrators(NUMBER, (page - 1) * NUMBER);和 list = Administrator.getAdministrators(NUMBER, 0);兩個(gè)方法怎么實(shí)現(xiàn)的。

    最好能把源碼發(fā)給我一份看看,謝謝你了。
    MSN:yjj03317@hotmail.com
    QQ: 76912421
    e-mail:yjj317@163.com
      回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-01 10:01 | 小米
    這是在公司寫(xiě)的代碼,不方便貼出來(lái)的。Administrator.getAdministrators(NUMBER, (page - 1)*NUMBER)方法就是從數(shù)據(jù)庫(kù)中獲取某個(gè)表的一部分記錄的方法。這個(gè)需要根據(jù)具體的情況實(shí)現(xiàn)的。我這里都主要說(shuō)的是原理,所以代碼部分沒(méi)有貼完整。  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-01 10:27 | kk
    我覺(jué)得要是交流的話最好能寫(xiě)個(gè)小例子,最好完整點(diǎn)啊
    不過(guò)還是要說(shuō)聲:謝謝  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-01 12:09 | 小米
    不客氣,以后盡量把例子寫(xiě)的完整些。呵呵。:)  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-01 15:04 | kk
    總算寫(xiě)好了一個(gè)關(guān)于分頁(yè)的例子,等我的博客搞好后,就把代碼貼上去,大家一起交流。:)  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-01 15:38 | 小米
    呵呵,歡迎交流。:)  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-26 11:06 | flyingis
    如果能給出該方法 Administrator.getAdministrators(NUMBER, (page - 1) * NUMBER); 中的SQL語(yǔ)句就好了,不知道樓主是否方便?  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-28 23:03 | 小米
    在MySQL中,這樣的語(yǔ)句應(yīng)該是這樣的SELECT * FROM [TABLE] ORDER BY [FIELD] LIMIT ? OFFSET ?  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-29 16:32 | flyingis
    謝謝!
    可是沒(méi)有看到number和(page-1)*number在SQL中出現(xiàn)啊?  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-08-29 22:29 | 小米
    LIMIT ? OFFSET ?中的兩個(gè)問(wèn)號(hào)分別對(duì)應(yīng)的就是number和(page - 1) * number。我覺(jué)得你可能需要多看看JDBC和數(shù)據(jù)庫(kù)的基礎(chǔ)編程。這里用的是PreparedStatement,所以用?號(hào)做占位符號(hào)。  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2005-12-19 16:20 | ahua
    看過(guò)了,寫(xiě)得不是太清楚,,,,,,,,,,,,對(duì)太多人沒(méi)有實(shí)際價(jià)值
    我初學(xué),不是太懂,,雖然也做過(guò)自己的分頁(yè),,,但不太理解你代碼的意義  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2006-02-18 10:44 | fjxmhjz
    我只能看到第2部分,代碼不全。

    最好能把源碼發(fā)給我一份看看,謝謝你了。

    e-mail:fjxmhjz@tom.com  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2006-09-23 11:54 | sartre
    你好,我覺(jué)得你的表格的樣子到是不錯(cuò),能否把定義表格的css代碼,發(fā)一份給我,自己做顯示的時(shí)候可以用一下,謝謝!! sartre05@163.com  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2006-12-03 19:26 | 小新
    你的文章真的不錯(cuò)。看過(guò)了感覺(jué)很好啊!我也是學(xué)java的,現(xiàn)在還有一年畢業(yè),希望和你交個(gè)朋友!  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2007-04-25 11:53 | sadsardine
    你的代碼可讀性很好,很容易看懂,但結(jié)構(gòu)不太好,分頁(yè)的邏輯應(yīng)該做成一個(gè)接口。還有那個(gè)當(dāng)前頁(yè)碼的傳遞你放在了SESSION里,總覺(jué)得這樣弄結(jié)構(gòu)不好,不過(guò)我也沒(méi)有更好的辦法。另外我有個(gè)疑問(wèn),你JSP中顯示數(shù)據(jù)用的BEAN不是PAGEDATA而是ADMIN,是怎么回事?還有個(gè)問(wèn)題請(qǐng)教,
    if (tform.getUrl().indexOf("?") == -1) {
    url = tform.getUrl() + "?page=" + page;
    } else {
    url = tform.getUrl() + "&page=" + page;
    }
    如果這段代碼之前url的值是“a?page=1”,執(zhí)行這段代碼之后url的值是什么?是“a?page=1&page=2”,這樣再ACTION中拿到的page是1還是2?
      回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2007-09-17 23:42 | peanent
    請(qǐng)問(wèn)前輩如果我想已下面一種想法去做我該如何實(shí)現(xiàn)呢。
    問(wèn)題定義:
    1、同樣適用<html:link>,每次點(diǎn)擊下一頁(yè),提交action,在action中把頁(yè)面上的form傳過(guò)去,然后根據(jù)form中的值重新到數(shù)據(jù)庫(kù)中去檢索數(shù)據(jù),把下一頁(yè)的數(shù)據(jù)存放到request中在頁(yè)面顯示。
    我現(xiàn)在遇到的問(wèn)題是,用戶在表單輸入數(shù)據(jù)以后點(diǎn)擊檢索按鈕能正常顯示第一頁(yè)的數(shù)據(jù),但是如果點(diǎn)擊第二頁(yè),第三頁(yè)。。。。下一頁(yè)的鏈接之后就不能顯示這些頁(yè)面的值,原因是form沒(méi)有提交上去,顯示form的值為NULL,自然也就取不到用戶寫(xiě)到form中的值,不能根據(jù)form中的值從數(shù)據(jù)庫(kù)中重新檢索數(shù)據(jù)顯示到頁(yè)面上
    請(qǐng)問(wèn):
    是不是html:link標(biāo)簽不能提交form還是html:link標(biāo)簽會(huì)重置request中的form導(dǎo)致action中的form為NULL呢

    懇請(qǐng)前輩們給予我指教,謝謝!

      回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2008-05-15 19:50 | f
    你的Adiministator在哪了??
    是個(gè)啥啊,也不介紹介紹,大家知道要實(shí)現(xiàn)啥處理啊  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2008-09-01 11:28 | 小文
    先謝謝樓主。。雖然我沒(méi)有搶到沙發(fā)。。但是我仍很高興。。請(qǐng)問(wèn)這個(gè)表格的CSS樣式該怎么編寫(xiě)。。看到您的實(shí)例。感覺(jué)不錯(cuò)。。所以也想跟樓主學(xué)習(xí)學(xué)習(xí)。。。


    我的QQ: 452241943 郵箱: YoungEngineer@163.com 期待樓主。  回復(fù)  更多評(píng)論
      
    # re: 如何在Struts中實(shí)現(xiàn)分頁(yè)顯示數(shù)據(jù)(2) 2008-12-29 23:36 | 小飛刀
    你的admins中只有3個(gè)屬性啊,page、list、maxPage這三個(gè)屬性,而在你的jsp中怎么又多出了lastPage、firstPage這兩個(gè)屬性了,難道說(shuō),這兩個(gè)屬性是從list中的?,要是那樣的話,能否分享一下啊 bifnmqj@sina.com  回復(fù)  更多評(píng)論
      
    主站蜘蛛池模板: 成人免费毛片内射美女-百度| 午夜亚洲乱码伦小说区69堂| 国产精品无码一区二区三区免费| 日本高清免费观看| 99久久国产精品免费一区二区| 一区二区三区在线免费观看视频 | 亚洲精品色播一区二区| 亚洲最大在线观看| 久久精品国产亚洲AV忘忧草18| 久久精品亚洲精品国产色婷 | 女人18一级毛片免费观看| 日本阿v免费费视频完整版| 91精品免费久久久久久久久| 18未年禁止免费观看| 麻豆一区二区免费播放网站| 一二三四免费观看在线视频中文版| 青青青国产在线观看免费| 国产福利在线免费| 免费国产一级特黄久久| 亚洲精品成人a在线观看| 国产亚洲精品自在线观看| 久久精品国产亚洲av成人| 亚洲天堂免费在线| 又黄又大的激情视频在线观看免费视频社区在线 | 亚洲av中文无码| 亚洲成熟xxxxx电影| 亚洲AV女人18毛片水真多| 中文字幕免费在线看| 好吊妞在线新免费视频| 亚洲av无码专区国产乱码在线观看 | 国产亚洲情侣久久精品| 在线看无码的免费网站| 亚洲精品视频在线观看你懂的| 亚洲AV人无码综合在线观看| 亚洲日韩亚洲另类激情文学| a在线观看免费视频| 成人永久免费福利视频网站| 亚洲国产第一页www| 九九九精品视频免费| 成人奭片免费观看| 国产精品亚洲片在线va|