⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 search.java

📁 一套基于struts+spring开发的网上聊天系统,欢迎下在研究.
💻 JAVA
字号:
package com.laoer.bbscs.web.action;

import com.laoer.bbscs.web.form.*;
import com.laoer.bbscs.web.servlet.*;
import com.laoer.bbscs.sys.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.lucene.queryParser.ParseException;

/**
 * <p>Title: TianYi BBS</p>
 * <p>Description: TianYi BBS System</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: LAOER.COM/TIANYISOFT.NET</p>
 * @author laoer
 * @version 6.0
 */

public class Search
    extends BaseAction {
  public ActionForward execute(ActionMapping actionMapping,
                               ActionForm actionForm,
                               HttpServletRequest httpServletRequest,
                               HttpServletResponse httpServletResponse) {

    ActionErrors errors = new ActionErrors();
    SearchForm form = (SearchForm) actionForm;

    UserCheck uc = this.getUserCheck(httpServletRequest, httpServletResponse,
                                     Constant.MESSAGE.getMessage(
        httpServletRequest.getLocale(), "place.search"));

    if (SysInfo.getInstance().getUseallsearch() != 0) {
      if (uc.isGuest()) {
        errors.add("error.pleaselogin", new ActionError("error.pleaselogin"));
        saveErrors(httpServletRequest, errors);
        //return actionMapping.findForward("login");
        return SysUtil.getPassLogin(actionMapping);
      }
    }

    boolean error = false;
    String indexName = SysUtil.getIndexPath();
    IndexSearcher searcher = null;
    Query query = null;

    Hits hits = null;
    int startindex = form.getStartat();
    int maxpage = form.getMaxresults();
    int thispage = 0;

    try {
      searcher = new IndexSearcher(IndexReader.open(indexName));
    }
    catch (IOException ex) {
      error = true;
    }

    if (!error) {
      Analyzer analyzer = new StandardAnalyzer();
      try {
        query = QueryParser.parse(form.getQuery(), "contents", analyzer);
      }
      catch (ParseException ex1) {
        error = true;
      }
    }
    if (!error && searcher != null) {
      thispage = maxpage;
      try {
        hits = searcher.search(query);
        if (hits.length() == 0) {
          error = true;
          errors.add("error.search.noresult",
                     new ActionError("error.search.noresult"));
          saveErrors(httpServletRequest, errors);
          return actionMapping.findForward("error");
        }
      }
      catch (Exception e) {
        e.printStackTrace();
        error = true;
      }
    }

    if (!error && searcher != null) {
      httpServletRequest.setAttribute("istartindex", new Integer(startindex));
      httpServletRequest.setAttribute("ithispage", new Integer(thispage));
      httpServletRequest.setAttribute("hits", hits);
      httpServletRequest.setAttribute("queryString", form.getQuery());
      return actionMapping.findForward("search");
    }
    else {
      errors.add("error.search.noresult",
                 new ActionError("error.search.noresult"));
      saveErrors(httpServletRequest, errors);
      return actionMapping.findForward("error");
    }
    //return actionMapping.findForward("error");
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -