栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

ajax实现页面无刷新分页

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ajax实现页面无刷新分页

注:个人笔记

准备工作 Oracle:

eclipse:

 GoodsDao类所需代码:

    public int getRows(String str) {
		int n = 0;
		try {
			con=DBHelper.getCon();
			String sql="select count(*) from "+str;
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			if(rs.next()) {
				n=rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.myClose(con, ps, rs);
		}
		return n;
	}


    public List getAllByPage(int pageIndex, int pageSize) {
		List ls=new ArrayList();
		try {
			con=DBHelper.getCon();
			String sql="select * from(select a.*,rownum as rid from goods a) b where b.rid between ? and ?";
			int a = (pageIndex-1)*pageSize+1;
			int b = pageIndex*pageSize;
			ps=con.prepareStatement(sql);
			//给占位符赋值
			ps.setInt(1, a);
			ps.setInt(2, b);
			rs=ps.executeQuery();
			while(rs.next()) {
				Goods g=new Goods();
				g.setGid(rs.getInt(1));
				g.setGname(rs.getString(2));
				g.setGprice(rs.getInt(3));
				g.setGinfo(rs.getString(4));
				g.setGpath(rs.getString(5));
				ls.add(g);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.myClose(con, ps, rs);
		}
		return ls;
	}

GoodsBiz细节(获取最大页数):

    public int getMax(String str, int pageSize) {
		int rows = igd.getRows(str);//拿到总行数
		int max = rows/pageSize;//求页码
		if(rows%pageSize!=0) {//判断是否能够除得尽
			max++;
		}
		return max;
	}

PageServlet:

@WebServlet("/page.do")//代替web.xml的配置映射,更加简便

public class PageServlet extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=UTF-8");
		
		int pageIndex = 1;
		int pageSize = 5;
		
		PrintWriter out = resp.getWriter();
		String pid = req.getParameter("pid");
		if(pid!=null) {
			pageIndex = Integer.parseInt(pid);
		}
		IGoodsBiz igb = new GoodsBiz();
		List ls = igb.getAllByPage(pageIndex, pageSize);
		String str = JSON.toJSonString(ls);
		int max=igb.getMax("goods", pageSize);
		out.print(str+"*"+max);
		out.flush();
		out.close();
	}

}
回到主页面

index.jsp类代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here




	
	
首页  上一页  下一页  末页 

完成后便可实现无页面刷新换页效果

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/785329.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号