zoukankan      html  css  js  c++  java
  • Javaweb分页

    在这里先说说分页的主要思路,分页limit语句和count(*)这两个你要知道之后的就是你的逻辑问题了。在这里简单的展示一下

    这里展示的是连接数据库的情况,分页一页5条数据如下代码

    1.HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>分页</title>
    </head>
    <script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
    <body>
    <form id="form1" >
    搜索:<input id="search" name="search" type="text"/><input value="提交" id="btn" type="button"/>
    </form>
    <table id="tables"> </table>
    <div id="div1"></div>
    </body>
    <script type="text/javascript">
    $("#btn").click(function(){
    var tableHtml = "<tr><th>编号</th><th>名字</th><th>价格</th></tr>";
    $.ajax({
    url:"hui.hui",
    type:"post",
    dataType:"json",
    data:{"search":$("#search").val(),"page":"0"},
    success:function(data){
    $.each(data.arr , function(i,item){
    tableHtml = tableHtml + "<tr><td>"+item.bookNumber+"</td><td>"+item.bookName+"</td><td>"+item.bookPressTime+"</td></tr>";
    });
    let count = parseInt(data.count)%5;
    if(count>0){
    count = parseInt(parseInt(data.count)/5)+1;
    }
    var div1="";
    for(var i=0;i<count;i++){  //注,如果想一次显示几页,就循环几次,
    div1 = div1 + "<a href='javaScript:void(0);' onclick="page('"+i*5+"')">"+(i+1)+"</a> ";
    }
    $("#div1").html(div1+"总页数:"+count);
    $("#tables").html(tableHtml);
    }
    });
    });


    function page(pageNo){
    var tableHtml = "<tr><th>编号</th><th>书名</th><th>时间</th></tr>";
    $.ajax({
    url:"hui.hui",
    type:"post",
    dataType:"json",
    data:{"search":$("#search").val(),"page":pageNo},
    success:function(data){
    $.each(data.arr , function(i,item){
    tableHtml = tableHtml + "<tr><td>"+item.bookNumber+"</td><td>"+item.bookName+"</td><td>"+item.bookPressTime+"</td></tr>";
    });
    let count = parseInt(data.count)%5;
    if(count>0){
    count = parseInt(parseInt(data.count)/5)+1;
    }
    var div1="";
    for(var i=0;i<count;i++){ //在这里修改开始(int i =0)和结束(i<count),就可以达到动态分页了
    div1 = div1 + "<a href='javaScript:void(0);' onclick="page('"+i*5+"')">"+(i+1)+"</a> ";
    }
    $("#div1").html(div1+"总页数:"+count);
    $("#tables").html(tableHtml);
    }
    });
    }
    </script>
    </html>
    分页是一个静态分页,如果想动态的就把蓝色部分循环改一下就好了

    2.servlet

    package com.hp2.servlet;

    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.HashMap;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;

    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;

    public class HServlet extends HttpServlet {
    final static String url = "jdbc:mysql://127.0.0.1:3306/project01?useUnicode=true&characterEncoding=UTF-8";
    final static String username = "root";
    final static String password = "root";
    final static String driver = "com.mysql.jdbc.Driver";

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

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



    public void loginHTML(HttpServletRequest request, HttpServletResponse response) throws IOException {
    List<Map> list = new LinkedList();
    Map resultMap = new HashMap();
    Connection conn = null;
    Statement state = null;
    ResultSet rs = null;
    ResultSet rs1 = null;
    String search = request.getParameter("search") ==null ? "" : request.getParameter("search").toString();
    String page = request.getParameter("page") ==null ? "0" : request.getParameter("page").toString();
    String sql = "select * from book where 1=1 and (bookNumber like '%"+search+"%' or bookName like '%"+search+"%') limit "+page+" ,5";
    String sql1 = "select count(*) count from book where 1=1 and (bookNumber like '%"+search+"%' or bookName like '%"+search+"%')";
    try {
    Class.forName(driver);
    conn = DriverManager.getConnection(url,username,password);
    state = conn.createStatement();
    rs1 = state.executeQuery(sql1);
    rs1.next();
    resultMap.put("count", rs1.getObject("count"));
    rs = state.executeQuery(sql);
    while(rs.next()) {
    Map map = new HashMap();
    map.put("bookNumber",rs.getObject("bookNumber")+"");
    map.put("bookName",rs.getObject("bookName")+"");
    map.put("bookPressTime",rs.getObject("bookPressTime")+"");
    list.add(map);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    try {
    if(rs == null) {
    rs.close();
    }
    if(state ==null) {
    state.close();
    }
    if(conn==null) {
    conn.close();
    }
    }catch (Exception e) {
    e.printStackTrace();
    }
    }
    resultMap.put("arr", list);
    JSONObject jsonObject = JSONObject.fromObject(resultMap);
    response.getWriter().print(jsonObject.toString());
    response.getWriter().close();
    }
    }

    上面的内容有什么不对的地方,请大家多多指教!


  • 相关阅读:
    druid去掉广告
    nested exception is java.lang.NoClassDefFoundError: org/aspectj/util/Partial
    org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer;
    @Transactional没起作用ssm框架
    Mac下Vim编辑快捷键小结(移动光标)
    埋点
    mysql 删除以某字符串开头的表
    idea去掉无效引用
    一个有用的正则表达式
    系统集成思维导图
  • 原文地址:https://www.cnblogs.com/lihui123/p/13893325.html
Copyright © 2011-2022 走看看