如何在Struts中实现分页显示数据 PageData类代码: 1import java.util.Collection; 2 3/**//** 4 * <p>Title: </p> 5 * <p>Description: </p> 6 * <p>Copyright: Copyright (c) 2003</p> 7 * <p>Company: </p> 8 * @author George Hill 9 * @version 1.0 10 */ 11 12public class PageData { 13 14 /**//** 15 * 每页显示的数据行数 16 */ 17 public final static int NUMBER_PER_PAGE = 15; 18 19 /**//** 20 * 保存页面显示的数据集合 21 */ 22 protected Collection list; 23 24 /**//** 25 * 当前页数 26 */ 27 protected int page; 28 29 /**//** 30 * 最大页数 31 */ 32 protected int maxPage; 33 34 /**//** 35 * 无参构造方法 36 */ 37 public PageData() {} 38 39 /**//** 40 * 构造方法 41 * @param list 数据集 42 * @param page 当前页数 43 * @param maxPage 最大页数 44 */ 45 public PageData(Collection list, int page, int maxPage) { 46 this.list = list; 47 this.page = page; 48 this.maxPage = maxPage; 49 } 50 51 /**//** 52 * 获得数据集合 53 * @return 数据集合 54 */ 55 public Collection getList() { 56 return this.list; 57 } 58 59 /**//** 60 * 设置数据集合 61 * @param list 新的数据集合 62 */ 63 public void setList(Collection list) { 64 this.list = list; 65 } 66 67 /**//** 68 * 获得当前页数 69 * @return 当前页数 70 */ 71 public int getPage() { 72 return this.page; 73 } 74 75 /**//** 76 * 获得上页页数 77 * @return 上页页数 78 */ 79 public int getPrePage() { 80 int prePage = page - 1; 81 82 if (prePage <= 0) 83 prePage = 1; 84 85 return prePage; 86 } 87 88 /**//** 89 * 获得下页页数 90 * @return 下页页数 91 */ 92 public int getNextPage() { 93 int next = page + 1; 94 95 if (next > maxPage) 96 next = maxPage; 97 98 return next; 99 } 100 101 /**//** 102 * 设置当前页数 103 * @param page 当前页数新值 104 */ 105 public void setPage(int page) { 106 this.page = page; 107 } 108 109 /**//** 110 * 获得最大页数 111 * @return 最大页数 112 */ 113 public int getMaxPage() { 114 return this.maxPage; 115 } 116 117 /**//** 118 * 设置最大页数 119 * @param maxPage 最大页数新值 120 */ 121 public void setMaxPage(int maxPage) { 122 this.maxPage = maxPage; 123 } 124 125 /**//** 126 * 获得首页标识 127 * @return 首页标识 128 */ 129 public boolean isFirstPage() { 130 if (this.page <= 1) 131 return true; 132 133 return false; 134 } 135 136 /**//** 137 * 获得末页标识 138 * @return 末页标识 139 */ 140 public boolean isLastPage() { 141 if (this.page >= this.maxPage) 142 return true; 143 144 return false; 145 } 146} 147 TurnPageForm类代码: 1import javax.servlet.http.*; 2import org.apache.struts.action.*; 3 4/**//** 5 * <p>Title: </p> 6 * <p>Description: </p> 7 * <p>Copyright: Copyright (c) 2003</p> 8 * <p>Company: </p> 9 * @author George Hill 10 * @version 1.0 11 */ 12 13public class TurnPageForm extends ActionForm{ 14 15 /**//** 16 * 要跳转的页码 17 */ 18 protected int page; 19 20 /**//** 21 * 跳转按钮 22 */ 23 protected String turn; 24 25 /**//** 26 * 首页按钮 27 */ 28 protected String first; 29 30 /**//** 31 * 上一页按钮 32 */ 33 protected String preview; 34 35 /**//** 36 * 下一页按钮 37 */ 38 protected String next; 39 40 /**//** 41 * 末页按钮 42 */ 43 protected String last; 44 45 /**//** 46 * 跳转地址 47 */ 48 protected String url; 49 50 /**//** 51 * 当前页码 52 */ 53 protected int currentPage; 54 55 /**//** 56 * 最大页码 57 */ 58 protected int maxPage; 59 60 /**//** 61 * 获得跳转页码 62 * @return 跳转页码 63 */ 64 public int getPage() { 65 return this.page; 66 } 67 68 /**//** 69 * 设置跳转页码 70 * @param page 跳转页码值 71 */ 72 public void setPage(int page) { 73 this.page = page; 74 } 75 76 /**//** 77 * 获得跳转按钮值 78 * @return 跳转按钮值 79 */ 80 public String getTurn() { 81 return this.turn; 82 } 83 84 /**//** 85 * 设置跳转按钮值 86 * @param turn 跳转按钮值 87 */ 88 public void setTurn(String turn) { 89 this.turn = turn; 90 } 91 92 /**//** 93 * 获得首页按钮值 94 * @return 首页按钮值 95 */ 96 public String getFirst() { 97 return this.first; 98 } 99 100 /**//** 101 * 设置首页按钮值 102 * @param first 首页按钮值 103 */ 104 public void setFirst(String first) { 105 this.first = first; 106 } 107 108 /**//** 109 * 获得上一页按钮值 110 * @return 上一页按钮值 111 */ 112 public String getPreview() { 113 return this.preview; 114 } 115 116 /**//** 117 * 设置上一页按钮值 118 * @param preview 上一页按钮值 119 */ 120 public void setPreview(String preview) { 121 this.preview = preview; 122 } 123 124 /**//** 125 * 获得下一页按钮值 126 * @return 下一页按钮值 127 */ 128 public String getNext() { 129 return this.next; 130 } 131 132 /**//** 133 * 设置下一页按钮值 134 * @param next 下一页按钮值 135 */ 136 public void setNext(String next) { 137 this.next = next; 138 } 139 140 /**//** 141 * 获得末页按钮值 142 * @return 末页按钮值 143 */ 144 public String getLast() { 145 return this.last; 146 } 147 148 /**//** 149 * 设置末页按钮值 150 * @param last 末页按钮值 151 */ 152 public void setLast(String last) { 153 this.last = last; 154 } 155 156 /**//** 157 * 获得跳转地址 158 * @return 跳转地址 159 */ 160 public String getUrl() { 161 return this.url; 162 } 163 164 /**//** 165 * 设置跳转地址 166 * @param url 跳转地址 167 */ 168 public void setUrl(String url) { 169 this.url = url; 170 } 171 172 /**//** 173 * 获得当前页码 174 * @return 当前页码 175 */ 176 public int getCurrentPage() { 177 return this.currentPage; 178 } 179 180 /**//** 181 * 设置当前页码 182 * @param page 当前页码值 183 */ 184 public void setCurrentPage(int page) { 185 this.currentPage = page; 186 } 187 188 /**//** 189 * 获得最大页码 190 * @return 最大页码 191 */ 192 public int getMaxPage() { 193 return this.maxPage; 194 } 195 196 /**//** 197 * 设置最大页码 198 * @param page 最大页码 199 */ 200 public void setMaxPage(int page) { 201 this.maxPage = page; 202 } 203 204 /**//** 205 * 重置属性 206 * @param mapping action映射 207 * @param request HTTP请求 208 */ 209 public void reset(ActionMapping mapping, HttpServletRequest request) { 210 this.page = 0; 211 this.turn = null; 212 this.first = null; 213 this.preview = null; 214 this.next = null; 215 this.last = null; 216 this.url = null; 217 this.currentPage = 0; 218 this.maxPage = 0; 219 } 220 221} 222 TurnPageAction代码: 1import java.io.*; 2import javax.servlet.*; 3import javax.servlet.http.*; 4 5import org.apache.struts.action.*; 6 7import xxx.TurnPageForm; 8 9/**//** 10 * <p>Title: </p> 11 * <p>Description: </p> 12 * <p>Copyright: Copyright (c) 2003</p> 13 * <p>Company: </p> 14 * @author George Hill 15 * @version 1.0 16 */ 17 18public class TurnPageAction extends Action { 19 20 /**//** 21 * 执行用户的跳转页面请求 22 * @param mapping 转发映射表 23 * @param form 请求的ActionForm Bean 24 * @param request 用户请求 25 * @param response 用户应答 26 * @return 成功或者失败的转发 27 * @throws IOException IO例外 28 * @throws ServletException Servlet例外 29 */ 30 public ActionForward execute(ActionMapping mapping, ActionForm form, 31 HttpServletRequest request, 32 HttpServletResponse response) throws IOException, 33 ServletException { 34 if (form instanceof TurnPageForm) { 35 TurnPageForm tform = (TurnPageForm) form; 36 int page = 0; 37 String url = null; 38 39 if (tform.getTurn() != null) { 40 page = tform.getPage(); 41 } else if (tform.getFirst() != null) { 42 page = 1; 43 } else if (tform.getPreview() != null) { 44 page = tform.getCurrentPage() - 1; 45 } else if (tform.getNext() != null) { 46 page = tform.getCurrentPage() + 1; 47 } else if (tform.getLast() != null) { 48 page = tform.getMaxPage(); 49 } 50 51 if (page < 1) { 52 page = 1; 53 } else if (page > tform.getMaxPage()) { 54 page = tform.getMaxPage(); 55 } 56 if (tform.getUrl().indexOf("?") == -1) { 57 url = tform.getUrl() + "?page=" + page; 58 } else { 59 url = tform.getUrl() + "&page=" + page; 60 } 61 return new ActionForward(url); 62 } 63 64 return mapping.findForward("error"); 65 } 66} 67 struts-config.xml的相关片断: 1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> 3<struts-config> 4 <form-beans> 5 6 <form-bean name="turnPageForm" type="xxx.TurnPageForm" /> 7 8 </form-bean> 9 <action-mappings> 10 11 <action name="turnPageForm" path="/turnPage" scope="request" type="xxx.TurnPageAction" /> 12 </action-mappings> 13</struts-config> 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>