WEB开发网
开发学院软件开发Java 再谈compass----集成站内搜索 阅读

再谈compass----集成站内搜索

 2009-09-21 00:00:00 来源:WEB开发网   
核心提示: 控制层 Java代码@Controller("/search/gosearch")publicclassSearchActionextendsAction{@Resource(name="searchServiceBean")privateSearchSe

控制层

Java代码   

@Controller("/search/gosearch") 
public class SearchAction extends Action { 
 @Resource(name = "searchServiceBean") 
 private SearchService SearchService; 
 
 @Override 
 public ActionForward execute(ActionMapping mapping, ActionForm form, 
  HttpServletRequest request, HttpServletResponse response) 
  throws Exception { 
 String keywords=request.getParameter("word").trim(); 
 if(keywords==null||"".equals(keywords)){ 
  return mapping.findForward("noproduct"); 
 } 
 System.out.println("------"+keywords); 
 List<ProductInfo> list = SearchService.find(keywords); 
 request.setAttribute("word", keywords); 
 request.setAttribute("product",list); 
    if(list.isEmpty()){ 
     return mapping.findForward("noproduct"); 
    }else{ 
     return mapping.findForward("list"); 
     
    } 
 } 
} 

junit测试

Java代码  

public class SearchTest { 
 
 private static AbstractApplicationContext context; 
 @BeforeClass 
 public static void setUpBeforeClass() throws Exception { 
 try { 
  context = new ClassPathXmlApplicationContext("beans.xml"); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 } 
 
 @Test 
 public void testDelete() { 
 SearchService searchService = (SearchService) context 
 .getBean("searchServiceBean"); 
 ProductInfo p = new ProductInfo(2); 
 searchService.delete(p); 
 } 
 
 @Test 
 public void createIndex(){ 
 SearchService searchService = (SearchService) context 
 .getBean("searchServiceBean"); 
 ProductInfoService productInfoService = (ProductInfoService) context 
 .getBean("productInfoServiceBean"); 
 List<ProductInfo> list=productInfoService.getAllProduct(); 
 for(ProductInfo productInfo:list){ 
//  System.out.println("-------"+productInfo.getName()); 
  searchService.index(productInfo); 
 } 
 } 
 
 @Test 
 public void testSearch() { 
 SearchService searchService = (SearchService) context 
 .getBean("searchServiceBean"); 
 String query = "手机"; 
 List<ProductInfo> ProductInfos; 
 
  ProductInfos = searchService.find(query); 
  for (ProductInfo p : ProductInfos) { 
  System.out.println(p.getName()); 
  } 
 System.out.println("------------"); 
 } 
} 

上一页  2 3 4 5 6 7 

Tags:compass 集成 搜索

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接