nutch+struts2整合二次开发代码
来源:互联网 发布:php调用类中的方法 编辑:程序博客网 时间:2024/06/05 14:08
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="gb2312" />
<package name="default" extends="struts-default">
<action name="search" class="org.nutch.web.SearchAction" >
<result>/list.jsp</result>
</action>
</package>
</struts>
DAO层接口:
package org.nutch.Dao;
import java.util.List;
import org.nutch.entity.UserInfo;
import org.nutch.util.PaginationSupport;
/**
*
* 项目名称:strutsnutch
* 类名称:SearchDao
* 包名:org.nutch.Dao
* 类描述:
* 创建人:梁章荣
* 创建时间:Oct 15, 2010 3:46:01 PM
* 修改人:梁章荣
* 修改时间:Oct 15, 2010 3:46:01 PM
* 修改备注:
* 个人主页:http://www.anizx.com
* 个人邮箱:jacksh.liang@gmail.com
* 个人QQ:543243603
*======================================================================
* Change History Log
* ----------------------------------------------------------------------
* Mod. No. | Date | Name | Reason | Change Req.
* ----------------------------------------------------------------------
* | Oct 15, 2010 | ZhangRong.Liang | Created | 梁章荣
*======================================================================
*/
public interface SearchDao {
/**
*
* @param keyword
* @param firstResult
* @param maxResult
* @return
* @throws Throwable
*/
public List<UserInfo> Search(String keyword,int firstResult,int maxResult) throws Throwable;
/**
*
* @param keyword
* @param firstResult
* @param maxResult
* @return
* @throws Throwable
*/
public PaginationSupport Query(String keyword,int firstResult,int maxResult) throws Throwable;
}
DAO层实现类:
package org.nutch.Dao.Impl;
import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.nutch.searcher.Hit;
import org.apache.nutch.searcher.HitDetails;
import org.apache.nutch.searcher.Hits;
import org.apache.nutch.searcher.NutchBean;
import org.apache.nutch.searcher.Query;
import org.apache.nutch.searcher.Summary;
import org.apache.nutch.util.NutchConfiguration;
import org.nutch.Dao.SearchDao;
import org.nutch.entity.UserInfo;
import org.nutch.util.PaginationSupport;
/**
*
* 项目名称:strutsnutch
* 类名称:SearchDaoImpl
* 包名:org.nutch.Dao.Impl
* 类描述:
* 创建人:梁章荣
* 创建时间:Oct 15, 2010 3:46:47 PM
* 修改人:梁章荣
* 修改时间:Oct 15, 2010 3:46:47 PM
* 修改备注:
* 个人主页:http://www.anizx.com
* 个人邮箱:jacksh.liang@gmail.com
* 个人QQ:543243603
*======================================================================
* Change History Log
* ----------------------------------------------------------------------
* Mod. No. | Date | Name | Reason | Change Req.
* ----------------------------------------------------------------------
* | Oct 15, 2010 | ZhangRong.Liang | Created | 梁章荣
*======================================================================
*/
public class SearchDaoImpl implements SearchDao {
@Override
public PaginationSupport Query(String keyword, int firstResult,
int maxResult) throws Throwable {
List<UserInfo> list = new ArrayList<UserInfo>();
Configuration conf = NutchConfiguration.create();
NutchBean bean = new NutchBean(conf);
Query query = Query.parse(keyword, conf);
Hits hits = bean.search(query, firstResult + maxResult);
int ff = firstResult + maxResult;
if (ff > hits.getTotal())
ff = (int) hits.getTotal();
System.out.println("命中条数: " + hits.getTotal());
int length = (int) Math.min(hits.getTotal(), ff);
Hit[] show = hits.getHits(0, length);
HitDetails[] details = bean.getDetails(show);
Summary[] summaries = bean.getSummary(details, query);
for (int i = firstResult; i < ff; i++) {
Hit hit = show[i];
HitDetails detail = details[i];
UserInfo info = new UserInfo();
info.setTile(detail.getValue("title"));
info.setUrl(detail.getValue("url"));
info.setId(hit.getIndexDocNo());
info.setContent(summaries[i].toHtml(true));
info.setTotal((int) hits.getTotal());
list.add(info);
}
PaginationSupport ps = new PaginationSupport(list, (int) hits
.getTotal(), maxResult, firstResult);
return ps;
}
@Override
public List<UserInfo> Search(String keyword, int firstResult, int maxResult)
throws Throwable {
List<UserInfo> list = new ArrayList<UserInfo>();
Configuration conf = NutchConfiguration.create();
NutchBean bean = new NutchBean(conf);
Query query = Query.parse(keyword, conf);
Hits hits = bean.search(query, firstResult + maxResult);
int ff = firstResult + maxResult;
if (ff > hits.getTotal())
ff = (int) hits.getTotal();
System.out.println("命中条数: " + hits.getTotal());
int length = (int) Math.min(hits.getTotal(), ff);
Hit[] show = hits.getHits(0, length);
HitDetails[] details = bean.getDetails(show);
Summary[] summaries = bean.getSummary(details, query);
for (int i = firstResult; i < ff; i++) {
Hit hit = show[i];
HitDetails detail = details[i];
UserInfo info = new UserInfo();
info.setTile(detail.getValue("title"));
info.setUrl(detail.getValue("url"));
info.setId(hit.getIndexDocNo());
info.setContent(summaries[i].toHtml(true));
info.setTotal((int) hits.getTotal());
list.add(info);
}
return list;
}
}
Service接口:
package org.nutch.Manager;
import java.util.List;
import org.nutch.entity.UserInfo;
import org.nutch.util.PaginationSupport;
/**
*
* 项目名称:strutsnutch
* 类名称:SearchManager
* 包名:org.nutch.Manager
* 类描述:
* 创建人:梁章荣
* 创建时间:Oct 15, 2010 3:47:25 PM
* 修改人:梁章荣
* 修改时间:Oct 15, 2010 3:47:25 PM
* 修改备注:
* 个人主页:http://www.anizx.com
* 个人邮箱:jacksh.liang@gmail.com
* 个人QQ:543243603
*======================================================================
* Change History Log
* ----------------------------------------------------------------------
* Mod. No. | Date | Name | Reason | Change Req.
* ----------------------------------------------------------------------
* | Oct 15, 2010 | ZhangRong.Liang | Created | 梁章荣
*======================================================================
*/
public interface SearchManager {
/**
*
* @param keyword
* @param firstResult
* @param maxResult
* @return
* @throws Throwable
*/
public List<UserInfo> Search(String keyword,int firstResult,int maxResult) throws Throwable;
/**
*
* @param keyword
* @param firstResult
* @param maxResult
* @return
* @throws Throwable
*/
public PaginationSupport Query(String keyword,int firstResult,int maxResult) throws Throwable;
}
Service接口实现类:
package org.nutch.Manager.Impl;
import java.util.List;
import org.nutch.Dao.SearchDao;
import org.nutch.Dao.Impl.SearchDaoImpl;
import org.nutch.Manager.SearchManager;
import org.nutch.entity.UserInfo;
import org.nutch.util.PaginationSupport;
/**
*
* 项目名称:strutsnutch
* 类名称:SearchManagerImpl
* 包名:org.nutch.Manager.Impl
* 类描述:
* 创建人:梁章荣
* 创建时间:Oct 15, 2010 3:47:32 PM
* 修改人:梁章荣
* 修改时间:Oct 15, 2010 3:47:32 PM
* 修改备注:
* 个人主页:http://www.anizx.com
* 个人邮箱:jacksh.liang@gmail.com
* 个人QQ:543243603
*======================================================================
* Change History Log
* ----------------------------------------------------------------------
* Mod. No. | Date | Name | Reason | Change Req.
* ----------------------------------------------------------------------
* | Oct 15, 2010 | ZhangRong.Liang | Created | 梁章荣
*======================================================================
*/
public class SearchManagerImpl implements SearchManager {
private SearchDao dao=new SearchDaoImpl();
@Override
public PaginationSupport Query(String keyword, int firstResult,
int maxResult) throws Throwable {
return this.dao.Query(keyword, firstResult, maxResult);
}
@Override
public List<UserInfo> Search(String keyword, int firstResult, int maxResult)
throws Throwable {
return this.dao.Search(keyword, firstResult, maxResult);
}
}
web 部分:
package org.nutch.web;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.nutch.Manager.SearchManager;
import org.nutch.Manager.Impl.SearchManagerImpl;
import org.nutch.entity.UserInfo;
import org.nutch.util.PaginationSupport;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* 项目名称:strutsnutch
* 类名称:SearchAction
* 包名:org.nutch.web
* 类描述:
* 创建人:梁章荣
* 创建时间:Oct 15, 2010 3:48:36 PM
* 修改人:梁章荣
* 修改时间:Oct 15, 2010 3:48:36 PM
* 修改备注:
* 个人主页:http://www.anizx.com
* 个人邮箱:jacksh.liang@gmail.com
* 个人QQ:543243603
*======================================================================
* Change History Log
* ----------------------------------------------------------------------
* Mod. No. | Date | Name | Reason | Change Req.
* ----------------------------------------------------------------------
* | Oct 15, 2010 | ZhangRong.Liang | Created | 梁章荣
*======================================================================
*/
public class SearchAction extends ActionSupport {
private static final long serialVersionUID = 6777785427777695618L;
private Integer startIndex;
private PaginationSupport ps;
private UserInfo info;
public Integer getStartIndex() {
return startIndex;
}
public void setStartIndex(Integer startIndex) {
this.startIndex = startIndex;
}
public PaginationSupport getPs() {
return ps;
}
public void setPs(PaginationSupport ps) {
this.ps = ps;
}
public UserInfo getInfo() {
return info;
}
public void setInfo(UserInfo info) {
this.info = info;
}
@SuppressWarnings("unchecked")
@Override
public String execute() throws Exception {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
HttpServletResponse response = (HttpServletResponse) ActionContext
.getContext().get(ServletActionContext.HTTP_RESPONSE);
//PrintWriter out = response.getWriter();
request.setCharacterEncoding("gb2312");
String keyword = request.getParameter("name");
System.out.println(keyword+"中文");
String _startIndex;// 当前页
_startIndex = (String) request.getParameter("startIndex");
if (null == _startIndex) {
startIndex = 0;
} else {
startIndex = Integer.parseInt(_startIndex);
}
SearchManager mamanger = new SearchManagerImpl();
try {
ps = mamanger
.Query(keyword, startIndex,PaginationSupport.PAGESIZE);
} catch (Throwable e) {
e.printStackTrace();
}
List list = ps.getItems();
request.setAttribute("list", list);
request.setAttribute("name", keyword);
request.setAttribute("item", ps.getIndexes());
request.setAttribute("page", ps.page(ps.getPageSize(), ps.getCurrentPage()));
return SUCCESS;
}
}
Junit测试代码:
package org.nutch.JUnit;
import static org.junit.Assert.fail;
import java.util.List;
import org.junit.Test;
import org.nutch.Manager.SearchManager;
import org.nutch.Manager.Impl.SearchManagerImpl;
import org.nutch.entity.UserInfo;
import org.nutch.util.PaginationSupport;
/**
*
* 项目名称:strutsnutch
* 类名称:SearchManagerTest
* 包名:org.nutch.JUnit
* 类描述:
* 创建人:梁章荣
* 创建时间:Oct 15, 2010 3:55:53 PM
* 修改人:梁章荣
* 修改时间:Oct 15, 2010 3:55:53 PM
* 修改备注:
* 个人主页:http://www.anizx.com
* 个人邮箱:jacksh.liang@gmail.com
* 个人QQ:543243603
*======================================================================
* Change History Log
* ----------------------------------------------------------------------
* Mod. No. | Date | Name | Reason | Change Req.
* ----------------------------------------------------------------------
* | Oct 15, 2010 | ZhangRong.Liang | Created | 梁章荣
*======================================================================
*/
public class SearchManagerTest {
private SearchManager manager=new SearchManagerImpl();
@Test
public void testSearch() {
fail("Not yet implemented");
}
@Test
public void testQuery() {
try {
PaginationSupport ps=manager
.Query("qq", 0, PaginationSupport.PAGESIZE);
List list=ps.getItems();
int id[]=ps.getIndexes();
for(int i=0;i<list.size();i++)
{
UserInfo info=(UserInfo)list.get(i);
System.out.println(info.getTile());
}
} catch (Throwable e) {
e.printStackTrace();
}
}
}
- nutch+struts2整合二次开发代码
- Nutch二次开发
- nutch导入myeclipse 二次开发
- Nutch二次开发介绍
- Nutch二次开发总结(一)
- Nutch二次开发总结(二)
- Nutch二次开发总结(二)
- Nutch二次开发总结(一)
- nutch-2.1、mysql整合
- Nutch 二次开发总结 - Nutch查询分析得出的结论
- Nutch 二次开发总结 - Nutch查询分析得出的结论
- Hibernate和struts2的整合代码截图
- Struts2和Spring的整合代码截图
- nutch安装,使用,二次开发入门 ( by quqi99 )
- Nutch 二次开发需要修改的东西
- Nutch 二次开发需要修改的东西
- Eclipse+nutch-1.2+tomcat(二次开发)
- Nutch 二次开发之parse正文内容
- MEA in Maximo
- BoundField中DataFormatString格式化数字,货币,日期
- [转]JDK如何设置环境变量
- .NET多线程的探讨
- 文件/流的加密解密方法
- nutch+struts2整合二次开发代码
- WinCE上BINFS实现详解
- Hibernate JPA 配置
- Spring 2.5 JPA hibernate 使用方法整理(一)
- 一周实习体会
- 常用命令
- WPF/Silverlight 控件的基本组成
- http://www.cnblogs.com/dubingsky/archive/2009/07/16/1524742.html
- 关于sql server 代理(已禁用代理 xp)