您查看的文章来源于http://www.oklinux.cn } 三、Web调用 下面我们来看看在Web中如果调用商业逻辑层的全文检索功能。下面是处理用户请请的Action中关于搜索部分的源码: package com.easyjf.news.action; public class SearchAction implements IWebAction { public Page doSearch(WebForm form,Module module)throws Exception { String key=CommUtil.null2String(form.get("v")); key=URLDecoder.decode(URLEncoder.encode(key,"ISO8859_1"),"utf-8"); form.set("v",key); form.addResult("v2",URLEncoder.encode(key,"utf-8")); if(key.getBytes().length>2){ String orderBy=CommUtil.null2String(form.get("order")); int currentPage=CommUtil.null2Int(form.get("page")); int pageSize=CommUtil.null2Int(form.get("pageSize")); if(currentPage<1)currentPage=1; if(pageSize<1)pageSize=15; SearchEngine search=new SearchEngine(key,orderBy,pageSize,currentPage); search.getLuceneSearch().setIndexDir(Globals.APP_BASE_DIR "/WEB-INF/index"); search.doSearchByLucene(); IPageList pList=search.getResult(); if(pList!=null && pList.getRowCount()>0){ form.addResult("list",pList.getResult()); form.addResult("pages",new Integer(pList.getPages())); form.addResult("rows",new Integer(pList.getRowCount())); form.addResult("page",new Integer(pList.getCurrentPage())); form.addResult("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages())); } else { form.addResult("notFound","true");//找不到数据 } } else form.addResult("errMsg","您输入的关键字太短!"); form.addResult("hotSearch",SearchEngine.getHotSearch(20)); return null; } } 其中调用的SearchEngine类中有关Lucene部分的源码: public class SearchEngine { private MyRssSearch luceneSearch=new MyRssSearch(); public void doSearchByLucene() { SearchKey keyObj=readCache(); if(keyObj!=null){ result=luceneSearch.search(key,pageSize,currentPage); if(updateStatus){ keyObj.setReadTimes(new Integer(keyObj.getReadTimes().intValue() 1)); keyObj.update(); } } else//缓存中没有该关键字信息,生成关键字搜索结果 { keyObj=new SearchKey(); keyObj.setTitle(key); keyObj.setLastUpdated(new Date()); keyObj.setReadTimes(new Integer(1)); keyObj.setStatus(new Integer(0)); keyObj.setSequence(new Integer(1)); keyObj.setVdate(new Date()); keyObj.save(); result=luceneSearch.search(key,pageSize,currentPage);; } } }
共3页: 上一页 [1] [2] 3 下一页 |