• 分页查询


    m页数 n条数 n*m-1   

    总页数      如果总条数不可以 整除 当前页数显示的条数  就+1

          反之整出后的结果就是总页数

    上一页  parseInt(获取当前页数)-1 

    下一页   parseInt(获取当前页数)+1 

    Page实体类

    public class Page {
        //当前页面码数
        private String pageNum;
        //当前页码显示几条
        private String pageSize;
        //总条数
        private int pageCount;
        //结果集
        private ResultSet rs;
        //将结果集放入list集合
        private List list;
    
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
    
        public int getPageCount() {
            return pageCount;
        }
    
        public void setPageCount(int pageCount) {
            this.pageCount = pageCount;
        }
    
        public ResultSet getRs() {
            return rs;
        }
    
        public void setRs(ResultSet rs) {
            this.rs = rs;
        }
    
        public String getPageNum() {
            return pageNum;
        }
    
        public void setPageNum(String pageNum) {
            this.pageNum = pageNum;
        }
    
        public String getPageSize() {
            return pageSize;
        }
    
        public void setPageSize(String pageSize) {
            this.pageSize = pageSize;
        }
    }

    Dao层

    public interface SelectDao {public Page selectPage(Page page);
    }
    public class SelectDaoImpl extends BaseDao implements SelectDao {
      
        @Override
        public Page selectPage(Page page) {
            String sql = "SELECT `id`,`title`,`createDate`  FROM `news_detail` ";
            Page page1 = this.page(sql,page);
            return page1;
        }
    //
    }
    public interface SelectService {
    // 示例  public 返回值类型(int、Boolean,String)方法名(userLogin)( 括号里写 参数类型 和 参数名 示例 String name);
    public List selectAll();
    public Page selectPage(Page page);
    }
    public class SelectServiceImpl implements SelectService{
        @Override
        public Page  selectPage(Page page) {
            SelectDao selectDao = new SelectDaoImpl();
            Page page1 = selectDao.selectPage(page);
            ResultSet rs = page1.getRs();
            List newsList = new ArrayList();
            try {
                while (rs.next()){
                    NewsDetail newsDetail = new NewsDetail();
                    int id = rs.getInt("id");
                    String title = rs.getString("title");
                    Date createDate = rs.getTimestamp("createDate");
                    newsDetail.setId(id);
                    newsDetail.setTitle(title);
                    newsDetail.setCreateDate(createDate);
    
                    newsList.add(newsDetail);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
    
            page1.setList(newsList);
            page1.setRs(null);
            return page1;
        }
    //
    }

    Servlet

    @WebServlet(name = "SelectServlet", urlPatterns = "/SelectServlet")
    public class SelectServlet extends javax.servlet.http.HttpServlet {
        protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
            request.setCharacterEncoding("UTF-8");  // 处理post请求乱码问题
            doGet(request, response);
        }
    
        protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException {
            response.setContentType("text/html;charset=UTF-8"); // 处理响应乱码问题:字节流需getBytes("UTF-8")
            // str = new String(str.getBytes("ISO-8859-1"), "UTF-8");   // 处理get请求乱码问题
    
            // response.getWriter().write("你好 servlet!");
            String opr = request.getParameter("opr");
          if("selectPage".equals(opr)){
                String pageNum = request.getParameter("pageNum");
                //如果页数为空或者为0 默认 显示第一页
                if(pageNum==null||pageNum==""||"0".equals(pageNum)){
                    pageNum="1";
                }
                String pageSize = "1";
                Page page =new Page();
                page.setPageNum(pageNum);
                page.setPageSize(pageSize);
                SelectService selectService = new SelectServiceImpl();
                Page page1 =selectService.selectPage(page);
    
    
                String newsJSON = JSON.toJSONStringWithDateFormat(page1,"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteNullNumberAsZero);
    
                PrintWriter out = response.getWriter();
                out.print(newsJSON);
                out.flush();
                out.close();
            }
        }
    }

    jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false" %>
    
    <%
        String info = (String)request.getAttribute("info");
    %>
    
    <!DOCTYPE HTML>
    <html>
      <head>
        <title>My JSP 'doUserCreate.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <link rel="stylesheet" type="text/css" href="/css/common.css">
          <script type="text/javascript" src="js/jquery-1.12.4.js"></script>
          <script type="text/javascript">
              $(function (){
                  page(1);
    
                  $("#bId").click(function () {
                      var pageNum =$("#pageId").val();
                      page(pageNum);
                  });
                  $("#backId").click(function () {
    
                      $(this).attr("page", $("#numId").text()-1);
                      var pageNum= $(this).attr("page");
                      page(pageNum);
                  })
                  $("#nextId").click(function () {
    
                      $(this).attr("page", parseInt($("#numId").text())+1);
                      var pageNum= $(this).attr("page");
                      page(pageNum);
                  })
                  $("#firstId").click(function () {
                      page(1);
                  })
    
                  function page(pNum) {
                      var pageNum = pNum;
    //                  alert(pageNum);
                      $.getJSON("/SelectServlet","opr=selectPage&pageNum="+pageNum,callBack);
                      function callBack(data) {
                          $("#uId").empty();
    //                      alert(data.list);
                          $(data.list).each(function () {
    
                              $("#uId").append("<li>
    " +
                                  "                                <span>"+this.createDate+"</span>
    " +
                                  "                                <a href="/NewsServlet?id="+this.id+"&opr=selectById">"+this.title+"</a>
    " +
                                  "                            </li>")
                          })
                          $("#countId").text(data.pageCount);
                          $("#numId").text(data.pageNum);
                      }
                  }
              } )
    
          </script>
    
      </head>
    <body>
    <%--<%!String  name = "恰同学少年";%>--%>
    
    <%--<%=name%>--%>
    
    <c:set value="pageName" var="name" scope="page"></c:set>
    <c:set value="requestName" var="name" scope="request"></c:set>
    ${requestScope.name}
    <%--<c:remove var="name" scope="page"></c:remove>--%>
    ${pageScope.name}
    <!--页面顶部-->
    <div id="header">
        <input type="hidden" id = "infoid" value="<%=info%>">
        <div class="main-top">
            <div class="logo"><a href=""><span>新闻大视野</span></a></div>
            <div class="login-box">
                <form action="/NewsServlet?opr=selectAll" method="post">
                    <label>用户名</label><input type="text" name="uname" /><label>密码</label>
                    <input type="text" name="upassword" /><button>登录</button>
                    <input type="submit" value="登录">
                </form>
    
            </div>
            <div class="nav">
                <ul class="clearfix">
                    <li><a href="#">首页</a></li>
                    <li><a href="#">国内</a></li>
                    <li><a href="#">国际</a></li>
                    <li><a href="#">娱乐</a></li>
                    <li><a href="#">军事</a></li>
                </ul>
            </div>
        </div>
        <!--banner-->
        <div class="main-banner">
        <img src="/images/banner.png" />
        </div>
        <!--搜索横框-->
        <div class="search-box">
            <div class="sl">
                <div class="sr clearfix">
                
                    <span class="left-search clearfix">
                        <label>站内搜索</label><input type="text" name="keyword" value="关键词" /><button class="go-btn"></button>
                    </span>
                    <span class="right-link">
                        <label>快速链接</label><select><option>-----专题选择-----</option></select><button class="go-btn"></button>
                    </span>
                    
                </div>
            </div>    
        </div>
    </div>
    <!--d页面主体-->
    <div id="content" class="main-content clearfix">
        <!--主体的的左边部分-->
        <div class="main-content-left">
            <!--新闻专题分类-->
            <div class="class-box">
                <div class="class-box-header">
                   <span class="fr"><a href="#">更多...</a></span>
                   <h3>新闻专题</h3>
                </div>
                <div class="class-box-content">
                    <ul>
                         <li><a href="#">国内</a></li>
                        <li><a href="#">国际</a></li>
                        <li><a href="#">娱乐</a></li>
                        <li><a href="#">军事</a></li>
                        <li><a href="#">财经</a></li>
                        <li><a href="#">天气</a></li>
                        <li class="clear-bottom-line"><a href="#">科技</a></li>
                    </ul>
                </div>
            </div>
            <!--最新新闻-->
            <div class="left-box">
                <div class="left-box-tbg">
                    <div class="left-box-bbg">
                        <div class="left-box-header"><h3><a href="#">最新新闻</a></h3></div>
                        <div class="left-box-content">
                            <ul>
                                <li><a href="#">课工场最靠谱的互联网教育 Java Web从入门到成就!</a></li>
                                <li><a href="#">课工场最靠谱的互联网教育 Java Web从入门到成就!</a></li>
                                <li><a href="#">课工场最靠谱的互联网教育 Java Web从入门到成就!</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
            <!--最新评论-->
            <div class="left-box">
                <div class="left-box-tbg">
                    <div class="left-box-bbg">
                        <div class="left-box-header"><h3>最新评论</h3></div>
                        <div class="left-box-content">
                            <ul>
                                <li><a href="#">课工场最靠谱的互联网教育 Java Web从入门到成就!</a></li>
                                <li><a href="#">课工场最靠谱的互联网教育 Java Web从入门到成就!</a></li>
                                <li><a href="#">课工场最靠谱的互联网教育 Java Web从入门到成就!</a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--页面主体的右部,包括新闻的列表和评论内容 -->
        <div class="main-content-right">
            <!--各专题的新闻列表-->
            <div class="main-text-box">
                <div class="main-text-box-tbg">
                    <div class="main-text-box-bbg">
                        
                        <div class="main-text-box-header">
                            <h3>即时新闻</h3>
                        </div>
                        <div class="main-text-box-content">
                          <ul class="news-list-ul clearfix" id ="uId">
                                <%--输出--%>
                              <%--<li><span>${newsList[0].createdate}</span><a href="#">${newsList[0].title}</a></li>--%>
    
    
    
    
                          </ul>
                        </div>
                        <!--图片新闻-->
                        <div class="news-pic">
                            <dl>
                                <dt><img src="/images/new-1.png" /></dt>
                                <dd>PHP从入门到放弃</dd>
                            </dl>
                            <dl>
                                <dt><img src="/images/new-2.png" /></dt>
                                <dd>Java Web从绝望到希望</dd>
                            </dl>
                            <dl>
                                <dt><img src="/images/new-3.png" /></dt>
                                <dd>课工场五一回馈</dd>
                            </dl>
                            <dl>
                                <dt><img src="/images/new-2.png" /></dt>
                                <dd>Java Web从绝望到希望</dd>
                            </dl>
                        </div>
                        <div class="page-bar">
                            <ul class="page-num-ul clearfix">
                                <li><a href="javascript:;" id="firstId">首页</a><li>
                                <li><a href="javascript:;" id="backId" page="">上一页</a><li>
                                <%--<li><a href="#" class="thisclass">1</a><li>--%>
                                <%--<li><a href="#">2</a><li>--%>
                                <%--<li><a href="#">3</a><li>--%>
                                <%--<li><a href="#">4</a><li>--%>
                                <%--<li><a href="#">5</a><li>--%>
                                    当前页<span id="numId"></span>/总页数:<span id="countId"></span>
                                <li><a href="javascript:;" id="nextId">下一页</a><li>
    
                            </ul>
    
                            <span class="page-go-form"><label>跳转至</label><input id="pageId" type="text" name="numkey" class="page-key" /><button id="bId" type="submit" class="page-btn">GO</button></span>
                        </div>
                    </div>
                </div>
            </div>
            <!--合作媒体-->
            <div class="main-text-box">
                <div class="main-text-box-tbg">
                    <div class="main-text-box-bbg">
                        <div class="main-text-box-header">
                            <h3>合作媒体</h3>
                        </div>
                        <div class="main-text-box-content">
                          <ul class="link-text-ul clearfix">
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                            <li><a href="#">中国政府网</a></li>
                          </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>           
    <!--页面底部-->
    <div id="footer" class="main-footer-box">
        24小时客户服务热线:010-68988888 常见问题解答 新闻热线:010-627488888<br />
        文明办网文明上网举报电话:010-627488888 举报邮箱:jubao@bj-aptech.com.cn<br />
        Coyright&copy;1999-2007 News China gov,All Right Reserved.<br />
        新闻中心版权所有
    </div>
    </body></html>
  • 相关阅读:
    验证整数或小数
    数据库的连接查询比较
    批处理按钮
    事务 SQL
    关于GridView模板的一些总结
    C#数据结构之线性表
    C#面向对象基础
    C#集合类:动态数组、队列、栈、哈希表、字典
    如何更改master中WebParts中自定义控件的值。
    如何取得web.config中connectings中的值
  • 原文地址:https://www.cnblogs.com/jiayiblog/p/11075323.html
Copyright © 2020-2023  润新知