1 import java.util.ArrayList; 2 import java.util.List; 3 4 /** 5 * 6 * @author cheney 7 * 8 * @date Aug 31, 2012 9 */ 10 public class PageModel<T> { 11 private int page = 1; // 当前页 12 public int totalPages = 0; // 总页数 13 private int pageRecorders;// 每页5条数据 14 private int totalRows = 0; // 总数据数 15 private int pageStartRow = 0;// 每页的起始数 16 private int pageEndRow = 0; // 每页显示数据的终止数 17 private boolean hasNextPage = false; // 是否有下一页 18 private boolean hasPreviousPage = false; // 是否有前一页 19 private List<T> list; 20 21 /** 22 * 23 * @param list 24 * @param pageRecorders 25 */ 26 public PageModel(List<T> list, int pageRecorders) { 27 init(list, pageRecorders);// 通过对象集,记录总数划分 28 } 29 30 /** */ 31 /** 32 * 初始化list,并告之该list每页的记录数 33 * 34 * @param list 35 * @param pageRecorders 36 */ 37 public void init(List<T> list, int pageRecorders) { 38 this.pageRecorders = pageRecorders; 39 this.list = list; 40 totalRows = list.size(); 41 42 hasPreviousPage = false; 43 44 if ((totalRows % pageRecorders) == 0) { 45 totalPages = totalRows / pageRecorders; 46 } else { 47 totalPages = totalRows / pageRecorders + 1; 48 } 49 50 if (page >= totalPages) { 51 hasNextPage = false; 52 } else { 53 hasNextPage = true; 54 } 55 56 if (totalRows < pageRecorders) { 57 this.pageStartRow = 0; 58 this.pageEndRow = totalRows; 59 } else { 60 this.pageStartRow = 0; 61 this.pageEndRow = pageRecorders; 62 } 63 } 64 65 public String displayForPage(String method) { 66 StringBuffer sb = new StringBuffer(); 67 sb.append("<div class="w-page"><label class="fr">"); 68 69 // 判断是否有上一页 70 if (this.isHasPreviousPage()) { 71 sb.append("<a href="javascript:void(0)" onclick="" + method 72 + "('" + String.valueOf(this.getPage() - 1) 73 + "');return false;">上一页</a>"); 74 } else { 75 sb.append("<span class="disabled">上一页</span>"); 76 } 77 78 // 中间显示 79 for (int i = 1; i <= this.getTotalPages(); i++) { 80 String spanClzz = "<a href="javascript:void(0)" onclick="" 81 + method + "('" + String.valueOf(i) + "');return false;">" 82 + i + "</a>"; 83 84 if (this.page == i) { 85 spanClzz = "<span class='current'>" + i + "</span>"; 86 } 87 sb.append(spanClzz); 88 89 // 当大于9页数的时候才进行分页显示 90 if (this.getTotalPages() - 2 > 7) { 91 if (i == 5) { 92 i = this.getTotalPages() - 2; 93 sb.append("..."); 94 } 95 } 96 } 97 // 判断是否有下一页 98 if (this.isHasNextPage()) { 99 sb.append("<a href="javascript:void(0)" onclick="" + method 100 + "('" + String.valueOf(this.getPage() + 1) 101 + "');return false;">下一页</a>"); 102 103 } else { 104 sb.append("<span class="disabled">下一页</span>"); 105 } 106 107 sb.append("</div>"); 108 return sb.toString(); 109 } 110 111 public String displayForPage() { 112 StringBuffer sb = new StringBuffer(); 113 sb.append("<div class="w-page"><label class="fr">"); 114 115 // 判断是否有上一页 116 if (this.isHasPreviousPage()) { 117 sb 118 .append("<a href="javascript:void(0)" onclick="findDistResult('" 119 + String.valueOf(this.getPage() - 1) 120 + "');return false;">上一页</a>"); 121 } else { 122 sb.append("<span class="disabled">上一页</span>"); 123 } 124 125 // 中间显示 126 for (int i = 1; i <= this.getTotalPages(); i++) { 127 String spanClzz = "<a href="javascript:void(0)" onclick="findDistResult('" 128 + String.valueOf(i) + "');return false;">" + i + "</a>"; 129 130 if (this.page == i) { 131 spanClzz = "<span class='current'>" + i + "</span>"; 132 } 133 sb.append(spanClzz); 134 135 // 当大于9页数的时候才进行分页显示 136 if (this.getTotalPages() - 2 > 7) { 137 if (i == 5) { 138 i = this.getTotalPages() - 2; 139 sb.append("..."); 140 } 141 } 142 } 143 // 判断是否有下一页 144 if (this.isHasNextPage()) { 145 sb 146 .append("<a href="javascript:void(0)" onclick="findDistResult('" 147 + String.valueOf(this.getPage() + 1) 148 + "');return false;">下一页</a>"); 149 150 } else { 151 sb.append("<span class="disabled">下一页</span>"); 152 } 153 154 sb.append("</div>"); 155 return sb.toString(); 156 } 157 158 // 判断要不要分页 159 public boolean isNext() { 160 return list.size() > 5; 161 } 162 163 public void setHasPreviousPage(boolean hasPreviousPage) { 164 this.hasPreviousPage = hasPreviousPage; 165 } 166 167 /** 168 * 169 * @return 170 */ 171 public List<T> getNextPage() { 172 page = page + 1; 173 disposePage(); 174 return getObjects(page); 175 } 176 177 /** 178 * 处理分页 179 */ 180 private void disposePage() { 181 if (page == 0) { 182 page = 1; 183 } 184 if ((page - 1) > 0) { 185 hasPreviousPage = true; 186 } else { 187 hasPreviousPage = false; 188 } 189 190 if (page >= totalPages) { 191 hasNextPage = false; 192 } else { 193 hasNextPage = true; 194 } 195 } 196 197 /** 198 * 上一页 199 * 200 * @return 201 */ 202 public List<T> getPreviousPage() { 203 page = page - 1; 204 205 if ((page - 1) > 0) { 206 hasPreviousPage = true; 207 } else { 208 hasPreviousPage = false; 209 } 210 if (page >= totalPages) { 211 hasNextPage = false; 212 } else { 213 hasNextPage = true; 214 } 215 return getObjects(page); 216 } 217 218 /** 219 * 获取第几页的内容 220 * 221 * @param page 222 * @return 223 */ 224 public List<T> getObjects(int page) { 225 if (page == 0) { 226 this.setPage(1); 227 page = 1; 228 } else { 229 this.setPage(page); 230 } 231 232 this.disposePage(); 233 234 if (page * pageRecorders < totalRows) {// 判断是否为最后一页 235 pageEndRow = page * pageRecorders; 236 pageStartRow = pageEndRow - pageRecorders; 237 } else { 238 pageEndRow = totalRows; 239 pageStartRow = pageRecorders * (totalPages - 1); 240 } 241 242 List<T> objects = null; 243 if (!list.isEmpty()) { 244 objects = list.subList(pageStartRow, pageEndRow); 245 } 246 return objects; 247 } 248 249 /** 250 * 第一页 251 * 252 * @return 253 */ 254 public List<T> getFistPage() { 255 if (this.isNext()) { 256 return list.subList(0, pageRecorders); 257 } else { 258 return list; 259 } 260 } 261 262 /** 263 * @return the page 264 */ 265 public int getPage() { 266 return page; 267 } 268 269 /** 270 * @param page 271 * the page to set 272 */ 273 public void setPage(int page) { 274 this.page = page; 275 } 276 277 /** 278 * @return the totalPages 279 */ 280 public int getTotalPages() { 281 return totalPages; 282 } 283 284 /** 285 * @param totalPages 286 * the totalPages to set 287 */ 288 public void setTotalPages(int totalPages) { 289 this.totalPages = totalPages; 290 } 291 292 /** 293 * @return the pageRecorders 294 */ 295 public int getPageRecorders() { 296 return pageRecorders; 297 } 298 299 /** 300 * @param pageRecorders 301 * the pageRecorders to set 302 */ 303 public void setPageRecorders(int pageRecorders) { 304 this.pageRecorders = pageRecorders; 305 } 306 307 /** 308 * @return the totalRows 309 */ 310 public int getTotalRows() { 311 return totalRows; 312 } 313 314 /** 315 * @param totalRows 316 * the totalRows to set 317 */ 318 public void setTotalRows(int totalRows) { 319 this.totalRows = totalRows; 320 } 321 322 /** 323 * @return the pageStartRow 324 */ 325 public int getPageStartRow() { 326 return pageStartRow; 327 } 328 329 /** 330 * @param pageStartRow 331 * the pageStartRow to set 332 */ 333 public void setPageStartRow(int pageStartRow) { 334 this.pageStartRow = pageStartRow; 335 } 336 337 /** 338 * @return the pageEndRow 339 */ 340 public int getPageEndRow() { 341 return pageEndRow; 342 } 343 344 /** 345 * @param pageEndRow 346 * the pageEndRow to set 347 */ 348 public void setPageEndRow(int pageEndRow) { 349 this.pageEndRow = pageEndRow; 350 } 351 352 /** 353 * @return the hasNextPage 354 */ 355 public boolean isHasNextPage() { 356 return hasNextPage; 357 } 358 359 /** 360 * @param hasNextPage 361 * the hasNextPage to set 362 */ 363 public void setHasNextPage(boolean hasNextPage) { 364 this.hasNextPage = hasNextPage; 365 } 366 367 /** 368 * @return the list 369 */ 370 public List<T> getList() { 371 return list; 372 } 373 374 /** 375 * @param list 376 * the list to set 377 */ 378 public void setList(List<T> list) { 379 this.list = list; 380 } 381 382 /** 383 * @return the hasPreviousPage 384 */ 385 public boolean isHasPreviousPage() { 386 return hasPreviousPage; 387 } 388 389 /* 390 * 391 * @param args 392 */ 393 public static void main(String[] args) { 394 395 List<String> list = new ArrayList<String>(); 396 397 list.add("a"); 398 list.add("b"); 399 list.add("c"); 400 list.add("d"); 401 list.add("e"); 402 list.add("f"); 403 list.add("g"); 404 list.add("h"); 405 list.add("h"); 406 list.add("i"); 407 list.add("j"); 408 409 System.out.println(list.size()); 410 411 PageModel<String> pm = new PageModel<String>(list, 3);// 每页显示条数 412 413 pm.getObjects(1); 414 415 System.out.println(pm.displayForPage("disResult")); 416 } 417 418 }