隨筆-57  評論-202  文章-17  trackbacks-0
                由于篇幅較長,所以分開兩篇來寫。下面是我的一個實際例子,從數據庫中分頁獲取管理員的數據,然后在JSP頁面上表示出來。
                我的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   * 執行管理員列表操作
          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    //獲取頁碼
          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    //獲得總記錄數
          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    //分頁部分
          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頁面部分的代碼片斷:

           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>

                實際的頁面效果圖:

          sample.JPG

                OK,我的整個實現就完成了。我覺得有許多地方還是需要完善的,例如我的實現不夠抽象,而且覺得繁瑣了點,不過我一直都用著,還沒有碰到過什么問題。請各位看完以后多多指教。
          posted on 2005-05-13 11:33 小米 閱讀(4853) 評論(25)  編輯  收藏 所屬分類: Struts

          評論:
          # re: 如何在Struts中實現分頁顯示數據(2) 2005-05-16 15:26 | 平淡是真
          你好!很高興認識你!我是一個搞IT的女子,可是總感到力不從心,因為自己的基礎好象很差!所以沒有什么信心!你能給我一些指導嗎?  回復  更多評論
            
          # re: 如何在Struts中實現分頁顯示數據(2) 2005-05-16 22:36 | 小米
          指導不敢當,大家一起交流。信心可以慢慢建立的,畢竟誰也不是一生下來什么都會。很高興,終于有人回復了。還以為這里的人全部都喜歡潛水的。:),你坐了我的沙發哦。  回復  更多評論
            
          # re: 如何在Struts中實現分頁顯示數據(2) 2005-06-09 11:29 | emu
          可以看看這個: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

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

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

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

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

          懇請前輩們給予我指教,謝謝!

            回復  更多評論
            
          # re: 如何在Struts中實現分頁顯示數據(2) 2008-05-15 19:50 | f
          你的Adiministator在哪了??
          是個啥啊,也不介紹介紹,大家知道要實現啥處理啊  回復  更多評論
            
          # re: 如何在Struts中實現分頁顯示數據(2) 2008-09-01 11:28 | 小文
          先謝謝樓主。。雖然我沒有搶到沙發。。但是我仍很高興。。請問這個表格的CSS樣式該怎么編寫。??吹侥膶嵗?。感覺不錯。。所以也想跟樓主學習學習。。。


          我的QQ: 452241943 郵箱: YoungEngineer@163.com 期待樓主。  回復  更多評論
            
          # re: 如何在Struts中實現分頁顯示數據(2) 2008-12-29 23:36 | 小飛刀
          你的admins中只有3個屬性啊,page、list、maxPage這三個屬性,而在你的jsp中怎么又多出了lastPage、firstPage這兩個屬性了,難道說,這兩個屬性是從list中的?,要是那樣的話,能否分享一下啊 bifnmqj@sina.com  回復  更多評論
            
          主站蜘蛛池模板: 南澳县| 南岸区| 澄城县| 松溪县| 玉环县| 定兴县| 绥德县| 枝江市| 瑞安市| 昭平县| 尉犁县| 天长市| 蒙山县| 大足县| 石嘴山市| 资源县| 揭东县| 泸定县| 九龙县| 米易县| 双牌县| 兴宁市| 洛阳市| 区。| 久治县| 鄯善县| 乡城县| 樟树市| 西乌珠穆沁旗| 大宁县| 米林县| 延津县| 时尚| 上饶县| 和政县| 东乡| 龙南县| 德庆县| 股票| 左云县| 盘山县|