日期:2014-05-18  浏览次数:20652 次

一个很奇怪的跳页问题
首先我写了一个搜索本地索引文件的JSP,用到lucene检索包里面的Hits类。searchindex.jsp如下:
Java code

<%@ page language="java" contentType="text/html; charset=gb2312"
    pageEncoding="gb2312"%>
    <%@page import="org.apache.lucene.search.*,ly.SearchIndex"%>
<!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=gb2312">
<title>搜索结果</title>
<style type="text/css">
<!--
.STYLE1 {
    font-family: "仿宋_GB2312";
    font-size: xx-large;
    font-color: #666666
}
-->
</style>
</head>
<body>
<p class="STYLE1">&#25628;&#32034;&#32467;&#26524;&#65306; </p><hr>
<%
 String keyword = new String(request.getParameter("searchquery").getBytes("ISO-8859-1"),"gb2312");
 Hits hits = (new SearchIndex()).search(keyword);
 int length = hits.length();
 int currentPage,recordPerPage=13;
 
 if (request.getParameter("currentPage") == null)
     currentPage = 1;
 else
     currentPage = Integer.valueOf(request.getParameter("currentPage"));

for (int i = (currentPage-1)*recordPerPage;i < length && i < currentPage*recordPerPage;i++)
{
    out.println("<a href=\""+hits.doc(i).get("URL")+"\">"+hits.doc(i).get("title")+i+"</a><br>");
    
    String bodyContain = hits.doc(i).get("body").substring(0,1023);
    String tempkeyword = null;
    if (keyword.charAt(0) >= 65 && keyword.charAt(0) <= 90)
    {
        tempkeyword = keyword.replace(keyword.charAt(0),(char)(keyword.charAt(0)+32));
        bodyContain = bodyContain.replaceAll(tempkeyword , "<font color=#E50060>" + tempkeyword + "</font>");
    }
    else if (keyword.charAt(0) >= 97 && keyword.charAt(0) <= 122)
    {
        tempkeyword = keyword.replace(keyword.charAt(0),(char)(keyword.charAt(0)-32));
        bodyContain = bodyContain.replaceAll(tempkeyword , "<font color=#E50060>" + tempkeyword + "</font>");
    }
    bodyContain = bodyContain.replaceAll(keyword , "<font color=#E50060>" + keyword + "</font>");
    out.println(i+"<br>");
    out.println(bodyContain);
    out.println(i+"<br>");
    out.println("<br/><br/>");
}


out.println("<br/>");
out.println("there is " + length + " result matched");
%>
<hr>
共有<%=length%>个记录&nbsp;
<%if (length%recordPerPage == 0) out.println(length/recordPerPage);
    else out.println(length/recordPerPage + 1);%>页&nbsp;每页显示<%=recordPerPage%>个记录
&nbsp;&nbsp;<br/>
<%if(currentPage == 1){ out.print(" 首页 上一页");   }else{  %>   
    <a href="searchindex.jsp?currentPage=<%=1%>">首页</a>
    <a href="searchindex.jsp?currentPage=<%=currentPage-1%>">上一页</a>
    <%}%>
<%if(currentPage == length){ out.print("下一页 尾页");   }else{  %>   
    <A HREF="searchindex.jsp?currentPage=<%=currentPage+1%>">下一页</A>
    <A HREF="searchindex.jsp?currentPage=<%=length%>">尾页</A>
    <%}%>
</body>
</html>

SearchIndex是一个类,我用来检索本地索引文件用的。Hits是搜索到的结果,用doc(i)取出来,实际相当一个记录。
这段代码会出现两个问题:一,先请看for语句下的
Java code
out.println(i+"