MVC模式实现搜索分页,含有首页、上一页、[1][2][3][4]、下一页

首先考虑搜索分页的内容,我分页的内容为二手商品,前台的JSP代码为:

第${ PageCode }页/共${ TotalPage }页 首页 上一页 <%--如果totalPage <= 10 --%> <%--算法:begin=pageCode -5 end = pageCode +4 --%> <%--头溢出 尾部溢出 --%> <%--目的:先1-10 for(int i=1;i<=10;i++) --%> [${ i }] 下一页 尾页

然后建立Page的JavaBean,代码如下:

package javaBean;

import java.util.List;
public class Page {
    // 包含5个属性
    // 当前页
    private int pageCode;
    // 总页数 = 总记录数/每页显示的记录条数
    // private int totalPage;
    // 总记录数
    private int totalCount;
    // 每页显示的记录条数
    private int pageSize;
    // 每页显示的数据
    private List GoodsBeanList;
    private List HouseBeanList;
    private List GoodsOrderBeanList;
    private List SignHouseBeanList;

    public int getPageCode() {
        return pageCode;
    }
    public void setPageCode(int pageCode) {
        this.pageCode = pageCode;
    }

    // totalPage 也是PageBean的一个属性
    // 通过计算,获取总页面
    public int getTotalPage() {
        // 获取总页数,如果100条,一页显示10条,页数10,如果109条,是11页
        int totalPage = totalCount / pageSize;
        if(totalCount % pageSize == 0){
            return totalPage;
        }else{
            return totalPage + 1;
        }
    }
   /*public void setTotalPage(int totalPage) {
      this.totalPage = totalPage;
   }*/
    public int getTotalCount() {
        return totalCount;
    }
    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }
    public int getPageSize() {
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public List getGoodsBeanList() {
        return GoodsBeanList;
    }

    public void setGoodsBeanList(List GoodsBeanList) {
        this.GoodsBeanList = GoodsBeanList;
    }

    public List getHouseBeanList() {
        return HouseBeanList;
    }

    public void setHouseBeanList(List houseBeanList) {
        HouseBeanList = houseBeanList;
    }

    public List getGoodsOrderBeanList() {
        return GoodsOrderBeanList;
    }

    public void setGoodsOrderBeanList(List goodsOrderBeanList) {
        GoodsOrderBeanList = goodsOrderBeanList;
    }

    public List getSignHouseBeanList() {
        return SignHouseBeanList;
    }

    public void setSignHouseBeanList(List signHouseBeanList) {
        SignHouseBeanList = signHouseBeanList;
    }
}

之后建立GoodsDivideByPageServlet,代码如下:

package servlet.Goods;

import javaBean.Goods;
import javaBean.User;
import services.GoodsServices;
import javaBean.Page;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/listByPage")
public class GoodsDivideByPageServlet extends HttpServlet{

    GoodsServices goodsServices = new GoodsServices();
//    HouseService houseServices = new HouseService();
    public int getPageCode(HttpServletRequest request){
        // 先获取从客户端传过来的参数
        String pc = request.getParameter("pc");
        // 进行判断
        if(pc == null || pc.trim().isEmpty()){
            // 返回第一页
            return 1;
        }
        return Integer.parseInt(pc);
    }

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        int pageCode = getPageCode(request);
        int pageSize = 3;

        User user = (User)request.getSession().getAttribute("my");
        if(user==null)
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        Page page = goodsServices.findByPage( pageCode, pageSize ,user.getUid());
        page.setHouseBeanList(null);
//不将page建立session
        request.setAttribute("page",page);
// 不将page.getGoodsBeanList()建立session
        request.setAttribute("goodss",page.getGoodsBeanList());
        request.setAttribute("PageCode",page.getPageCode());
        request.setAttribute("PageSize",page.getPageSize());
        request.setAttribute("TotalPage",page.getTotalPage());
        request.getRequestDispatcher("/JSP/user/userInfo.jsp").forward(request,response);
    }
}

Service层代码为:

public Page findByPage(int pageCode, int pageSize , String  uid){
    return goodsDao.findByPage(pageCode, pageSize, uid);
}

Dao层代码为:

public Page findByPage(int pageCode, int pageSize , String uid){
        // 创建PageBean对象
        Page page = new Page();
        page.setPageCode(pageCode);
        page.setPageSize(pageSize);
        // totalPage  计算不用处理
        // totalCount 总记录数
        // 通过uid就可以搜索他发布的二手商品,也就是说要
        String SQL = " select count(*)  from " + tableName + " where uid = ? ";
        try {
            long count;
            ResultSet rs = JDBCHelper.query( SQL , uid) ;
            if(rs.next()) {
                count = rs.getInt(1);
                page.setTotalCount((int) count);
            }
            // beanList  数据
//      通过uid就可以搜索他发布的二手商品,也就是说要 select * from usedGoods where uid = ?  order by uid  desc limit ? , ?
            String GoodsSQL = " select * from  " + tableName + "  where uid = ?  order by  uid  desc limit ? , ? ";
            ResultSet GoodsList = JDBCHelper.query(GoodsSQL,uid, (pageCode-1)*pageSize, pageSize);
            List GoodsBeanList = wrapList(GoodsList);
            // 把每页显示的数据设置成功了
            page.setGoodsBeanList(GoodsBeanList);
            // 所有的数据全部都封装成功了!!
            return page;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
public List  wrapList ( ResultSet rs ){
    Goods g = null ;
    if( rs != null ){
        List goods = new ArrayList<>();
        try {
            while (rs.next()) {
                g = new Goods();
                g.setGid(rs.getInt("gid"));
                g.setType(rs.getString("type"));
                g.setName(rs.getString("name"));
                g.setPrice(rs.getInt("price"));
                g.setDescription(rs.getString("description"));
                g.setGstatus(rs.getInt("gstatus"));
                g.setUid(rs.getString("uid"));
                g.setGdate(rs.getDate("gdate"));
                g.setGimage(rs.getString("gimage"));
                g.setOld(rs.getString("old"));
                g.setColor(rs.getString("color"));
                goods.add(g);
            }
        }catch( SQLException e){
            e.printStackTrace();
        }
        return goods ;
    }
    return null;

以上为实现分页效果代码

你可能感兴趣的:(分页,JavaWeb)