设计一用户界面,例如基于B/S结构的:

把Lucene等程序包和开发的检索程序(类)导入到检索页面中,编写结果页面代码,例如searchresult.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="java.io.IOException,java.io.StringReader,java.util.ArrayList,
  org.apache.lucene.analysis.Analyzer,org.apache.lucene.analysis.Token,org.apache.lucene.analysis.TokenStream,org.apache.lucene.search.Hits,
  edu.guet.shilong.searcher.*,LucenerHighlight.WebLuceneHighlighter,org.apache.lucene.analysis.standard.StandardAnalyzer" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>搜索结果</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
   <style type="text/css">
   u{font: "新宋体" ; color:#FF0066; font-weight:100;}
   em{ color:#66FFCC}
   </style>
  </head>
  
  <body>
    <jsp:include page="common.jsp"></jsp:include>
    <div>
   <%
   
   //接收url传值,处理参数 ---------------
   String key=request.getParameter("key");  //接收的QueryString
   String keyVal=new String(key.getBytes("ISO8859-1"),"gb2312");
   Searcher sc=new Searcher(keyVal);  //构建搜索实例
   Hits hits=sc.getHits();        //获取命中记录
    //关键字分词处理
    Analyzer analyzer=new StandardAnalyzer();;
    StringReader in = new StringReader(keyVal);
    TokenStream tokenStream = analyzer.tokenStream("", in);       
    ArrayList al = new ArrayList();
    for (Token token = tokenStream.next(); token != null; token = tokenStream.next()) {
           al.add(token.termText());
    }   //end for
    WebLuceneHighlighter hl = new WebLuceneHighlighter(al);//构建高亮实例      
     //end 处理
     //-----------------------------------------
     
 //分页处理开始过-----------------
 int intPageSize=0;//一页显示的记录数
 int intRowCount=0;//总记录数
 int intPageCount=0;//总页面数
 int intPage=0;//带显示页面
 String strPage;//url中的参数接收变量
  
 //开始设置
 intPageSize=10;//额定每一页显示10条记录
 strPage=request.getParameter("page");
 if(strPage==null)
 {
    //表示url中的QueryString没有page参数,则显示第一页
    intPage=1;
 }
 else
 {
   //将url中的字符串值转换为整形
   intPage=Integer.parseInt(strPage);
 }
 if(intPage<1){
      intPage=1;
   }
   //开始计算记录
   intRowCount= hits.length();//获取命中记录,设置记录总数
   //计算总页数
   intPageCount=(intRowCount+intPageSize-1)/intPageSize;
   //调整带显示的页码
  if(intPage>intPageCount){
      intPage=intPageCount;
   }
  //显示的数据指针 
  int currentPoint=(intPage-1)*intPageSize; //当前指针
  int roopCount;  //显示的记录数
  if(intRowCount-currentPoint>intPageSize)
  {
     roopCount=intPageSize;
  }
  else
  {
     roopCount=intRowCount-currentPoint;
  }
   //out.print("循环次数为:"+roopCount);
    //记录数据显示处理开始------------
  
    out.println("<table width=\"800px\">");    
    for(int i=currentPoint;i<currentPoint+roopCount;i++) //显示全部
   {
       out.println("<tr>");
       String title=hl.highlight(hits.doc(i).get("title"));
       String url=hits.doc(i).get("url");
       out.println("<td><a href='"+ url +"'>" + title + "</a></td>" );
       out.println("</tr>");
       out.println("<tr>");
       String content=hl.highlight(hits.doc(i).get("content"),100);
       out.println("<td>"+ content +"</td>");
       out.println("</tr>");
       out.println("<tr>");
       out.println("<td><font font-size:9px><a href="+url+"><em>"+ url +"</em></a></font></td>");
       out.println("</tr>");
   }
    out.println("</table>");    
    //显示结束-----------           
   %>
   </div>
    <form action="show.jsp"><%=intPage %>页    共<%= intPageCount %><%
if(intPage>1)
{  //如果当前显示的页小于总页数
 %>
 <a href="show.jsp?key=<%=keyVal%>&page=<%= intPage-1 %>">上一页</a>
 <%}
 if(intPage<intPageCount) {//如果带显示页大于1%>
 <a href="show.jsp?key=<%=keyVal%>&page=<%= intPage+1 %>">下一页</a>
 转到第:<input type="text" name="page" size="4"><input type="submit" value="GO" name="cndok">
 <%} %>
</form>
   <jsp:include page="common.jsp"></jsp:include>
  </body>
</html>

 

测试结果:

 

posted on 2013-01-13 15:19  烤德  阅读(1406)  评论(4编辑  收藏  举报