2013-04-28期-根据输入条件查询索引内容

BaseAction.java 基础类

/**
 *  Class Name: BaseAction.java  
 *  @author JLC
 *  @version 1.0
 */
public class BaseAction extends ActionSupport {

    private static final long serialVersionUID = -2983580695806850367L;
    /**
     * 
     */
    protected ServletContext getServletContext(){
        return ServletActionContext.getServletContext();
    }
    /**
     * 
     */
    protected HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }
    /**
     * 
     */
    protected HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }
    /**
     * 
     */
    protected HttpSession getSession() {
        return getRequest().getSession();
    }
    /**
     * 取得访问域名
     * @return
     */
    public String getURL(){
        return getRequest().getServerName();
    }
    /**
     * 存放Cookie
     * @param name
     * @param value
     */
    public void addCookie(String name,String value){
        Cookie cookie = new Cookie(name, value);
        cookie.setMaxAge(60*60*24);
        cookie.setPath("/");
        getResponse().addCookie(cookie);
    }
    /**
     * 删除Cookie
     * @param name
     * @param value
     */
    public void removeCookie(String name){
        Cookie cookie = new Cookie(name, null);
        cookie.setMaxAge(0);
        cookie.setPath("/");
        getResponse().addCookie(cookie);
    }
    /**
     * Cookie取得
     * @return
     * @throws Exception
     */
    public String getCookie(String name){
      try{
          Cookie[] cookies = getRequest().getCookies();
          for(Cookie cookie : cookies){
              cookie.setPath("/");
            if(cookie.getName().equals(name)){
                return cookie.getValue();
            }
          }
      }catch(Exception e){  }
      return "";
    }
    /**
     *  获取IP地址
     */
    public String getIpAddress(){
        HttpServletRequest request= getRequest();
        String ip = request.getHeader("x-forwarded-for");    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
            ip = request.getHeader("WL-Proxy-Client-IP");    
        }    
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {    
           ip = request.getRemoteAddr();    
        } 
        return ip;
    }
}

SearchAction.java 搜索类

/**
 *  Class Name: SearchAction.java   搜索类
 *  @author JLC
 *  @version 1.0
 */
public class SearchAction extends BaseAction {

    /**
     * 
     */
    private static final long serialVersionUID = 3141706082945311957L;
    /**
     * 搜索关键字
     */
    private String keyWord;
    /**
     * 结果集
     */
    private List<ContentObject> searcheResult;

    private ContentObject contentObj;
    /**
     *  Function:
     *  @author  
     *  @return
     *  @throws Exception
     */
    @Action(
      value = "index", 
      results = { @Result(name = SUCCESS,  location = "/result.jsp") }
    )  
    public String searcheContent() throws Exception {  
        searcheResult =  new ArrayList<ContentObject>();
        try{
            Version v = Version.LUCENE_42;
            //取得查询对象
            IndexReader[] readers =  SearchObject.getInstance().getSearcherReads();
            MultiReader mReaders = new MultiReader(readers);
            IndexSearcher indexSearch = new  IndexSearcher(mReaders);
            Analyzer analyzer =new StandardAnalyzer(v);
            //创建boolean查询
            BooleanQuery query = new BooleanQuery();
            String[] field = { "title", "content"};
            BooleanClause.Occur[] flags = new BooleanClause.Occur[2];
            flags[0] = BooleanClause.Occur.SHOULD;
            flags[1] = BooleanClause.Occur.SHOULD;
            Query query1 = MultiFieldQueryParser.parse(v, QueryParser.escape(keyWord), field, flags, analyzer);
            query.add(query1, Occur.MUST);
            TopScoreDocCollector topCollector = TopScoreDocCollector.create(10000, true);
            indexSearch.search(query, topCollector); 
            //取得查询结果
            TopDocs topDocs = topCollector.topDocs(); 
            int resultCount=topDocs.totalHits;
            for(int i=0;i<resultCount;i++){
                Document doc = indexSearch.doc(topDocs.scoreDocs[i].doc);
                //转换Document对象为内容对象
                ContentObject content = PackContentObject.convertDocToContent(doc);
                //加入到结果列表 返回给前台页面获取
                searcheResult.add(content);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

       return SUCCESS;  
    } 
    public String getKeyWord() {
        return keyWord;
    }
    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }
    public List<ContentObject> getSearcheResult() {
        return searcheResult;
    }
    public ContentObject getContentObj() {
        return contentObj;
    }
    public void setContentObj(ContentObject contentObj) {
        this.contentObj = contentObj;
    }
}

创建index.jsp内容查询页面

请注意页面如果要使用Struts2的标签必须加入到顶部

<%@ taglib uri="/struts-tags" prefix="s" %>

下面为页面片段内容

 <body>
     <!-- 添加索引 -->
     <s:form action="index">
         <input type="text"  name="keyWord" value=""><input type="submit" name="" value="搜索">
     </s:form>
     <a href="addcontent.jsp" >添加内容</a>
  </body>

创建result.jsp结果显示页面

下面为页面片段内容

  <body>
       <s:iterator  id="obj" value="searcheResult" >
            标题:<s:property value="#obj.title" /></p>
            内容:<s:property value="#obj.content" /></p>
            <hr>
       </s:iterator>
  </body>

实际运行效果1.查询条件“搜狐”

2.结果列表

你可能感兴趣的:(条件查询)