nutch+struts2整合二次开发代码

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();
        }
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值