add<产品分页>

master
liyansheng 2024-12-22 01:38:37 +08:00
parent 032385d7a7
commit e2b6123de8
5 changed files with 80 additions and 6 deletions

View File

@ -13,6 +13,7 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = {"/computer"}) @WebServlet(urlPatterns = {"/computer"})
public class ComputerServlet extends HttpServlet { public class ComputerServlet extends HttpServlet {
@ -64,11 +65,26 @@ public class ComputerServlet extends HttpServlet {
} }
private void listServlet(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException { private void listServlet(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
List<Computer> computers= computerService.listComputer(); int page = 1;
req.setAttribute("computers", computers); int pageSize = 5; // 每页显示的条数
// 获取页码参数
String pageParam = req.getParameter("page");
if (pageParam != null) {
page = Integer.parseInt(pageParam);
}
// 获取分页数据
Map<String, Object> data = computerService.listComputersWithPagination(page, pageSize);
req.setAttribute("computers", data.get("computers"));
req.setAttribute("totalPages", data.get("totalPages"));
req.setAttribute("currentPage", data.get("currentPage"));
req.getRequestDispatcher("computerList.jsp").forward(req, resp); req.getRequestDispatcher("computerList.jsp").forward(req, resp);
} }
@Override @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String action = req.getParameter("action"); String action = req.getParameter("action");

View File

@ -41,11 +41,21 @@ public class ComputerDao {
} }
} }
public List<Computer> getAllComputer() throws SQLException { public List<Computer> getComputersByPage(int page, int pageSize) throws SQLException {
String sql = "select * from computer"; int offset = (page - 1) * pageSize;
return queryRunner.query(sql,new BeanListHandler<>(Computer.class)); String sql = "SELECT * FROM computer LIMIT ? OFFSET ?";
return queryRunner.query(sql, new BeanListHandler<>(Computer.class), pageSize, offset);
} }
public int getComputerCount() throws SQLException {
String sql = "SELECT COUNT(*) FROM computer";
return queryRunner.query(sql, rs -> {
rs.next();
return rs.getInt(1);
});
}
public Computer getComputerById(int i) { public Computer getComputerById(int i) {
String sql = "select * from computer where id=?"; String sql = "select * from computer where id=?";

View File

@ -4,6 +4,7 @@ import example.model.Computer;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map;
public interface IComputerService { public interface IComputerService {
@ -16,4 +17,6 @@ public interface IComputerService {
Computer getComputer(String id); Computer getComputer(String id);
void updateComputer(String id, String name, double v, int i); void updateComputer(String id, String name, double v, int i);
Map<String, Object> listComputersWithPagination(int page, int pageSize) throws SQLException;
} }

View File

@ -5,7 +5,10 @@ import example.model.Computer;
import example.service.IComputerService; import example.service.IComputerService;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class ComputerServiceImpl implements IComputerService { public class ComputerServiceImpl implements IComputerService {
@ -19,9 +22,10 @@ public class ComputerServiceImpl implements IComputerService {
@Override @Override
public List<Computer> listComputer() throws SQLException { public List<Computer> listComputer() throws SQLException {
return computerDao.getAllComputer(); return Collections.emptyList();
} }
@Override @Override
public void deleteComputer(String id) { public void deleteComputer(String id) {
computerDao.deleteComputer(Integer.parseInt(id)); computerDao.deleteComputer(Integer.parseInt(id));
@ -36,4 +40,20 @@ public class ComputerServiceImpl implements IComputerService {
public void updateComputer(String id, String name, double v, int i) { public void updateComputer(String id, String name, double v, int i) {
computerDao.updateComputer(name, v, i, Integer.parseInt(id)); computerDao.updateComputer(name, v, i, Integer.parseInt(id));
} }
@Override
public Map<String, Object> listComputersWithPagination(int page, int pageSize) throws SQLException {
List<Computer> computers = computerDao.getComputersByPage(page, pageSize);
int totalCount = computerDao.getComputerCount();
int totalPages = (int) Math.ceil((double) totalCount / pageSize);
Map<String, Object> result = new HashMap<>();
result.put("computers", computers);
result.put("totalPages", totalPages);
result.put("currentPage", page);
return result;
}
} }

View File

@ -44,6 +44,31 @@
</c:forEach> </c:forEach>
</tbody> </tbody>
</table> </table>
<div>
<!-- 首页链接 -->
<a href="/computer?action=list&page=1">首页</a>
<!-- 上一页链接 -->
<c:if test="${currentPage > 1}">
<a href="/computer?action=list&page=${currentPage - 1}">上一页</a>
</c:if>
<!-- 分页页码 -->
<c:forEach var="i" begin="1" end="${totalPages}">
<a href="/computer?action=list&page=${i}"
style="margin: 0 5px; ${i == currentPage ? 'font-weight:bold;' : ''}">
${i}
</a>
</c:forEach>
<!-- 下一页链接 -->
<c:if test="${currentPage < totalPages}">
<a href="/computer?action=list&page=${currentPage + 1}">下一页</a>
</c:if>
<!-- 尾页链接 -->
<a href="/computer?action=list&page=${totalPages}">尾页</a>
</div>
<hr> <hr>
<p style="color: red;">${requestScope.msg}</p> <p style="color: red;">${requestScope.msg}</p>