如何实现分页查询

本人今天在一次课堂小作业当中被要求完成一个查询页面并且要求实现分页

设计思想,采用一个二维列表来储存数据,按固定长度截取数据

pojo下的类

package org.example.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Policy {
    private long id;
    private String name;
    private String type;
    private String category;
    private String range;
    private String document;
    private String form;
    private String organ;
    private Date viadata;
    private Date pubdata;
    private Date perdata;
    private String field;
    private String theme;
    private String  keyword;
    private String superior;
    private String precursor;
    private String succeed;
    private String state;
    private String text;
    private String pdf;
    private String redundancy;
    private String rank;
    private String policykey;
    private String newrank;
    private String year;
    private String newkey;
    private String secondtheme;
    private int allsum;
}

Utils下的类

package org.example.utils;


import javax.servlet.*;
import java.io.IOException;

public class AllFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器加载");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
       request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        chain.doFilter(request,response);
    }

    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }
}
package org.example.utils;

import org.example.pojo.Policy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.http.HttpRequest;
import java.util.List;

public class Last extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int k=(int)req.getSession().getAttribute("k");
        if(k>0){
            req.getSession().setAttribute("k",--k);
        }
        resp.sendRedirect("/");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
package org.example.utils;

import org.example.pojo.Policy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class Next extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int k=(int)req.getSession().getAttribute("k");
        List<Policy> policies= (List<Policy>) req.getSession().getAttribute("policies");
        if(k<policies.size()-1){
            req.getSession().setAttribute("k",++k);
        }
        resp.sendRedirect("/");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
package org.example.utils;

import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.example.pojo.Policy;

public class PolicySelect {
    private Boolean check(Object o){
        return o==null||o.equals("");
    }
    @SneakyThrows
    private  Connection getConnection(){
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3307/exam?useSSL=false";
        String username="root";
        String password="123456";
        return DriverManager.getConnection(url,username,password);
    }
    @SneakyThrows
    public  List<Policy> select(String name, String document, String organ, String text){
        name="%"+name+"%";
        document="%"+document+"%";
        organ="%"+organ+"%";
        text="%"+text+"%";
        System.out.println(name+" "+document+" "+organ+" "+text);
        String sql="select * from policy";
        int k=1;
        List<String> p=new ArrayList<>();
        if(!check(name)){
            if(k==1) sql+=" where ";
            else sql+=" and ";
            sql+="name like ?";
            p.add(name);
            k++;
        }
        if(!check(document)){
            if(k==1) sql+=" where ";
            else sql+=" and ";
            sql+="document like ?";
            p.add(document);
            k++;
        }
        if(!check(organ)){
            if(k==1) sql+=" where ";
            else sql+=" and ";
            sql+="organ like ?";
            p.add(organ);
            k++;
        }
        if(!check(text)){
            if(k==1) sql+=" where ";
            else sql+=" and ";
            sql+="text like ?";
            p.add(text);
            k++;
        }
        System.out.println(sql);
        PreparedStatement preparedStatement=getConnection().prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        for(int i=0;i<p.size();i++){
            preparedStatement.setString(i+1,p.get(i));
        }
        ResultSet resultSet=preparedStatement.executeQuery();
        List<Policy> policies=new ArrayList<>();
        while (resultSet.next()){
            int u=1;
            policies.add(new Policy(
                    resultSet.getLong(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getDate(u++),
                    resultSet.getDate(u++),
                    resultSet.getDate(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getString(u++),
                    resultSet.getInt(u++)
            ));
        }
        preparedStatement.close();
        return policies;
    }
}
package org.example.utils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class See extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       String k=req.getParameter("k");
       int K=Integer.valueOf(k);
       if(K>0) K--;
        req.getSession().setAttribute("k",K);
        resp.sendRedirect("/");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req,resp);
    }
}
package org.example.utils;

import org.example.pojo.Policy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class SelectServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name=req.getParameter("name");
        String hyphen=req.getParameter("hyphen");
        String agency=req.getParameter("agency");
        String content=req.getParameter("content");
        PolicySelect select=new PolicySelect();
        List<Policy> Pol=select.select(name,hyphen,agency,content);
        List<List<Policy>> policies=new ArrayList<>();
        List<Policy> temp=new ArrayList<>();
        for(int i=0;i< Pol.size();i++){
            if(temp.size()<10){
                temp.add(Pol.get(i));
            }
            else{
                policies.add(temp);
                temp=new ArrayList<>();
                i--;
            }
        }
        if(Pol.size()%10>0) policies.add(temp);
        req.getSession().setAttribute("policies",policies);
        req.getSession().setAttribute("Num",Pol.size());
        resp.sendRedirect("/");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
         version="6.0">
    <servlet>
        <servlet-name>find</servlet-name>
        <servlet-class>org.example.utils.SelectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>find</servlet-name>
        <url-pattern>/find</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>res</servlet-name>
        <jsp-file>/index.jsp</jsp-file>
    </servlet>
    <servlet-mapping>
        <servlet-name>res</servlet-name>
        <url-pattern>/res</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>last</servlet-name>
        <servlet-class>org.example.utils.Last</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>last</servlet-name>
        <url-pattern>/last</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>next</servlet-name>
        <servlet-class>org.example.utils.Next</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>next</servlet-name>
        <url-pattern>/next</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>see</servlet-name>
        <servlet-class>org.example.utils.See</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>see</servlet-name>
        <url-pattern>/see</url-pattern>
    </servlet-mapping>
    <filter>
        <filter-name>All</filter-name>
        <filter-class>org.example.utils.AllFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>All</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

jsp.xml

<%@ page import="java.util.List" %>
<%@ page import="org.example.pojo.Policy" %><%--
  Created by IntelliJ IDEA.
  User: 19505
  Date: 2023/4/10
  Time: 22:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
int k=Integer.valueOf(request.getParameter("k"));
int i=Integer.valueOf(request.getParameter("i"));
    List<List<Policy>> p= (List<List<Policy>>) session.getAttribute("policies");
%>
<%=p.get(k).get(i).getText()%>
</body>
</html>
<%@ page import="java.util.List" %>
<%@ page import="org.example.pojo.Policy" %>
<%@ page import="org.example.utils.SelectServlet" %>
<%@ page import="org.example.utils.PolicySelect" %>
<%@ page import="javax.sql.DataSource" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: 19505
  Date: 2023/4/10
  Time: 15:15
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>查询界面</title>
  <meta charset="UTF-8">
  <style>
    label{
      margin: 20pt;
    }
    table
    {
      border-collapse: collapse;
      margin: 0 auto;
      text-align: center;
    }
    table td, table th
    {
      border: 1px solid #cad9ea;
      color: #666;
      height: 30px;
    }
    table thead th
    {
      background-color: #CCE8EB;
      width: 100px;
    }
    table tr:nth-child(odd)
    {
      background: #fff;
    }
    table tr:nth-child(even)
    {
      background: #F5FAFA;
    }
  </style>
</head>
<body>
<img src="LOGO.png" style="margin: 0">
<h1 style="text-align: center;color: deepskyblue;">科技政策查询系统</h1>
<form action="/find" method="post" style="text-align: center">
  <label>政策名称</label><input type="text" name="name" placeholder="请输入政策名称" style="color: orange">
  <label>发文字号</label><input type="text" name="hyphen" placeholder="请输入发文字号" style="color: orange">
  <label>发文机构</label><input type="text" name="agency">
  <label>全文检索</label><input type="text" name="content">
  <input type="submit" value="搜索" style="width: 40px;height: 30px;background: paleturquoise">
</form>
<%
  PolicySelect select=new PolicySelect();
  List<List<Policy>> policies= (List<List<Policy>>) session.getAttribute("policies");
  int k=0;
  System.out.println(session.getAttribute("k"));
  if(session.getAttribute("k")!=null) k=(int) session.getAttribute("k");
  else session.setAttribute("k",k);
  int Num=0;
  if(session.getAttribute("Num")!=null) {
    Num=(int)session.getAttribute("Num");
  }
  if(policies!=null){
    if(k>=policies.size()) k=0;
%>
<table>
  <tr>
    <th>政策名称</th>
    <th>发文机构</th>
    <th>颁布日期</th>
    <th>政策分类</th>
    <th>操作</th>
  </tr>
  <%{
    List<Policy> policyList=new ArrayList<>();
    if(k>=0&&k<policies.size())
    policyList=policies.get(k);
    if(policyList!=null){
      for(int i=0;i<policyList.size();i++){
      Policy policy=policyList.get(i);
  %>
  <tr>
    <td style="font-size: 5px;width: 400px;"><%=policy.getName()%></td>
    <td style="font-size: 5px;width: 400px;"><%=policy.getOrgan()%></td>
    <td><%=policy.getPubdata()%></td>
    <td><%=policy.getCategory()%></td>
    <td>
      <a href="detail.jsp?k=<%=k%>&i=<%=i%>" style="background: blue;color:orange;width: 50px;height: 40px;">查看</a>
    </td>
  </tr>
  <%
        }
      }
    }%>
</table>
<div style="text-align: center">
<form action="/last" style="display: inline">
  <input type="submit" value="上一页">
</form>
<%if (k+1<=policies.size()){%>
<span onclick="location.href='/see?k=<%=k+1%>'"><%=k+1%></span>
<%}%>
<%if (k+2<=policies.size()){%>
<span onclick="location.href='/see?k=<%=k+2%>'"><%=k+2%></span>
<%}%>
<%if (k+3<=policies.size()){%>
<span onclick="location.href='/see?k=<%=k+3%>'"><%=k+3%></span>
<%}%>
<%if (k+4<=policies.size()){%>
<span onclick="location.href='/see?k=<%=k+4%>'"><%=k+4%></span>
<%}%>
<%if (k+5<=policies.size()){%>
<span onclick="location.href='/see?k=<%=k+5%>'"><%=k+5%></span>
<%}%>
<form action="/next" style="display: inline">
  <input type="submit" value="下一页">
</form>
  <span style="font-size: 15pt">总共条数:<%=Num%></span>
</div>
<%}%>
</body>
</html>
posted @ 2023-04-11 00:20  突破铁皮  阅读(85)  评论(0编辑  收藏  举报