add<产品分页>
parent
032385d7a7
commit
e2b6123de8
|
@ -13,6 +13,7 @@ import java.io.IOException;
|
|||
import java.io.UnsupportedEncodingException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@WebServlet(urlPatterns = {"/computer"})
|
||||
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 {
|
||||
List<Computer> computers= computerService.listComputer();
|
||||
req.setAttribute("computers", computers);
|
||||
int page = 1;
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
String action = req.getParameter("action");
|
||||
|
|
|
@ -41,11 +41,21 @@ public class ComputerDao {
|
|||
}
|
||||
}
|
||||
|
||||
public List<Computer> getAllComputer() throws SQLException {
|
||||
String sql = "select * from computer";
|
||||
return queryRunner.query(sql,new BeanListHandler<>(Computer.class));
|
||||
public List<Computer> getComputersByPage(int page, int pageSize) throws SQLException {
|
||||
int offset = (page - 1) * pageSize;
|
||||
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) {
|
||||
String sql = "select * from computer where id=?";
|
||||
|
|
|
@ -4,6 +4,7 @@ import example.model.Computer;
|
|||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface IComputerService {
|
||||
|
||||
|
@ -16,4 +17,6 @@ public interface IComputerService {
|
|||
Computer getComputer(String id);
|
||||
|
||||
void updateComputer(String id, String name, double v, int i);
|
||||
|
||||
Map<String, Object> listComputersWithPagination(int page, int pageSize) throws SQLException;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,10 @@ import example.model.Computer;
|
|||
import example.service.IComputerService;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ComputerServiceImpl implements IComputerService {
|
||||
|
||||
|
@ -19,9 +22,10 @@ public class ComputerServiceImpl implements IComputerService {
|
|||
|
||||
@Override
|
||||
public List<Computer> listComputer() throws SQLException {
|
||||
return computerDao.getAllComputer();
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteComputer(String 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) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -44,6 +44,31 @@
|
|||
</c:forEach>
|
||||
</tbody>
|
||||
</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>
|
||||
<p style="color: red;">${requestScope.msg}</p>
|
||||
|
||||
|
|
Reference in New Issue