使用Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页
步一:创建ArticleDao.java类
public class ArticleDao { public Integer getAllObjectNum(String keywords) throws Exception{ QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"content",LuceneUtil.getAnalyzer()); Query query = queryParser.parse(keywords); IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory()); TopDocs topDocs = indexSearcher.search(query,3); return topDocs.totalHits; } public List<Article> findAllObjectWithFY(String keywords,Integer start,Integer size) throws Exception{ List<Article> articleList = new ArrayList<Article>(); QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"content",LuceneUtil.getAnalyzer()); Query query = queryParser.parse(keywords); IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory()); TopDocs topDocs = indexSearcher.search(query,100000000); int middle = Math.min(start+size,topDocs.totalHits); for(int i=start;i<middle;i++){ ScoreDoc scoreDoc = topDocs.scoreDocs[i]; int no = scoreDoc.doc; Document document = indexSearcher.doc(no); Article article = (Article) LuceneUtil.document2javabean(document,Article.class); articleList.add(article); } return articleList; } }
步二:创建PageBean.java类
public class PageBean { private Integer allObjectNum; private Integer allPageNum; private Integer currPageNum; private Integer perPageNum = 2; private List<Article> articleList = new ArrayList<Article>(); public PageBean(){} public Integer getAllObjectNum() { return allObjectNum; } public void setAllObjectNum(Integer allObjectNum) { this.allObjectNum = allObjectNum; if(this.allObjectNum % this.perPageNum == 0){ this.allPageNum = this.allObjectNum / this.perPageNum; }else{ this.allPageNum = this.allObjectNum / this.perPageNum + 1; } } public Integer getAllPageNum() { return allPageNum; } public void setAllPageNum(Integer allPageNum) { this.allPageNum = allPageNum; } public Integer getCurrPageNum() { return currPageNum; } public void setCurrPageNum(Integer currPageNum) { this.currPageNum = currPageNum; } public Integer getPerPageNum() { return perPageNum; } public void setPerPageNum(Integer perPageNum) { this.perPageNum = perPageNum; } public List<Article> getArticleList() { return articleList; } public void setArticleList(List<Article> articleList) { this.articleList = articleList; } }
步三:创建ArticleService.java类
public class ArticleService { private ArticleDao articleDao = new ArticleDao(); public PageBean fy(String keywords,Integer currPageNum) throws Exception{ PageBean pageBean = new PageBean(); pageBean.setCurrPageNum(currPageNum); Integer allObjectNum = articleDao.getAllObjectNum(keywords); pageBean.setAllObjectNum(allObjectNum); Integer size = pageBean.getPerPageNum(); Integer start = (pageBean.getCurrPageNum()-1) * size; List<Article> articleList = articleDao.findAllObjectWithFY(keywords,start,size); pageBean.setArticleList(articleList); return pageBean; } }
步四:创建ArticleServlet.java类
public class ArticleServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { try { request.setCharacterEncoding("UTF-8"); Integer currPageNum = Integer.parseInt(request.getParameter("currPageNum")); String keywords = request.getParameter("keywords"); ArticleService articleService = new ArticleService(); PageBean pageBean = articleService.fy(keywords,currPageNum); request.setAttribute("pageBean",pageBean); request.getRequestDispatcher("/list.jsp").forward(request,response); } catch (Exception e) { e.printStackTrace(); } } }
步五:导入EasyUI相关的js包的目录
步六:在WebRoot目录下创建list.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <link rel="stylesheet" href="themes/default/easyui.css" type="text/css"></link> <link rel="stylesheet" href="themes/icon.css" type="text/css"></link> <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript" src="js/jquery.easyui.min.js"></script> <script type="text/javascript" src="locale/easyui-lang-zh_CN.js"></script> </head> <body> <!-- 输入区 --> <form action="${pageContext.request.contextPath}/ArticleServlet?currPageNum=1" method="POST"> 输入关健字:<input type="text" name="keywords" value="传智" maxlength="4"/> <input type="button" value="提交"/> </form> <!-- 显示区 --> <table border="2" align="center" width="70%"> <tr> <th>编号</th> <th>标题</th> <th>内容</th> </tr> <c:forEach var="article" items="${pageBean.articleList}"> <tr> <td>${article.id}</td> <td>${article.title}</td> <td>${article.content}</td> </tr> </c:forEach> </table> <!-- 分页组件区 --> <center> <div id="pp" style="background:#efefef;border:1px solid #ccc;width:600px"></div> </center> <script type="text/javascript"> $("#pp").pagination({ total:${pageBean.allObjectNum}, pageSize:${pageBean.perPageNum}, showPageList:false, showRefresh:false, pageNumber:${pageBean.currPageNum} }); $("#pp").pagination({ onSelectPage:function(pageNumber){ $("form").attr("action","${pageContext.request.contextPath}/ArticleServlet?currPageNum="+pageNumber); $("form").submit(); } }); </script> <script type="text/javascript"> $(":button").click(function(){ $("form").submit(); }); </script> </body> </html>
步六:在WebRoot目录下创建list2.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>根据关键字分页查询所有信息</title> </head> <body> <!-- 输入区 --> <form action="${pageContext.request.contextPath}/ArticleServlet" method="POST"> <input id="currPageNOID" type="hidden" name="currPageNO" value="1"> <table border="2" align="center"> <tr> <th>输入关键字:</th> <th><input type="text" name="keywords" maxlength="4" value="${requestScope.keywords}"/></th> <th><input type="submit" value="站内搜索"/></th> </tr> </table> </form> <!-- 输出区 --> <table border="2" align="center" width="60%"> <tr> <th>编号</th> <th>标题</th> <th>内容</th> </tr> <c:forEach var="article" items="${requestScope.pageBean.articleList}"> <tr> <td>${article.id}</td> <td>${article.title}</td> <td>${article.content}</td> </tr> </c:forEach> <!-- 分页条 --> <tr> <td colspan="3" align="center"> <a onclick="fy(1)" style="text-decoration:none;cursor:hand"> 【首页】 </a> <c:choose> <c:when test="${requestScope.pageBean.currPageNO+1<=requestScope.pageBean.allPageNO}"> <a onclick="fy(${requestScope.pageBean.currPageNO+1})" style="text-decoration:none;cursor:hand"> 【下一页】 </a> </c:when> <c:otherwise> 下一页 </c:otherwise> </c:choose> <c:choose> <c:when test="${requestScope.pageBean.currPageNO-1>0}"> <a onclick="fy(${requestScope.pageBean.currPageNO-1})" style="text-decoration:none;cursor:hand"> 【上一页】 </a> </c:when> <c:otherwise> 上一页 </c:otherwise> </c:choose> <a onclick="fy(${requestScope.pageBean.allPageNO})" style="text-decoration:none;cursor:hand"> 【未页】 </a> </td> </tr> </table> <script type="text/javascript"> function fy(currPageNO){ document.getElementById("currPageNOID").value = currPageNO; document.forms[0].submit(); } </script> </body> </html>
最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!