JSP实现分页功能(同步请求)

1.jsp代码

 <script>
        function select() {
            var val = $("#inputcount").val();
            window.location.href = "${pageContext.request.contextPath}/querypage?everyCount="+val;
        }
    </script>
  
<%--页面实现代码--%>
<body>
<div class="container">
    <div class="row">
        <span style="margin-left: 20px"> 每页显示</span>
        <input type="text" style="height:30px;width:30px;border-radius: 8px" class="text-left text-info"
               id="inputcount" value="10"> <span>条</span>
        <button class="btn btn-success" οnclick="select()">查询</button>
        <h3 style="text-align: center">我的所有项目</h3>
    </div>

    <table border="1" class="table table-bordered table-hover table-responsive text-center">
        <tr class="success text-center">
            <th></th>
            <th>编号</th>
            <th>项目名称</th>
            <th>项目ID</th>
            <th>项目类型</th>
            <th>创建时间</th>
            <th>项目路径</th>
            <th colspan="2">操作</th>
        </tr>

        <c:forEach var="h" items="${pageBean.list}" varStatus="vs">
            <tr>
                <td><input type="checkbox"></td>
                <td>${vs.count}</td>
                <td>${h.pName}</td>
                <td>${h.id}</td>
                <td>${h.pType}</td>
                <td>${h.date}</td>
                <td>${h.pPath}</td>
                <td>
                    <a class="btn btn-default btn-sm" href="#">修改</a>&nbsp;
                    <a class="btn btn-default btn-sm">删除</a>
                </td>
            </tr>
        </c:forEach>

        <tr>
            <td colspan="8" align="center">
                <ul class="pagination success">

                    <%--此处判断当前页是否是第一页,如果是第一页,上一页按钮不能用--%>
                    <c:if test="${pageBean.currentPage==1}">
                        <li class="disabled">
                            <a href="#" aria-label="Previous"><span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    </c:if>
                    <c:if test="${pageBean.currentPage!=1}">
                        <li class="disabled">
                            <a href="${pageContext.request.contextPath}/querypage?currentPage=${pageBean.currentPage-1}&everyCount=${pageBean.everypagecount}"
                               aria-label="Previous"><span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                    </c:if>

                    <%--此处生成页号--%>
                    <c:forEach var="i" begin="1" end="${pageBean.countPage}" step="1">
                        <%--当前活动页面深色显示不能点击--%>
                        <c:if test="${pageBean.currentPage==i}">
                            <li class="active"><a
                                    href="${pageContext.request.contextPath}/querypage?currentPage=${i}&everyCount=${pageBean.everypagecount}">${i}</a></li>
                        </c:if>
                        <c:if test="${pageBean.currentPage!=i}">
                            <li><a href="${pageContext.request.contextPath}/querypage?currentPage=${i}&everyCount=${pageBean.everypagecount}">${i}</a></li>
                        </c:if>

                    </c:forEach>

                    <%--此处判断是否为最后一页,如果是最后一页,下一页按钮不能用--%>
                    <c:if test="${pageBean.currentPage==pageBean.countPage}">
                        <li><a class="disabled" href="#" aria-label="Next"><span aria-hidden="true">&raquo;</span></a>
                        </li>
                    </c:if>
                    <c:if test="${pageBean.currentPage!=pageBean.countPage}">
                        <li><a class="disabled"
                               href="${pageContext.request.contextPath}/querypage?currentPage=${pageBean.currentPage+1}&everyCount=${pageBean.everypagecount}"
                               aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
                    </c:if>

                </ul>
            </td>
        </tr>
    </table>
</div>
</body>    

Dao层代码

 /**
     * 查询当前页面的数据
     * @param currentPage 当前页
     * @param a           每页记录数
     * @return 当前页数据
     */
    public List<Project> queryCurrentPageData(int currentPage, int a) {
        String sql = "select * from project limit ?,? ";
        int begin = (currentPage - 1) * a;//当前页开始数据位置
        List<Project> currentProject = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Project.class),begin,a);
        return currentProject;
    }

service层实现代码

/**
     * 获取当前页的数据
     *
     * @param currentPage      当前页
     * @param currentCountPage 当前页要显示的记录数
     * @return
     */
    public PageBean queryPageProject(String currentPage, String currentCountPage) {
        int cp = Integer.parseInt(currentPage); //当前页
        int ccp = Integer.parseInt(currentCountPage); //每页数据数
        List<Project> currentPageList = projectOperationDao.queryCurrentPageData(cp, ccp); //当前页面用户数据

        //将项目数据封装到PageBean
        PageBean pageBean = new PageBean();
        pageBean.setList(currentPageList); //当前页数据
        pageBean.setCurrentPage(cp); //当前页

        //查询数据库记录总条数
        int pagecount = projectOperationDao.pagecount();
        int ceil = (int) Math.ceil(pagecount/ ccp);
        pageBean.setCountPage(ceil); //总页数
        pageBean.setEverypagecount(ccp);
        return pageBean;
    }

4.web层实现代码(servlet的实现)

@WebServlet(name = "querypage", urlPatterns = "/querypage")
public class PageQueryServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取每页显示的条数,如果未指定每页显示数目,则默认显示十条
        String everyCount = request.getParameter("everyCount");
        if (everyCount==null||"".equals(everyCount)){
            everyCount="10";
        }

        //获取当前页
        String currentPage = request.getParameter("currentPage");
        if (currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }

        //调用查询方法查询每页数据
        ProjectOperationService projectOperationService = new ProjectOperationService();
        PageBean pageBean = projectOperationService.queryPageProject(currentPage, everyCount);

        request.setAttribute("pageBean",pageBean);
        request.getRequestDispatcher("/page/testpage/pagelist.jsp").forward(request,response);
    }
}

5.JavaBean

package com.sps.entity;
import java.util.List;
public class PageBean {
    private List<Project> list;  //每页的项目数据
    private int currentPage; //当前页
    private int countPage;   //总页数
    private int everypagecount; //每页显示的记录数

    public PageBean() {
    }

    public PageBean(List<Project> list, int currentPage, int countPage, int everypagecount) {
        this.list = list;
        this.currentPage = currentPage;
        this.countPage = countPage;
        this.everypagecount = everypagecount;
    }
    public int getEverypagecount() {
        return everypagecount;
    }

    public void setEverypagecount(int everypagecount) {
        this.everypagecount = everypagecount;
    }

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

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

    public int getCurrentPage() {
        return currentPage;
    }

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

    public int getCountPage() {
        return countPage;
    }

    public void setCountPage(int countPage) {
        this.countPage = countPage;
    }

    @Override
    public String toString() {
        return "PageBean{" +
                "list=" + list +
                ", currentPage=" + currentPage +
                ", countPage=" + countPage +
                ", everypagecount=" + everypagecount +
                '}';
    }
}

6.util工具类和配置文件(此Demo用的是c3p0连接池和mysql数据库,可根据个人情况修改配置文件,此处包括util工具类和配置文件)

/**
 * 目的:
 * 1. 保证DataSource只有一个
 * 2. 提供连接(DataSource获得)
 * 3. 释放资源
 */
public class C3P0Utils {

    //创建C3P0数据源(连接池)
    private static DataSource dataSource = new ComboPooledDataSource();

    /**
     * 提供数据源
     * @return
     */
    public static DataSource getDataSource(){
        return  dataSource;
    }

    /**
     * 从dataSource(连接池)获得连接对象
     *
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
        Connection connection = dataSource.getConnection();
        return  connection;
    }

    /**
     * 释放资源
     *
     * @param resultSet
     * @param statement
     * @param connection
     */
    public static void release(ResultSet resultSet, Statement statement, Connection connection) {
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();//看Connection来自哪里, 如果Connection是从连接池里面获得的, close()方法其实是归还; 如果Connection是创建的, 就是销毁
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}


---------------------------------以下是配置文件----------------------------------------
    <c3p0-config>
    <!--配置方式一:使用默认配置(default-config)读取连接池对象-->
    <default-config>
        <!--连接参数,必须要写的-->
        <property name="driverClass"> com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/mysystem </property>
        <property name="user">root</property>
        <property name="password">339869</property>

        <!-- 连接池参数 -->
        <property name="initialPoolSize">7</property>
        <!--初始连接数:刚创建好连接池的时候准备的连接数量-->
        <property name="maxPoolSize">10</property>
        <!--最大连接数:连接池中最多可以放多少个连接-->
        <property name="cheakoutTimeout">1000</property>
        <!--最大等待时间:连接池中没有连接时最长等待时间-->
        <property name="maxIdleTime">2000</property>
        <!--最大空闲回收时间:连接池中的空闲连接多久没有使用就会回收-->
    </default-config>

</c3p0-config>

7.实现效果图

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章