www.pudn.com > lucene+mysql+eclipe.rar > SearcherAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.struts.action;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import sun.security.provider.MD5;
import com.struts.form.SearcherForm;
import com.lucene.*;
import javax.security.*;
public class SearcherAction extends Action {
private final static int PageIndex = 3; // 每页的显示的搜索结果条数
private int startIndex = 0;
private int endIndex = 1;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String key;//传入lucene的关键字
int pageNo = 1;//第几页
if(request.getParameter("pageNo")!=null){
pageNo = Integer.parseInt(request.getParameter("pageNo"));
}
String _key = request.getParameter("key");
SearcherForm searcherForm = (SearcherForm) form;
System.out.println(searcherForm.getSort());
if (_key != "" && _key != null && pageNo > 1) {//如果不是第一页
int _pageNo = Integer.valueOf(pageNo);
startIndex = PageIndex * (_pageNo-1);
endIndex = PageIndex * _pageNo -1;
key = _key;
System.out.println("11111111111" );
}else if(_key != "" && _key != null && pageNo == 1){ //如果是第一页
startIndex = 0;
endIndex = 2;
key = _key;
System.out.println("11111112" );
}else{//不是翻页搜索
startIndex = 0;
endIndex = 2;
key = searcherForm.getSearch();
System.out.println("11111113" );
}
System.out.println("11111112" +key);
// 如果为空
if (key == null || key.equals("")) {
return mapping.findForward("success");
}
String[] q = { key, "查询条件2", "查询条件3", "查询条件3" };
try {
long start = new Date().getTime();
List lst = TestSearch.lucene(q, startIndex, endIndex);
long end = new Date().getTime();
System.out.println("花费时间:" + (double) (end - start) / 1000 + "秒");
Map map = new HashMap();
for (int i = 0; i < lst.size(); i++) {
Product pt = Product.queryProductbyId((long) Integer
.parseInt(lst.get(i).toString()));
String[] str = { "商品名称:" + pt.getTitle(),
",商品描述:" + pt.getDescription(),
",所有者:" + pt.getUserName(),
",标签:" + pt.getTag() + pt.getUserName(),
",发布时间:" + pt.getDate() };
map.put(i, str);
}
if (lst.isEmpty()) {
System.out.println("lst为null");
}
int HitsLength = SearcherFile.getHitsLength();
int MaxPage = HitsLength / this.PageIndex;
System.out.println("搜索"+HitsLength+"返回的总长度"+MaxPage);
StringBuffer sb = new StringBuffer();
for (int j = 0; j <= MaxPage; j++) {
sb.append("["
+ String.valueOf(j + 1) + "] ");
// _map.put(j, str);
}
request.setAttribute("page", sb.toString());
request.setAttribute("photo", map);
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("success");
}
}