分页(调用数据库的数据,在页面进行分页)(a标签调用servlet)

页面显示:

<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);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值