zoukankan      html  css  js  c++  java
  • jsp的分页查询

    jsp页面步骤

    1.定义页面位置input类型为hidden的隐藏域名称为pageIndex初始值为1
    2.定义页面长度
    3.通过请求获得参数的方法传入pageIndex得到pageIndex字符串
    4.定义当前页面为1
    5.进行页面位置的非空判断 (1)为空则值为1 (2)否则值为页面位置的包装类
    6.定义总新闻数,通过实现类的getNewsCount()获得
    7.创建分页类的对象
    8.设置属性页面长度,当前页面,总新闻数,再通过分页对象的自定义方法得到总页数
    9.进行判断:当前页为1以下时,或者大于总页数
    10.定义总页数input类型为hidden的隐藏域名称为totalpagecount值为totalpagecont
    11.通过c标签导入页面,同时用c标签的name,value属性传递三个参数,总新闻数,当前页码,总页数

    接口

    //获取新闻信息总数
    public int getNewsCount();

    //得到分页列表
    public List<News> getPageNewsList(int pageNum,int pageSize);

    实现类

    @Override
    public int getNewsCount() {
    // TODO Auto-generated method stub
    int count=0;
    String sql="select count(*)as count from news_detail";
    Object[] obj={};
    if(this.getConnection()){
    ResultSet rs=this.executeSQL(sql, obj);
    try {
    if(rs.next()){
    count=rs.getInt("count");

    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    //释放资源
    this.closeResource();
    }
    }
    return count;

    }

    @Override
    public List<News> getPageNewsList(int pageNum, int pageSize) {
    // TODO Auto-generated method stub
    List<News> newsList=new ArrayList<News>();
    String sql="SELECT id,title,author,createDate FROM news_detail ORDER BY id ASC LIMIT ?,?";
    pageNum=(pageNum-1)*pageSize;
    Object[] params={pageNum,pageSize};
    if(this.getConnection()){
    ResultSet rs=executeSQL(sql, params);
    try {
    while(rs.next()){
    News news=new News();
    news.setId(rs.getInt("id"));
    news.setTitle(rs.getString("title"));
    news.setAuthor(rs.getString("author"));
    news.setCreateDate(rs.getTimestamp("createDate"));
    newsList.add(news);
    }
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally {
    //释放资源
    this.closeResource();
    }
    }
    return newsList;
    }

    对象类

    //总页数
    private int totalpageCount=1;
    //页面容量
    private int pageSize=0;

    //总记录数
    private int totalCount=0;

    //当前页码
    private int currentPageNo=1;


    public int getTotalpageCount() {
    return totalpageCount;
    }

    public void setTotalpageCount(int totalpageCount) {
    this.totalpageCount = totalpageCount;
    }

    public int getTotalCount() {
    return totalCount;
    }

    public void setTotalCount(int totalCount) {
    if(totalCount>0){
    this.totalCount = totalCount;
    this.setTotalPageCountByRs();
    }
    }

    public int getCurrentPageNo() {
    return currentPageNo;
    }

    public void setCurrentPageNo(int currentPageNo) {
    if(currentPageNo>0)
    this.currentPageNo = currentPageNo;
    }

    public int getPageSize() {
    return pageSize;
    }

    public void setPageSize(int pageSize) {
    if(pageSize>0)
    this.pageSize = pageSize;
    }

    public void setTotalPageCountByRs(){
    if(this.totalCount%this.pageSize==0){
    this.totalpageCount=this.totalCount/this.pageSize;
    }else if(this.totalCount%this.pageSize>0){
    this.totalpageCount=this.totalCount/this.pageSize+1;
    }else{
    this.totalpageCount=0;
    }
    }

    jsp页面

    <%@page import="com.pb.news.entity.News"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@page import="com.pb.news.util.PageSupport"%>
    <%@include file="../../common/common.jsp" %>
    <script type="text/javascript">

    function addNews(){
    window.location="newsDetailCreateSimple.jsp";
    }
    function page_nav(frm,num){
    frm.pageIndex.value=num;
    frm.submit();
    }
    function jump_to(frm,num){
    var regexp=/^[1-9]d*$/;
    var totalpagecount= document.getElementById("totalpagecount");
    if(!regexp.test(num)){
    alert("输入正整数");
    return false;
    }else if(num>totalpagecount){
    alert("超过总页数");
    return false;
    }else{
    page_nav(frm,num);
    }

    }

    </script>

    <input type="hidden" name="pageIndex" value="1"/>

    <%

    //页面容量
    int pageSize=5;

    //当前页码
    String pageIndex=request.getParameter("pageIndex");
    int currentPageNo=1;
    if(pageIndex==null){
    currentPageNo=1;
    }else{

    try{
    currentPageNo=Integer.parseInt(pageIndex);
    }catch(NumberFormatException e){
    response.sendRedirect("error.jsp");
    }
    }



    //总记录数
    int totalCount=newsService.getNewsCount();

    //总页数
    PageSupport pageSupport=new PageSupport();
    pageSupport.setPageSize(pageSize);
    pageSupport.setCurrentPageNo(currentPageNo);
    pageSupport.setTotalCount(totalCount);
    int totalpagecount=pageSupport.getTotalpageCount();
    if(currentPageNo<1){
    currentPageNo=1;
    }
    if(currentPageNo>totalpagecount){
    currentPageNo=totalpagecount;
    }



    List<News> newsList=newsService.getPageNewsList(currentPageNo,pageSize);
    int i=0;
    for(News news:newsList){
    i++;
    %>
    <tbody>
    <input type="hidden" id="totalpagecount" name="totalpagecount" value=<%=totalpagecount %>/>
    <tr <%if(i%2!=0){%>class="admin-list-td-h2"<%} %>>
    <td><a href='adminNewsView.jsp?id=<%=news.getId() %>'><%=news.getTitle() %></a></td>
    <td><%=news.getAuthor()%></td>
    <td><%=news.getCreateDate() %></td>
    <td><a href='adminNewsCreate.jsp?id=<%=news.getId() %>'>修改</a>
    <a href="javascript:if(confirm('确认是否删除此新闻?')) location='adminNewsDel.jsp?id=<%=news.getId() %>'">删除</a>
    </td>
    </tr>
    </tbody>
    <%
    }
    %>

    </table>
    <div class="page-bar">
    <ul class="page-num-ul clearfix">
    <li>共<%=totalCount %></>条记录&nbsp;&nbsp; <%=currentPageNo%>/<%=totalpagecount %>页</li>
    <%if(currentPageNo>1){%>
    <a href="javascript:page_nav(document.forms[0],1)">首页</a>
    <a href="javascript:page_nav(document.forms[0],<%=currentPageNo-1%>)">上一页</a>
    <%}if(currentPageNo<totalpagecount){ %>
    <a href="javascript:page_nav(document.forms[0],<%=currentPageNo+1%>)">下一页</a>
    <a href="javascript:page_nav(document.forms[0],<%=totalpagecount%>)">最后一页</a>&nbsp;&nbsp;
    <%} %>
    </ul>
    <span class="page-go-form"><label>跳转至</label>
    <input type="text" name="inputPage" id="inputPage" class="page-key" />页
    <button type="button" class="page-btn" onClick='jump_to(document.forms[0],document.getElementById("inputPage").value)'>GO</button>
    </span>
    </div>
    </div>

  • 相关阅读:
    Apache ServiceComb Pack 微服务分布式数据最终一致性解决方案
    EF core 性能调优
    Entity Framework Core Query Types
    Using the Repository and Unit Of Work Pattern in .net core
    CENTOS 7 下安装 REDIS 5.0.6 完整步骤
    MySQL InnoDB 群集–在Windows上设置InnoDB群集
    RFM客户价值分类
    修改meta标签
    $.Deferred 使用 (支持jQuery1.5版本以上)
    error: Unexpected console statement (no-console) 解决办法
  • 原文地址:https://www.cnblogs.com/m97i/p/6936095.html
Copyright © 2011-2022 走看看