⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmstemplatesearch.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateSearch.java,v $
 * Date   : $Date: 2006/03/27 14:52:51 $
 * Version: $Revision: 1.13 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * For further information about Alkacon Software GmbH, please see the
 * company website: http://www.alkacon.com
 *
 * For further information about OpenCms, please see the
 * project website: http://www.opencms.org
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.opencms.frontend.templateone;

import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsLocaleManager;
import org.opencms.main.CmsException;
import org.opencms.search.CmsSearch;
import org.opencms.search.CmsSearchResult;
import org.opencms.search.Messages;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.CmsWorkplace;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

/**
 * Provides methods for the search result JSP page.<p>
 * 
 * @author Andreas Zahner 
 * 
 * @version $Revision: 1.13 $ 
 * 
 * @since 6.0.0 
 */
public class CmsTemplateSearch extends CmsTemplateBean {

    /** Request parameter name for the search entire website flag.<p> */
    public static final String PARAM_SEARCHALL = "entire";

    /** Stores the URI of the page calling the search result page.<p> */
    private String m_pageUri;

    /** The search entire website flag.<p> */
    private boolean m_searchAll;

    /**
     * Empty constructor, required for every JavaBean.<p>
     */
    public CmsTemplateSearch() {

        super();
    }

    /**
     * Constructor, with parameters.<p>
     * 
     * Use this constructor for the template.<p>
     * 
     * @param context the JSP page context object
     * @param req the JSP request 
     * @param res the JSP response 
     */
    public CmsTemplateSearch(PageContext context, HttpServletRequest req, HttpServletResponse res) {

        super();
        init(context, req, res);
    }

    /**
     * Builds the html to display error messages on the search result page.<p>
     * 
     * If no error occurs, an empty String will be returned.<p>
     * 
     * @param search the search result object holding all necessary information
     * @param results the list of result objects to display
     * @return the html to display error messages
     */
    public String buildSearchErrorMessages(CmsSearch search, List results) {

        StringBuffer result = new StringBuffer(32);
        if (search.getLastException() != null) {
            // the search did not run properly, create error output
            String errorMessage = "";
            if (((CmsException)search.getLastException()).getMessageContainer().getKey().equals(
                Messages.ERR_QUERY_TOO_SHORT_1)) {
                // query String was too short
                errorMessage = key("search.error.wordlength");
            } else {
                // other error
                errorMessage = key("search.error.details");
            }
            result.append("<h3>");
            result.append(key("search.error"));
            result.append("</h3>\n");
            result.append("<p>");
            result.append(errorMessage);
            result.append("</p>\n");
            result.append("<!-- Exception message: ");
            result.append(search.getLastException().toString());
            result.append("// -->\n");
        } else if (results == null || results.size() == 0) {
            // no results were found for the current query
            result.append("<h3>");
            result.append(key("search.error.nomatch"));
            result.append("</h3>\n");
        }
        return result.toString();
    }

    /**
     * Builds the html for the search result page headline.<p>
     * 
     * @return the html for the search result page headline 
     */
    public String buildSearchHeadline() {

        StringBuffer result = new StringBuffer(32);
        result.append(key("search.headline"));
        if (!isSearchAll() && !"/".equals(getStartFolder())) {
            result.append(key("search.headline.area"));
            result.append(getAreaName());
        } else {
            result.append(key("search.headline.all"));
        }
        return result.toString();
    }

    /**
     * Builds the html for the links to previous and next search result pages.<p>
     * 
     * @param search the search result object holding all necessary information
     * @return the html for the links to previous and next search result pages
     */
    public String buildSearchLinks(CmsSearch search) {

        StringBuffer result = new StringBuffer(32);
        boolean showPageLinks = false;
        // additional parameters for search result page in module folder
        StringBuffer additionalParams = new StringBuffer(16);
        if (search.getPreviousUrl() != null || search.getNextUrl() != null) {
            // there is at least one previous or next page, build page links
            showPageLinks = true;
            result.append("<div class=\"searchlinks\">\n");
            // fill additional parameters
            additionalParams.append("&uri=");
            additionalParams.append(CmsEncoder.encode(getRequestContext().getUri()));
            additionalParams.append("&");
            additionalParams.append(CmsLocaleManager.PARAMETER_LOCALE);
            additionalParams.append("=");
            additionalParams.append(getRequestContext().getLocale());
            additionalParams.append("&");
            additionalParams.append(PARAM_SEARCHALL);
            additionalParams.append("=");
            additionalParams.append(isSearchAll());
        }
        if (search.getPreviousUrl() != null) {
            // build the link to the previous page
            result.append("\t<input type=\"button\" class=\"formbutton\" value=\"&lt;&lt; ");
            result.append(key("search.previous"));
            result.append("\" onclick=\"location.href=\'");
            result.append(link(search.getPreviousUrl()));
            result.append(additionalParams);
            result.append("\';\">");
        }
        Map pageLinks;
        try {
            // first set request context URI to form URI to obtain right page links
            getRequestContext().setUri(CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/pages/search.html");
            pageLinks = search.getPageLinks();
        } finally {
            // reset URI to page
            getRequestContext().setUri(getPageUri());
        }
        Iterator i = pageLinks.keySet().iterator();
        while (i.hasNext()) {
            // show the page links
            int pageNumber = ((Integer)i.next()).intValue();
            String pageLink = link((String)pageLinks.get(new Integer(pageNumber)));
            result.append("&nbsp;&nbsp;");
            if (pageNumber != search.getSearchPage()) {
                // create a link to the page
                result.append("<a href=\"");
                result.append(pageLink);
                result.append(additionalParams);
                result.append("\">");
                result.append(pageNumber);
                result.append("</a>");
            } else {
                // show currently active page, but not as link
                result.append(pageNumber);
            }
        }
        if (search.getNextUrl() != null) {
            // build the link to the next page
            result.append("&nbsp;&nbsp;<input type=\"button\" class=\"formbutton\" value=\"");
            result.append(key("search.next"));
            result.append(" &gt;&gt;\" onclick=\"location.href=\'");
            result.append(link(search.getNextUrl()));
            result.append(additionalParams);
            result.append("\';\">");
        }
        if (showPageLinks) {
            result.append("\n</div>\n");
        }
        return result.toString();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -