JSP&EL&JSTL(案例1)_查询所有用户信息_4_分页显示

分页显示

1. 从BootStrap文档中查询组件分页条,将组件添加到list.jsp页面的最下面

2. 建立一个JavaBean,起名为PageBean,PageBean用于存放页面的一些信息

    private int totalCount; //总的记录数
    private int totalPage; //总页码
    private List<T> list; //每页的数据
    private int currentPage; //当前页码
    private int rows; //每页显示的记录数

添加其setter和getter和toString方法

3. 在分页条部分的所有a超链接处,href属性为 ${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage}&rows=5

4. 写一个Servlet为FindUserByPageServlet,在Servlet中,获取request中的currentPage和rows参数分别返回为currentPage和rows字符串,判断两个值是否为空,如果为空,那么分别命其为“1” 和 “5”,调用service层中的findUserByPage方法,如下,参数分别为currentPage,rows

PageBean<User> pb=service.findUserByPage(currentPage,rows);

5. 在service层,findUserByPage方法中,创建一个空的PageBean<User>对象,在对象中设置参数,分别设置其currentPage和rows值,调用dao层的findTotalCount方法,将返回的值设置到PageBean<User>对象的totalCount中,调用findByPage方法,传入的参数为 dao.findByPage(start,rows); ,start参数为开始查询的编号,为(currentPage-1)*rows

6. 在dao层,findTotalCount调用sql语句查询出数据库表中的记录总数,findByPage方法,调用相应sql语句,查询出start开始,rows条记录,返回为list集合

 

代码实现

分页条,添加了判断是否是为第一页,当为第一页之后上一页按钮禁用,并且点击还是处在第一页,若为最后一页,下一页按钮禁用,并且点击后还是处在最后一页

<div>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            <c:if test="${pb.currentPage==1}">
                <li class="disabled">
            </c:if>
            <c:if test="${pb.currentPage!=1}">
                <li>
            </c:if>
                <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage-1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
                    <span aria-hidden="true">&laquo;</span>
                </a>
             </li>

            <c:forEach begin="1" end="${pb.totalPage}" var="i">
                <c:if test="${pb.currentPage == i}">
                    <li class="active"><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>

                </c:if>
                <c:if test="${pb.currentPage != i}">
                    <li><a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>

                </c:if>
            </c:forEach>
            <c:if test="${pb.currentPage==pb.totalPage}">
                <li class="disabled">
                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>
            <c:if test="${pb.currentPage!=pb.totalPage}">
                <li>
                    <a href="${pageContext.request.contextPath}/findUserByPageServlet?currentPage=${pb.currentPage+1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>


            <span style="font-size: 25px;margin-left: 5px">
                共${pb.totalCount}条记录,共${pb.totalPage}页
            </span>
        </ul>
    </nav>
</div>

JavaBean类

package zr.web.userlist.domain;

import java.util.List;

/**
 * 分页工具对象
 */
public class PageBean<T> {
    private int totalCount; //总的记录数
    private int totalPage; //总页码
    private List<T> list; //每页的数据
    private int currentPage; //当前页码
    private int rows; //每页显示的记录数

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public List<T> getList() {
        return list;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getRows() {
        return rows;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "totalCount=" + totalCount +
                ", totalPage=" + totalPage +
                ", list=" + list +
                ", currentPage=" + currentPage +
                ", rows=" + rows +
                '}';
    }
}

FindUserByPageServlet

package zr.web.userlist.web.Servlet;

import zr.web.userlist.domain.PageBean;
import zr.web.userlist.domain.User;
import zr.web.userlist.service.UserService;
import zr.web.userlist.service.impl.UserServiceImpl;

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;
import java.util.Map;

@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //1. 获取参数
        String currentPage = request.getParameter("currentPage");  //当前页码

        String rows = request.getParameter("rows"); //每页显示的记录数

        if (currentPage == null ||"".equals(currentPage)){
                currentPage="1";



        }
        if (rows == null ||"".equals(rows)){
            rows="5";

        }



        //2. 调用service查询

        UserService service=new UserServiceImpl();
        PageBean<User> pb=service.findUserByPage(currentPage,rows);
        //3. 将PageBean存入request
        request.setAttribute("pb",pb);
        //4. 转发到list.jsp
        request.getRequestDispatcher("/list.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

service层

接口

    /**
     * 分页查询条件查询
     *
     *
     * 
     * @param rows
     * @param condition
     * @return
     */
    PageBean<User> findUserByPage(String currentPage, String rows);

实现

    @Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows) {
        int currentPage=Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);

        if (currentPage<=0){
            currentPage=1;
        }

        //1. 创建空的PageBean对象
        PageBean<User> pb=new PageBean<User>();
        //2. 设置参数

        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //3. 调用dao查询总记录数

        int totalCount=dao.findTotalCount();
        pb.setTotalCount(totalCount);
        //4. 调用dao查询List集合
        //计算开始记录的索引
        int start =(currentPage-1)*rows;
        List<User> list=dao.findByPage(start,rows);
        pb.setList(list);

        //5. 计算总页码

        int totalPage= (totalCount % rows) == 0 ?  (totalCount / rows) : (totalCount / rows + 1);
        pb.setTotalPage(totalPage);
        return pb;
    }

dao层

接口

    /**
     * 查询总记录数
     * @return
     * 
     */
    int findTotalCount(Map<String, String[]> condition);

    /**
     * 分页查询每一页的记录
     * @param start
     * @param rows
     * 
     * @return
     */
    List<User> findByPage(int start, int rows);

实现

    @Override
    public int findTotalCount() {

        //1. 定义模板初始化sql
        String sql="select count(*) from user";
        

        return template.queryForObject(sql,Integer.class);
    }

    @Override
    public List<User> findByPage(int start, int rows) {
        String sql="select * from user limit ?,?");
        //添加分页查询参数值
        return template.query(sql,new BeanPropertyRowMapper<User>(User.class),start,rows);

    }
}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值