jsp+servlet+mysql实现分页处理

jsp+servlet+mysql实现带有模糊查询的分页

解决方法:

1.将分页的独立成一个新的pagination.jsp 需要用的时候使用动作指令jsp includie导入即可使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <div class="am-cf">
                            共 ${param.total } 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    <li>
                                        <a href="${param.url }&page=${param.curPage-1}&searchContent=${param.searchContent}">«</a>
                                    </li>
                                    <span>第${param.curPage }/ 共${param.totalPage }</span>
                                    <!--<li class="am-active">
                                        <a href="#">1</a>
                                    </li>
                                    <li>
                                        <a href="#">2</a>
                                    </li>
                                    <li>
                                        <a href="#">3</a>
                                    </li>
                                    <li>
                                        <a href="#">4</a>
                                    </li>
                                    <li>
                                        <a href="#">5</a>
                                    </li>-->
                                    <li>
                                        <a href="${param.url }&page=${param.curPage+1}&searchContent=${param.searchContent}">»</a>
                                    </li>
                                </ul>
                            </div>
                        </div>
</body>
</html>

2.创建一个servlet 进行数据库查询 查询需要的数据

        package com.chinasofti.dormsystem.servlet;
        
        import java.io.IOException;
        import java.sql.SQLException;
        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.chinasofti.dormsystem.beans.Handle;
        import com.chinasofti.dormsystem.beans.Pagination;
        import com.chinasofti.dormsystem.service.adminService;
        
        /**
         * 报修处理表 增删查改
         */
        @WebServlet("/admin/HandleFind")
        public class HandleFind extends HttpServlet {
        ;
        	adminService service=new adminService();
           
        	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        		doPost(request, response);
        		
        	}
        	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        		// TODO Auto-generated method stub
        		String oper=request.getParameter("oper");
        		if("load".equals(oper)) {
        			paging(request, response);
        		}
        		
        		
        		
        		
        	}
    
    private void paging(HttpServletRequest request, HttpServletResponse response) throws IOException {
    		String searchContent=request.getParameter("search");
    		
    		
    		String page=request.getParameter("page");
    		
    		 if(page==null||Integer.parseInt(page)<1) {
    			 page="1";
    			 }
    		 String pageSize=request.getParameter("pageSize");
    		 if(pageSize==null) {
    			 pageSize="5";
    		 }
    		 //开始查询的地方
    		 int starIndex =(Integer.parseInt(page)-1)*(Integer.parseInt(pageSize));
    		 
    		 
    		 try {
    			
    			//获取总条数
    			 int total = service.pagingcout( searchContent);
    			 if(total!=0) {
    				Pagination  paging=new Pagination(searchContent,page, pageSize, total);
    				 //获取每一页的内容
    				List<Handle> reslist = service.pagingHandle(searchContent,(paging.getCurPage()-1)*Integer.parseInt(pageSize), Integer.parseInt(pageSize));
    				request.setAttribute("paging", paging);
    				request.setAttribute("handlers", reslist);
    			 }else {	
    				 request.setAttribute("Notrs", "没有查询结果");
    		
    			}
    		 
    		
    			
    			request.getRequestDispatcher("/admin/findHandle.jsp").forward(request, response);
    			
    		
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    			
    		
    	}
    	}
    

  		
### 查询数据库 获取数据总数,每一页的数据



```java

	public List<Handle> pagingHandle(String searchContent, int start, int pageSize) throws SQLException {
		
		return admin.pagingHandle(searchContent, start, pageSize);

	}
	
	//查询报修处理表的总数目
	public int pagingcout(String searchContent) throws SQLException {

		return admin.cout(searchContent);

	}
	/**
	 * 分页查询报修信息表
	 * @param searchContent
	 * @param start
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
		
	public List<Handle> pagingHandle(String searchContent, int start ,int pageSize) throws SQLException  {
		// TODO Auto-generated method stub
		Object o=null;
		
		if(searchContent != null &&!"".equals(searchContent)) {
			String sql="SELECT handle.*, USER.`userName` FROM handle,USER WHERE handle.`handler`=user.`userId`&& content like ? ORDER BY handle.handleDate DESC limit ?,?  ";
			
			o = qrWith.query(sql, new BeanListHandler(Handle.class), new Object[] {"%"+searchContent+"%",start,pageSize});
		
		}else {
			
		String sql="SELECT handle.*, USER.`userName` FROM handle,USER WHERE handle.`handler`=user.`userId`  ORDER BY handle.handleDate DESC  limit ?,? ";
		 o = qrWith.query(sql, new BeanListHandler(Handle.class), new Object[] {start,pageSize});
	
		}
		if(o!=null) {
			List<Handle> o1=(List<Handle>) o;
			
			return o1;
		}
		return null;
	
		
		
	}
	/**、
	 * 查询报修处理表总数
	 * @param searchContent
	 * @return
	 * @throws SQLException
	 */
	public int cout(String searchContent) throws SQLException {
		Long o=null;
		if(searchContent != null &&!"".equals(searchContent)) {
			String sql="select count(*) from handle WHERE content like ?  ";
			
			 o = (Long)qrWith.query(sql,new ScalarHandler() ,new Object[] {"%"+searchContent+"%"});
		}else {
			String sql="select count(*) from handle";
			 o = (Long)qrWith.query(sql,new ScalarHandler());
			
		}
		
		
	
		
		
		return o.intValue();
		
	}

引用分页的jsp

		   <jsp:include page="/WEB-INF/jsp/pagination.jsp">
                            <jsp:param name="url" value="${pageContext.request.contextPath}/admin/HandleFind?oper=load" />
                            <jsp:param name="curPage" value="${paging.curPage }"/>
                            <jsp:param name="pageSize" value="${paging.pageSize }" />
                            <jsp:param name="total" value="${paging.total }" />
                            <jsp:param name="totalPage" value="${paging.totalPage}"/> 
                        </jsp:include>

你可能感兴趣的:(前端)