📄 searchaction.java
字号:
package com.laoer.bbscs.bbs.action;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.queryParser.*;
import com.laoer.bbscs.lucene.*;
import com.laoer.bbscs.lucene.html.Entities;
import com.laoer.bbscs.bbs.actionform.*;
import com.laoer.bbscs.sysinfo.*;
import com.laoer.bbscs.servlet.*;
public class SearchAction
extends Action {
public ActionForward execute(ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
SearchActionForm form = (SearchActionForm) actionForm;
ActionErrors errors = new ActionErrors();
UserSessionCheck myUserSessionCheck = new UserSessionCheck(form.getSid(),
httpServletRequest);
if (!myUserSessionCheck.checkSession()) {
errors.add("error.timeout", new ActionError("error.timeout"));
saveErrors(httpServletRequest, errors);
return actionMapping.findForward("error");
}
if (myUserSessionCheck.isGuest()) {
errors.add("noguest", new ActionError("error.user.noguest"));
saveErrors(httpServletRequest, errors);
return actionMapping.findForward("error");
}
httpServletRequest.setAttribute("myUserSessionCheck", myUserSessionCheck);
httpServletRequest.setAttribute("sid", form.getSid());
boolean error = false; //used to control flow for error messages
String indexName = Sys.SYSINFO.FILEPATH + "index/"; //local copy of the configuration variable
IndexSearcher searcher = null; //the searcher used to open/search the index
Query query = null; //the Query created by the QueryParser
Hits hits = null; //the search results
int startindex = 0; //the first index displayed on this page
int maxpage = 50; //the maximum items displayed on this page
String queryString = null; //the query entered in the previous page
String startVal = null; //string version of startindex
String maxresults = null; //string version of maxpage
int thispage = 0; //used for the for/next either maxpage or
//hits.length() - startindex - whichever is
//less
try {
searcher = new IndexSearcher(IndexReader.open(indexName));
}
catch (Exception e) {
error = true; //don't do anything up to the footer
}
if (error == false) { //did we open the index?
queryString = form.getQuery(); //get the search criteria
startVal = form.getStartat(); //get the start index
maxresults = form.getMaxresults(); //get max results per page
try {
maxpage = Integer.parseInt(maxresults); //parse the max results first
startindex = Integer.parseInt(startVal); //then the start index
}
catch (Exception e) {}
Analyzer analyzer = new StopAnalyzer(); //construct our usual analyzer
try {
query = QueryParser.parse(queryString, "contents", analyzer); //parse the
}
catch (ParseException e) { //query and construct the Query
error = true; //don't bother with the rest of
}
}
if (error == false && searcher != null) { // if we've had no errors
// searcher != null was to handle
// a weird compilation bug
thispage = maxpage; // default last element to maxpage
try {
hits = searcher.search(query); // run the query
if (hits.length() == 0) { // if we got no results tell the user
error = true; // don't bother with the rest of the
errors.add("error.search.noresult",
new ActionError("error.search.noresult"));
saveErrors(httpServletRequest, errors);
return actionMapping.findForward("error");
// page
}
}
catch (Exception e) {
}
}
if (error == false && searcher != null) {
httpServletRequest.setAttribute("startindex", String.valueOf(startindex));
httpServletRequest.setAttribute("thispage", String.valueOf(thispage));
httpServletRequest.setAttribute("hits", hits);
httpServletRequest.setAttribute("queryString", queryString);
return actionMapping.findForward("search");
}
else {
errors.add("error.search.noresult",
new ActionError("error.search.noresult"));
saveErrors(httpServletRequest, errors);
return actionMapping.findForward("error");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -