利用ajax实现web项目的无刷新分页(最新版)

在学习中用ajax实现web项目的无刷新分页,代码中注释较少,有没看懂的可以留言

jsp代码:

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




Insert title here





	


    当前页数:     首页 上一页 下一页 末页

    后端servlet

    package servlet;
    
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.List;
    
    
    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.alibaba.fastjson.JSON;
    
    
    import dao.NewsDao;
    import dao.TopicDao;
    import dao.impl.NewsDaoImpl;
    import dao.impl.TopicDaoImpl;
    
    
    import entity.News;
    import entity.Page;
    import entity.Topic;
    
    
    @WebServlet("*.to")
    public class TestServlet extends HttpServlet {
    
    
    	@Override
    	protected void service(HttpServletRequest req, HttpServletResponse res)
    			throws ServletException, IOException {
    		req.setCharacterEncoding("utf-8");
    		res.setContentType("text/html;charset=utf-8");
    		String p=req.getServletPath();
    		PrintWriter out=res.getWriter();
    		if("/newsServlet.to".equals(p)){
    			int pageSize=5;
    			int currPageNo=1;
    			int start = 0;
    			String pageIndex=req.getParameter("pageIndex");
    			String tid=req.getParameter("tid");
    			System.out.println(tid);
    			Page page=null;
    			List nlist=null;//条件查询的集合
    			List flist=null;//分页查询的集合
    			NewsDao newsDao=new NewsDaoImpl();
    			if(pageIndex!=null){
    				currPageNo=Integer.parseInt(pageIndex);	
    				start=(currPageNo-1)*pageSize;
    			}
    			if(tid==null||tid==""){
    				String sql1="select * from news";
    				String sql2="select * from news limit ?,?";
    				nlist=newsDao.selectNews(sql1);
    				flist=newsDao.selectNews(sql2,start,pageSize);				
    			}else if(tid!=null){
    				int ntid=Integer.parseInt(tid);
    				String sql3="select * from news where ntid=?";
    				String sql4="select * from news where ntid=? limit ?,?";
    				nlist=newsDao.selectNews(sql3,ntid);
    				flist=newsDao.selectNews(sql4,ntid,start,pageSize);
    			}
    			int totalCount=nlist.size();
    			int totalPages=(totalCount/pageSize==0)?(totalCount/pageSize):(totalCount/pageSize+1);
    			page=new Page(currPageNo,pageSize,totalCount,totalPages,flist);	
    			String newsJson=JSON.toJSONStringWithDateFormat(page, "yyyy-MM-dd HH:mm:ss");	
    			out.print(newsJson);
    		    out.flush();
    		    out.close();
    		}else if("/topicServlet.to".equals(p)){
    			TopicDao tdao=new TopicDaoImpl();
    			List tlist=tdao.getAllTopics();
    			String topicJson=JSON.toJSONString(tlist);
    			out.write(topicJson);
    			out.flush();
    		    out.close();
    		}
    			
    	}
    	
    }
    

    Page类:

    package entity;
    
    import java.io.Serializable;
    import java.util.List;
    
    public class Page implements Serializable {
    	private int currPageNo; // 当前页码
    	private int pageSize; // 页面大小,即每页显示记录数
    	private int totalCount; // 记录总数
    	private int totalPageCount; // 总页数
    	private List newsList; // 每页数据集合
    	public int getCurrPageNo() {
    		return currPageNo;
    	}
    	public void setCurrPageNo(int currPageNo) {
    		this.currPageNo = currPageNo;
    	}
    	public int getPageSize() {
    		return pageSize;
    	}
    	public void setPageSize(int pageSize) {
    		this.pageSize = pageSize;
    	}
    	public int getTotalCount() {
    		return totalCount;
    	}
    	public void setTotalCount(int totalCount) {
    		this.totalCount = totalCount;
    	}
    	public int getTotalPageCount() {
    		return totalPageCount;
    	}
    	public void setTotalPageCount(int totalPageCount) {
    		this.totalPageCount = totalPageCount;
    	}
    	public List getNewsList() {
    		return newsList;
    	}
    	public void setNewsList(List newsList) {
    		this.newsList = newsList;
    	}
    	public Page(int currPageNo, int pageSize, int totalCount,
    			int totalPageCount, List newsList) {
    		super();
    		this.currPageNo = currPageNo;
    		this.pageSize = pageSize;
    		this.totalCount = totalCount;
    		this.totalPageCount = totalPageCount;
    		this.newsList = newsList;
    	}
    	
    }
    

    效果图:

    利用ajax实现web项目的无刷新分页(最新版)_第1张图片


    你可能感兴趣的:(利用ajax实现web项目的无刷新分页(最新版))