博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java web分页技术(jsp+servlet+Dao+javabean)
阅读量:5143 次
发布时间:2019-06-13

本文共 11536 字,大约阅读时间需要 38 分钟。

经过两天的折腾,终于自己搞定一个完整的分页项目。。。。。。眼都要瞎了。。。。。。有用信息全部展开了,整体截图如下:

1:DBC.java(数据库连接封装类)

package com.common.db;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Connection;  import java.sql.PreparedStatement;  import java.sql.Statement;  public class DBC {    private static String driver = "com.mysql.jdbc.Driver";    private static String url = "jdbc:mysql://localhost:3306/StaffRec";    private static String username = "root";    private static String password = "root";    static {        try {            /**             * 加载驱动             */            Class.forName(driver);        }catch(Exception ex) {            ex.printStackTrace();        }    }    public static Connection getConnection() {        /**         * 创建连接对象         */        Connection conn=null;        try {            conn=(Connection) DriverManager.getConnection(url,username,password);        }catch(Exception ex) {            ex.printStackTrace();        }        return conn;    }    /**     * 关闭相关流     */    public static void close(ResultSet rs,Statement st,Connection conn) {        try {            if(rs!=null) {                rs.close();            }            if(st!=null) {                st.close();            }            if(conn!=null) {                conn.close();            }        }catch(SQLException e) {            e.printStackTrace();        }    }    public static void closePst(ResultSet rs,PreparedStatement pst,Connection conn) {        try {            if(rs!=null) {                rs.close();            }            if(pst!=null) {                pst.close();            }            if(conn!=null) {                conn.close();            }        }catch(SQLException e) {            e.printStackTrace();        }    }}
View Code

2:Rec.java(和数据库表对应的实体类,需要显示的内容)

package javabean;public class Rec {    private int id;      private String rec;      private String reced;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getRec() {        return rec;    }    public void setRec(String rec) {        this.rec = rec;    }    public String getReced() {        return reced;    }    public void setReced(String reced) {        this.reced = reced;    }  }
View Code

3:PageModel.java(完整的分页模型!!!)

package javabean;import java.util.List;  /**  * 封装分页信息  * @author Administrator  *  */  public class PageModel
{ //结果集 private List
list; //查询记录数 private int totalRecords; //每页多少条数据 private int pageSize; //第几页 private int pageNo; /** * 总页数 * @return */ public int getTotalPages() { return (totalRecords + pageSize - 1) / pageSize; } /** * 取得首页 * @return */ public int getTopPageNo() { return 1; } /** * 上一页 * @return */ public int getPreviousPageNo() { if (pageNo <= 1) { return 1; } return pageNo - 1; } /** * 下一页 * @return */ public int getNextPageNo() { if (pageNo >= getBottomPageNo()) { return getBottomPageNo(); } return pageNo + 1; } /** * 取得尾页 * @return */ public int getBottomPageNo() { return getTotalPages(); } /** * get set函数 * @return */ public List
getList() { return list; } public void setList(List
list) { this.list = list; } public int getTotalRecords() { return totalRecords; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } }
View Code

4:RecommentDao.java(业务逻辑层,操作数据库等操作,返回值为PageModel对象)

package com.common.page;import java.sql.Connection;  import java.sql.PreparedStatement;  import java.sql.ResultSet;  import java.sql.SQLException;  import java.util.ArrayList;  import java.util.List;    import com.common.db.DBC;  import javabean.*;  public class RecommentDao {      public PageModel
findData(String pageNo,String pageSize){ //去掉了static PageModel
pageModel=null; //移到这儿 Connection conn=DBC.getConnection(); String sql="select * from recommend limit ?,?"; //PageModel pageModel=null; PreparedStatement pst=null; ResultSet rs=null; Rec rec=null; List
list=new ArrayList
(); try { pst=conn.prepareStatement(sql); //pstm.setInt(1, (pageNo-1)*pageSize); //pstm.setInt(2, pageNo*pageSize); pst.setInt(1, (Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize)); pst.setInt(2, Integer.parseInt(pageSize)); rs=pst.executeQuery(); while(rs.next()){ rec=new Rec(); rec.setId(rs.getInt("id")); rec.setRec(rs.getString("rec")); rec.setReced(rs.getString("reced")); list.add(rec); } ResultSet rs2=pst.executeQuery("select count(*) from recommend"); int total=0; if(rs2.next()){ total=rs2.getInt(1);//总的数据条数 } pageModel=new PageModel
(); pageModel.setPageNo(Integer.parseInt(pageNo)); pageModel.setPageSize(Integer.parseInt(pageSize)); pageModel.setTotalRecords(total); pageModel.setList(list); } catch (SQLException e) { e.printStackTrace(); }finally{ DBC.closePst(rs, pst, conn); } return pageModel; } /*public static void main(String[] args) { RecommentDao client=new RecommentDao(); PageModel
pageModel=client.findData("2","4"); List
list=pageModel.getList(); for(Rec a:list){ System.out.print("ID:"+a.getId()+",推荐人:"+a.getRec()+",被推荐人:"+a.getReced()); System.out.println(); } System.out.print("当前页:"+pageModel.getPageNo()+" "); System.out.print("共"+pageModel.getTotalPages()+"页 "); System.out.print("首页:"+pageModel.getTopPageNo()+" "); System.out.print("上一页:"+pageModel.getPreviousPageNo()+" "); System.out.print("下一页:"+pageModel.getNextPageNo()+" "); System.out.print("尾页:"+pageModel.getBottomPageNo()+" "); System.out.print("共"+pageModel.getTotalRecords()+"条记录"); System.out.println(); }*/ }
View Code

5:recomment.java(控制层servlet,连接前端和业务层,获得PageModel对象后跳转到新页面展示分页数据信息)

package servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.common.page.RecommentDao;import javabean.PageModel;import javabean.Rec;/** * Servlet implementation class recomment */@WebServlet("/recomment")public class recomment extends HttpServlet {    private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public recomment() {        super();        // TODO Auto-generated constructor stub    }    /**     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)     */    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        //response.getWriter().append("Served at: ").append(request.getContextPath());        this.doPost(request, response);    }    /**     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)     */    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub        //System.out.println("+1");        request.setCharacterEncoding("UTF-8");        response.setCharacterEncoding("UTF-8");        String pageSize = request.getParameter("pageSize");// 每页显示行数        String pageNo = request.getParameter("pageNo");// 当前显示页次        if (pageSize == null) {
// 为空时设置默认页大小为10 pageSize = "10"; } if (pageNo == null) {
// 为空时设置默认为第1页 pageNo = "1"; } //System.out.println("pageNo="+pageNo+"pageSize="+pageSize); // 保存分页参数,传递给下一个页面 request.setAttribute("pageSize", pageSize); request.setAttribute("pageNo", pageNo); //新建Dao对象,获取pageModel RecommentDao client=new RecommentDao(); PageModel
pageModel=client.findData(pageNo,pageSize); request.setAttribute("pageModel", pageModel);//前端获取这个值 request.getRequestDispatcher("../Result.jsp").forward(request, response); }}
View Code

6:index.jsp(程序入口,调到servlet层)

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>
Insert title here查看所有推荐信息
View Code

7:result.jsp(展示分页结果的页面)

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ page import="java.util.*" %><%@ page import="com.common.page.*" %><%@ page import="javabean.*" %><%String pageSize = (String) request.getAttribute("pageSize");String pageNo = (String) request.getAttribute("pageNo");PageModel
pageModel=(PageModel
) request.getAttribute("pageModel");List
list=pageModel.getList();%>
Insert title here

还没有任何数据!

<% if(list==null||list.size()<1){ %> <%}else{ for(Rec rec:list){ %>
<% } } %>

所有信息

ID 推荐人 推荐号码
<%=rec.getId() %> <%=rec.getRec().substring(0, 3)+"****"+rec.getRec().substring(7, 11) %> <%=rec.getReced() %>
每页条数 首页 上一页 下一页 尾页
View Code

8:配置web.xml(主要是针对servlet进行配置)

haha
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
recomment
servlet.recomment
recomment
/servlet/recomment
720
View Code

9:结果展示

运行index.jsp

单击:

转载于:https://www.cnblogs.com/zhouerba/p/7793507.html

你可能感兴趣的文章
Liunx expect 基础
查看>>
------- 当前全球最新的 IPv4 地址池使用报告 -------
查看>>
js数据类型
查看>>
18-08-27 机器人自动化之页面表格数据的定位拾取
查看>>
驾驭大数据pdf
查看>>
WOW!Illustrator CS6完全自学宝典pdf
查看>>
ASP.NET MVC 入门
查看>>
day28飞机大战-战斗机.js
查看>>
中英互译软件测试计划
查看>>
Js 类定义的几种方式
查看>>
空间数据全文检索初步设计和实现
查看>>
最长递增子串
查看>>
Linux 的 Crontab 命令运用(转)
查看>>
【单页应用】view与model相关梳理(转载)
查看>>
8.1Solr API使用(分页,高亮)
查看>>
【web】基础 安全
查看>>
为CentOS下的Docker安装配置python3【转】
查看>>
python_面向对象—代码练习
查看>>
Linux kali信息探测以及 Nmap 初体验
查看>>
Java中Atomic包的实现原理及应用
查看>>