由于篇幅较长,所以分开两篇来写。下面是我的一个实际例子,从数据库中分页获取管理员的数据,然后在JSP页面上表示出来。
      我的Action的代码:
 1 import java.util.List;
import java.util.List;
 2 import javax.servlet.http.*;
import javax.servlet.http.*;
 3
 4 import org.apache.struts.action.*;
import org.apache.struts.action.*;
 5
 6 import xxx.Administrator;
import xxx.Administrator;
 7 import xxx.TurnPageForm;
import xxx.TurnPageForm;
 8 import xxx.PageData;
import xxx.PageData;
 9
10
 /**//**
/**//**
11 * <p>Title: </p>
 * <p>Title: </p>
12 *
 *
13 * <p>Description: </p>
 * <p>Description: </p>
14 *
 *
15 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Copyright: Copyright (c) 2003</p>
16 *
 *
17 * <p>Company: </p>
 * <p>Company: </p>
18 *
 *
19 * @author George Hill
 * @author George Hill
20 * @version 1.0
 * @version 1.0
21 */
 */
22
23
 public class AdminListAction extends Action
public class AdminListAction extends Action  {
{
24
25 private static final int NUMBER = 15;
  private static final int NUMBER = 15;
26
27
 /**//**
  /**//**
28 * 执行管理员列表操作
   * 执行管理员列表操作
29 * @param mapping ActionMapping
   * @param mapping ActionMapping
30 * @param form ActionForm
   * @param form ActionForm
31 * @param request HttpServletRequest
   * @param request HttpServletRequest
32 * @param response HttpServletResponse
   * @param response HttpServletResponse
33 * @throws Exception
   * @throws Exception
34 * @return ActionForward
   * @return ActionForward
35 */
   */
36 public ActionForward execute(ActionMapping mapping, ActionForm form,
  public ActionForward execute(ActionMapping mapping, ActionForm form,
37 HttpServletRequest request,
                               HttpServletRequest request,
38
 HttpServletResponse response) throws Exception
                               HttpServletResponse response) throws Exception  {
{
39 HttpSession session = request.getSession();
    HttpSession session = request.getSession();
40
41 //获取页码
    //获取页码
42 String pageStr = request.getParameter("page");
    String pageStr = request.getParameter("page");
43 if (pageStr == null)
    if (pageStr == null)
44 pageStr = String.valueOf(session.getAttribute("page"));
      pageStr = String.valueOf(session.getAttribute("page"));
45 else
    else
46 session.setAttribute("page", pageStr);
      session.setAttribute("page", pageStr);
47 int page = 1;
    int page = 1;
48
 try
    try  {
{
49 page = Integer.parseInt(pageStr);
      page = Integer.parseInt(pageStr);
50
 } catch (NumberFormatException nfe)
    } catch (NumberFormatException nfe)  {
{
51 }
    }
52
53 //获得总记录数
    //获得总记录数
54 int count = Administrator.countAllAdministrators();
    int count = Administrator.countAllAdministrators();
55 int maxPage = count / NUMBER;
    int maxPage = count / NUMBER;
56 if (count % NUMBER != 0)
    if (count % NUMBER != 0)
57 maxPage++;
      maxPage++;
58
59 //获得列表
    //获得列表
60 List list = Administrator.getAdministrators(NUMBER, (page - 1) * NUMBER);
    List list = Administrator.getAdministrators(NUMBER, (page - 1) * NUMBER);
61
62 if (count != 0 && list == null)
    if (count != 0 && list == null)
63 list = Administrator.getAdministrators(NUMBER, 0);
      list = Administrator.getAdministrators(NUMBER, 0);
64
65
 if (list != null)
    if (list != null)  {
{
66 PageData data = new PageData(list, page, maxPage);
      PageData data = new PageData(list, page, maxPage);
67
68 request.setAttribute("admins", data);
      request.setAttribute("admins", data);
69 }
    }
70
71 //分页部分
    //分页部分
72 TurnPageForm tform = new TurnPageForm();
    TurnPageForm tform = new TurnPageForm();
73 tform.setCurrentPage(page);
    tform.setCurrentPage(page);
74 tform.setMaxPage(maxPage);
    tform.setMaxPage(maxPage);
75 request.setAttribute("turnPageForm", tform);
    request.setAttribute("turnPageForm", tform);
76
77 return mapping.findForward("list");
    return mapping.findForward("list");
78 }
  }
79 }
}
80
        JSP页面部分的代码片断:
 1 <logic:present name="admins">
    <logic:present name="admins">
 2 <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" class="table6">
      <table width="90%" border="0" align="center" cellpadding="0" cellspacing="1" class="table6">
 3 <tr align="center" class="table4">
        <tr align="center" class="table4">
 4 <td width="10%" height="18" align="center" nowrap class="head1"><bean:message key="list.index"/></td>
          <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>
          <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>
          <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>
          <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>
          <td width="30%" class="head1" align="center" nowrap><bean:message key="list.action"/></td>
 9 </tr>
        </tr>
10 <logic:iterate name="admins" property="list" id="entry" indexId="index">
      <logic:iterate name="admins" property="list" id="entry" indexId="index">
11 <tr class="table5" onmouseover="changeColor(this, '#99CCFF')" onmouseout="changeColor(this, '#F2F3F4')">
        <tr class="table5" onmouseover="changeColor(this, '#99CCFF')" onmouseout="changeColor(this, '#F2F3F4')">
12 <td align="center" nowrap><bean:write name="index"/></td>
          <td align="center" nowrap><bean:write name="index"/></td>
13 <td align="center" nowrap><bean:write name="entry" property="account"/></td>
          <td align="center" nowrap><bean:write name="entry" property="account"/></td>
14 <td align="center" nowrap><bean:write name="entry" property="name"/></td>
          <td align="center" nowrap><bean:write name="entry" property="name"/></td>
15 <logic:equal name="entry" property="status" value="true">
        <logic:equal name="entry" property="status" value="true">
16 <td align="center" nowrap><bean:message key="status.enable"/></td>
          <td align="center" nowrap><bean:message key="status.enable"/></td>
17 <td align="center" nowrap>
          <td align="center" nowrap>
18 <html:link page="/disableAdmin.do?status=false" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDisableMsg()">
            <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>
              <font color="red"><bean:message key="status.disable"/></font>
20 </html:link>
            </html:link>
21 <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
            <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
22 <bean:message key="action.modify"/>
              <bean:message key="action.modify"/>
23 </html:link>
            </html:link>
24 <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
            <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
25 <font color="red"><bean:message key="action.delete"/></font>
              <font color="red"><bean:message key="action.delete"/></font>
26 </html:link>
            </html:link>
27 </td>
          </td>
28 </logic:equal>
        </logic:equal>
29 <logic:equal name="entry" property="status" value="false">
        <logic:equal name="entry" property="status" value="false">
30 <td align="center" nowrap><font color="red"><bean:message key="status.disable"/></font></td>
          <td align="center" nowrap><font color="red"><bean:message key="status.disable"/></font></td>
31 <td align="center" nowrap>
          <td align="center" nowrap>
32 <html:link page="/enableAdmin.do?status=true" paramId="account" paramName="entry" paramProperty="account">
            <html:link page="/enableAdmin.do?status=true" paramId="account" paramName="entry" paramProperty="account">
33 <bean:message key="status.enable"/>
              <bean:message key="status.enable"/>
34 </html:link>
            </html:link>
35 <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
            <html:link page="/modifyAdmin.do?action=link" paramId="account" paramName="entry" paramProperty="account">
36 <bean:message key="action.modify"/>
              <bean:message key="action.modify"/>
37 </html:link>
            </html:link>
38 <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
            <html:link action="/deleteAdmin" paramId="account" paramName="entry" paramProperty="account" onclick="return MM_popupDelMsg()">
39 <font color="red"><bean:message key="action.delete"/></font>
              <font color="red"><bean:message key="action.delete"/></font>
40 </html:link>
            </html:link>
41 </td>
          </td>
42 </logic:equal>
        </logic:equal>
43 </tr>
        </tr>
44 </logic:iterate>
      </logic:iterate>
45 </table>
      </table>
46 <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0">
      <table width="90%" border="0" align="center" cellpadding="3" cellspacing="0">
47 <tr>
        <tr>
48 <td align="left"></td>
          <td align="left"></td>
49 <html:form action="/turnPage" method="POST">
        <html:form action="/turnPage" method="POST">
50 <td align="right" nowrap>
          <td align="right" nowrap>
51 <html:hidden property="url" value="/listAdmin.do"/>
            <html:hidden property="url" value="/listAdmin.do"/>
52 <html:hidden property="currentPage"/>
            <html:hidden property="currentPage"/>
53 <html:hidden property="maxPage"/>
            <html:hidden property="maxPage"/>
54 <bean:message key="page.the"/>
            <bean:message key="page.the"/>
55 <bean:write name="admins" property="page"/>
            <bean:write name="admins" property="page"/>
56 <bean:message key="page.page"/>/
            <bean:message key="page.page"/>/
57 <bean:message key="page.all"/><bean:write name="admins" property="maxPage"/><bean:message key="page.page"/>
            <bean:message key="page.all"/><bean:write name="admins" property="maxPage"/><bean:message key="page.page"/>
58 <bean:message key="page.turn"/>
            <bean:message key="page.turn"/>
59 <logic:equal name="admins" property="maxPage" value="1">
          <logic:equal name="admins" property="maxPage" value="1">
60 <html:text property="page" styleClass="input_log" styleId="page" size="3" value="" disabled="true"/>
            <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"/>
            <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO" disabled="true"/>
62 </logic:equal>
          </logic:equal>
63 <logic:notEqual name="admins" property="maxPage" value="1">
          <logic:notEqual name="admins" property="maxPage" value="1">
64 <html:text property="page" styleClass="input_log" styleId="page" size="3" value=""/>
            <html:text property="page" styleClass="input_log" styleId="page" size="3" value=""/>
65 <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO"/>
            <html:submit property="turn" styleClass="t_input" styleId="turn" value="GO"/>
66 </logic:notEqual>
          </logic:notEqual>
67 <logic:equal name="admins" property="firstPage" value="true">
          <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>
            <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>
            <html:submit property="preview" styleClass="t_input" styleId="preview" disabled="true"><bean:message key="page.previous"/></html:submit>
70 </logic:equal>
          </logic:equal>
71 <logic:notEqual name="admins" property="firstPage" value="true">
          <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>
            <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>
            <html:submit property="preview" styleClass="t_input" styleId="preview"><bean:message key="page.previous"/></html:submit>
74 </logic:notEqual>
          </logic:notEqual>
75 <logic:equal name="admins" property="lastPage" value="true">
          <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>
            <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>
            <html:submit property="last" styleClass="t_input" styleId="last" disabled="true"><bean:message key="page.last"/></html:submit>
78 </logic:equal>
          </logic:equal>
79 <logic:notEqual name="admins" property="lastPage" value="true">
          <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>
            <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>
            <html:submit property="last" styleClass="t_input" styleId="last"><bean:message key="page.last"/></html:submit>
82 </logic:notEqual>
          </logic:notEqual>
83 </td>
          </td>
84 </html:form>
        </html:form>
85 </tr>
        </tr>
86 </table>
      </table>
87 </logic:present>
    </logic:present>        实际的页面效果图:

      OK,我的整个实现就完成了。我觉得有许多地方还是需要完善的,例如我的实现不够抽象,而且觉得繁琐了点,不过我一直都用着,还没有碰到过什么问题。请各位看完以后多多指教。
	posted on 2005-05-13 11:33 
小米 阅读(4878) 
评论(25)  编辑  收藏  所属分类: 
Struts