再谈compass----集成站内搜索
2009-09-21 00:00:00 来源:WEB开发网控制层
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("------------");
}
}
更多精彩
赞助商链接