分页类
package com.cee.com; import java.util.List; //分页类 public class PageBean { private List list; // 要返回的某一页的记录列表 private int allRow; // 总记录数 private int totalPage; // 总页数 private int currentPage; // 当前页 private int pageSize; // 每页记录数 private int offset; public int getOffset() { return offset; } public void setOffset(int offset) { this.offset = offset; } private boolean isFirstPage; // 是否为第一页 private boolean isLastPage; // 是否为最后一页 private boolean hasPreviousPage; // 是否有前一页 private boolean hasNextPage; // 是否有下一页 public List getList() { return list; } public void setList(List list) { this.list = list; } public int getAllRow() { return allRow; } public void setAllRow(int allRow) { this.allRow = allRow; } public int getTotalPage() { int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1; return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** */ /** * 初始化分页信息 */ public void init() { this.isFirstPage = isFirstPage(); this.isLastPage = isLastPage(); this.hasPreviousPage = isHasPreviousPage(); this.hasNextPage = isHasNextPage(); } /** */ /** * 以下判断页的信息,只需getter方法(is方法)即可 * * @return */ public boolean isFirstPage() { return currentPage == 1; // 如是当前页是第1页 } public boolean isLastPage() { return currentPage == totalPage; // 如果当前页是最后一页 } public boolean isHasPreviousPage() { return currentPage != 1; // 只要当前页不是第1页 } public boolean isHasNextPage() { if(totalPage==0){ return false; }else{ return currentPage != totalPage; // 只要当前页不是最后1页 } } /** */ /** * 计算总页数,静态方法,供外部直接通过类名调用 * * @param pageSize * 每页记录数 * @param allRow * 总记录数 * @return 总页数 */ public static int countTotalPage(final int pageSize, final int allRow) { int totalPage = allRow % pageSize == 0 ? allRow / pageSize : allRow / pageSize + 1; return totalPage; } /** */ /** * 计算当前页开始记录 * * @param pageSize * 每页记录数 * @param currentPage * 当前第几页 * @return 当前页开始记录号 */ public static int countOffset(final int pageSize, final int currentPage) { final int offset = pageSize * (currentPage - 1); return offset; } /** */ /** * 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替 * * @param page * 传入的参数(可能为空,即0,则返回1) * @return 当前页 */ public static int countCurrentPage(int page) { final int curPage = (page == 0 ? 1 : page); return curPage; } public static int lastSqlIdx(int rowStartIdx, int pageSize) { return rowStartIdx + pageSize; } }
分页代码与高亮代码
注意 indexDir 目录下存放的是 lucene 的索引, 直接运行代码会发生错误,请看上篇文章生成索引
package com.cee.test; import java.io.File; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.queryParser.MultiFieldQueryParser; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.Searcher; import org.apache.lucene.search.TopScoreDocCollector; import org.apache.lucene.search.highlight.Highlighter; import org.apache.lucene.search.highlight.InvalidTokenOffsetsException; import org.apache.lucene.search.highlight.QueryScorer; import org.apache.lucene.search.highlight.SimpleHTMLFormatter; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import com.cee.com.PageBean; /** * 分页与高亮显示 * * @author qcy * */ public class PageSearcher { /** * * @param pageNo * @param pageSize * @param q * 表示查询条件 * @return */ public final static String indexDir = "d:\\TestLucene\\indexDB"; @SuppressWarnings({ "unchecked", "unchecked", "deprecation", "deprecation" }) public static PageBean getPageQuery(int pageNo, int pageSize, String[] q) throws CorruptIndexException, IOException, ParseException, InvalidTokenOffsetsException { List result = new ArrayList(); Searcher searcher = new IndexSearcher(FSDirectory.open(new File( indexDir)), true); Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); String[] fields = { "cbs", "zz" }; Query query = MultiFieldQueryParser.parse(Version.LUCENE_CURRENT, q, fields, analyzer); TopScoreDocCollector topCollector = TopScoreDocCollector.create( searcher.maxDoc(), false); searcher.search(query, topCollector); // 高亮设置 SimpleHTMLFormatter simpleHtmlFormatter = new SimpleHTMLFormatter( "<B>", "</B>");// Highlighter highlighter = new Highlighter(simpleHtmlFormatter, new QueryScorer(query)); // 查询当页的记录 ScoreDoc[] docs = topCollector.topDocs((pageNo - 1) * pageSize, pageSize).scoreDocs; for (ScoreDoc scdoc : docs) { Document document = searcher.doc(scdoc.doc); TokenStream tokenStream = analyzer.tokenStream("text", new StringReader(document.get("cbs"))); String str = highlighter.getBestFragment(tokenStream, document.get("cbs")); result.add("id=" + document.get("id") + "|cbs=" + document.get("cbs") + "|zz=" + document.get("zz")+"|red"+str); } PageBean pb = new PageBean(); pb.setCurrentPage(pageNo);// 当前页 pb.setPageSize(pageSize); pb.setAllRow(topCollector.getTotalHits());// hit中的记录数目 pb.setList(result); return pb; } public static void main(String[] args) throws CorruptIndexException, IOException, ParseException, InvalidTokenOffsetsException { String[] q = { "中国", "外国" }; long start = new Date().getTime(); PageBean pb = getPageQuery(1, 4, q); System.out.println("页面内容-------开始---"); List pgResult = pb.getList(); for (int i = 0; i < pgResult.size(); i++) { System.out.println(pgResult.get(i).toString()); } System.out.println("页面内容-------结束---"); System.out.println("当前页号 " + pb.getCurrentPage()); System.out.println("是否是第一个页? " + pb.isFirstPage()); System.out.println("是否是最后页? " + pb.isLastPage()); System.out.println("存在上一页? " + pb.isHasPreviousPage()); System.out.println("存在下一页? " + pb.isHasNextPage()); System.out.println("每页的记录数 " + pb.getPageSize()); System.out.println("总页数 " + pb.getTotalPage()); System.out.println("总记录数 " + pb.getAllRow()); long end = new Date().getTime(); System.out.println("花费时间:" + (double) (end - start) / 1000 + "秒"); } }
分页类有个小bug
分页的前台scrip
function changepage(pid){ var url=""; if(pid=='1'){ window.location=url+"&page=1"; } if(pid=='2'){ window.location=url+"&page=${dataList.currentPage-1}"; } if(pid=='3'){ window.location=url+"&page=${dataList.currentPage+1}"; } if(pid=='4'){ window.location=url+"&page=${dataList.totalPage}"; } }
前台分页java
<table> <tr> <td> <table> <c:forEach items="${dataList.list}" var="m"> ${m}</c:forEach> </table> </td> </tr> <tr> <td> <table width="100%" border="0" cellspacing="2" cellpadding="0"> <tr> <td height="25" align="center"> <div id="page" class="txt_p"> 共${dataList.totalPage}页 <c:choose> <c:when test="${dataList.hasPreviousPage}"> <a href="javascript:changepage('1')">首页</a> <a href="javascript:changepage('2')"> <img src="${skinPath}images/hygl/left_h.gif" align="absmiddle" border="0" /> </a> <a href="javascript:changepage('2')">上一页</a> </c:when> <c:otherwise> 首页 <img src="${skinPath}images/hygl/left_b.gif" align="absmiddle" />上一页 </c:otherwise> </c:choose> 第${dataList.currentPage}页 <c:choose> <c:when test="${dataList.hasNextPage}"> <a href="javascript:changepage('3')">下一页</a> <a href="javascript:changepage('3')"> <img src="${skinPath}images/hygl/right_h.gif" align="absmiddle" border="0" /> </a> <a href="javascript:changepage('4')">尾页</a> </c:when> <c:otherwise> 下一页<img src="${skinPath}images/hygl/right_b.gif" align="absmiddle" />尾页 </c:otherwise> </c:choose> </div> </td> </tr> </table> </td> </tr> </table>