页面显示:
<body>
<table align="center" border="0" width="85%">
<tr bgcolor="#6699FF" width="30">
<td>产品编号</td>
<td>产品名称</td>
<td>产品价格</td>
</tr>
<c:forEach var="g" items="${goods}" begin="${pageNum*4}" end="${(pageNum+1)*4-1}">
<tr bgcolor="#DDDDDD" width="410">
<td>${g.id }</td>
<td>${g.name }</td>
<td>${g.price }</td>
</tr>
</c:forEach>
<tr bgcolor="#6699FF" width="30">
<td colspan="3">
<a href="ShowProductServlet?pageNum=0">[首页]</a>
<c:if test="${pageNum>0}">
<a href="ShowProductServlet?pageNum=${pageNum-1 }">[上一页]</a>
</c:if>
<c:if test="${pageNum==0}">
[上一页]
</c:if>
<c:if test="${pageNum<maxPage-1}">
<a href="ShowProductServlet?pageNum=${pageNum+1 }">[下一页]</a>
</c:if>
<c:if test="${pageNum==maxPage-1}">
[下一页]
</c:if>
<a href="ShowProductServlet?pageNum=${maxPage-1 }">[尾页]</a>
</td>
</tr>
</table>
</body>
带样式的!!
通过上边的a标签,调用servlet类进行逻辑操作
import java.io.*;
import java.util.List;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* 4. 控制层开发
*
*/
public class ShowProductServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
List<Product> lst = new BaseDao().selectAll();
// 接收分页页面传递过来的页面数
String strNum = request.getParameter("pageNum");
int pageNum = 0;// 表示当前要显示的页面数
int maxPage = 0;// 最大页
int pageCount = lst.size();// 得到查询出来的所有数据的总数目
// 如果是第一次执行,就会接收不到数据
if (strNum == null) {
strNum = "0";
} else {// 接收到了用户点击的第几(pageNum)页
pageNum = Integer.parseInt(strNum);
}
// 计算出要分多少页(每页显示4条信息)
if (pageCount % 4 == 0) {
maxPage = pageCount / 4;
} else {
maxPage = pageCount / 4 + 1;
}
request.setAttribute("maxPage", maxPage);// 存储最大页数
request.setAttribute("pageNum", pageNum);// 将当前页面存储起来,给分页页面使用
request.setAttribute("goods", lst);// 存储要显示的数据
request.getRequestDispatcher("showProduct.jsp").forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
}