ssm管理系统文件中util文件里PageHelper类解释以下代码package com.util; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; public class PageHelper { public static void getPage(List<?> list, String name, List<String> nameList, List<String> valueList, int pageSize, String number, HttpServletRequest request, String method) { StringBuffer buffer = new StringBuffer(); String name2 = name.substring(0, 1).toUpperCase() + name.substring(1); String path = ""; String action = "getAll" + name2 + ".action"; if (method != null) { action = "query" + name2 + "ByCond.action"; } List<Object> objList = new ArrayList<Object>(); if (nameList != null && valueList != null) { for (int i = 0; i < nameList.size(); i++) { path += "&" + nameList.get(i) + "=" + valueList.get(i); } } int pageNumber = list.size(); int maxPage = pageNumber; if (maxPage % pageSize == 0) { maxPage = maxPage / pageSize; } else { maxPage = maxPage / pageSize + 1; } if (number == null) { number = "0"; } int start = Integer.parseInt(number) * pageSize; int over = (Integer.parseInt(number) + 1) * pageSize; int count = pageNumber - over; if (count <= 0) { over = pageNumber; } for (int i = start; i < over; i++) { Object obj = list.get(i); objList.add(obj); } buffer.append(" 共为"); buffer.append(maxPage); buffer.append("页 共有"); buffer.append(pageNumber); buffer.append("条 当前为第"); buffer.append((Integer.parseInt(number) + 1)); buffer.append("页 "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("首页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=0" + path + "\">首页</a>"); } buffer.append(" "); if ((Integer.parseInt(number) + 1) == 1) { buffer.append("上一页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (Integer.parseInt(number) - 1) + "" + path + "\">上一页</a>"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("下一页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (Integer.parseInt(number) + 1) + "" + path + "\">下一页</a>"); } buffer.append(" "); if (maxPage <= (Integer.parseInt(number) + 1)) { buffer.append("尾页"); } else { buffer.append("<a href=\"" + name + "/" + action + "?number=" + (maxPage - 1) + "" + path + "\">尾页</a>"); } String html = buffer.toString(); request.setAttribute("html", html); request.setAttribute(name + "List", objList); }
时间: 2023-06-10 18:07:43 浏览: 88
The `PageHelper` class in the `util` package of the SSM management system is used to implement pagination functionality for displaying data in pages.
The `getPage` method takes the following parameters:
- `list`: the list of objects to be paginated
- `name`: the name of the entity being paginated
- `nameList`: a list of parameter names used for filtering the data
- `valueList`: a list of parameter values used for filtering the data
- `pageSize`: the number of objects to be displayed per page
- `number`: the current page number
- `request`: the HTTP servlet request object
- `method`: the name of the method used for filtering the data
The method first constructs the URL for the pagination links by appending the `action` parameter to the entity name. If a filter method is provided, it replaces the default `getAll` method with the filter method.
Next, it creates a new list to hold the paginated data and calculates the total number of pages based on the size of the original data list and the page size. It then adds the relevant parameters to the URL for each page and constructs the HTML code for the pagination links.
Finally, it sets the paginated data list and pagination HTML code as attributes of the request object to be passed back to the view for display.
阅读全文