SV: [opencms-dev] Search Module Help NEEDED!!!

Stefan Uldum Grinsted stefan at e-nation.dk
Mon Nov 13 14:41:05 CET 2006


As you wish.

 

Taglibs:

<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

 

Note! I’m using JSP2, so you may not be able to use all the code as it is,
but the essentials are here.

The variable “cmsRequest” is the CmsRequestContext gotten vis
cms.getRequestContext()

 

Search form part:

            <c:if test="${not empty param.query}"><!-- this is nasty, I
know. But I couldn’t get it to search for e.g. “blå” without it interpreting
it as “blÃ¥” as the querystring looks like this “query=bl%C3%A5” -->

              <c:set var="query" scope="request"><%= new
String(request.getParameter("query").getBytes(), "UTF-8") %></c:set>

            </c:if>

            <form accept-charset="UTF-8" name="searchForm" id="searchForm"
action="<cms:link>/${cmsRequest.locale.language}/searchresults.html</cms:lin
k>" method="get">

               <input type="hidden" name="index" value="Mita-Teknik <%=
cmsRequest.getLocale().getDisplayLanguage(new java.util.Locale("en")) %>
Index"/>

               <input type="hidden" name="field" value="title"/>

               <input type="hidden" name="field" value="keywords"/>

               <input type="hidden" name="field" value="description"/>

               <input type="hidden" name="field" value="content"/>

              <input type="text" name="query" id="query" value="${query}"
class="text_100"/>

              <input type="submit" name="searchButton" id="searchButton"
value="<fmt:message key="search.button" />" class="submit" />

            </form>

 

 

Search results page:

<jsp:useBean id="search" scope="request"
class="org.opencms.search.CmsSearch">

    <jsp:setProperty name="search" property="matchesPerPage"
param="matchesperpage"/>

    <jsp:setProperty name="search" property="displayPages"
param="displaypages"/>

    <jsp:setProperty name="search" property="*"/>

  <% 

     search.init(cms.getCmsObject());

     search.setQuery((String) pageContext.findAttribute("query")); // This
refers to the earlier defined query variable in the form-part

  %>

</jsp:useBean>

 

  <c:set var="page"><c:choose><c:when test="${!empty
param.page}">${param.page -
1}</c:when><c:otherwise>0</c:otherwise></c:choose></c:set>

  <c:set var="fields"><c:choose><c:when test="${empty
search.fields}">${param.fields}<%
search.setField(request.getParameter("fields").split(" "));
%></c:when><c:otherwise>${search.fields}</c:otherwise></c:choose></c:set>

  <c:catch var="e">

     <c:set var="results" value="${search.searchResult}" />

  </c:catch>

 

     <c:choose>

       <c:when test="${e != null || !empty search.lastException}">

          <h1><fmt:message key="search.error.heading" /></h1>

          <p><fmt:message key="search.error.message" /></p>

          <c:if test="${search.lastException != null}">

            <!--  <% search.getLastException().printStackTrace(new
java.io.PrintWriter(out)); %> -->

          </c:if>

          <c:if test="e != null">

            <!--  <% ((Throwable)
pageContext.getAttribute("e")).printStackTrace(new
java.io.PrintWriter(out)); %> -->

          </c:if>

       </c:when>

       <c:otherwise>

          <c:set var="navigation">

            <div align="center">

              <c:if test="${search.previousUrl != null}">

                 <a
href="<cms:link>${search.previousUrl}&fields=${fields}</cms:link>"><fmt:mess
age key="search.page.previous" /></a>

              </c:if>

              <c:forEach items="${search.pageLinks}" var="searchPage">

                 <c:choose>

                   <c:when test="${search.searchPage != searchPage.key}">

                     <a
href="<cms:link>${searchPage.value}&fields=${fields}</cms:link>">${searchPag
e.key}</a>

                   </c:when>

                   <c:otherwise>

                     <span>${searchPage.key}</span>

                   </c:otherwise>

                 </c:choose>

              </c:forEach>

              <c:if test="${search.nextUrl != null}">

                 <a
href="<cms:link>${search.nextUrl}&fields=${fields}</cms:link>"><fmt:message
key="search.page.next" /></a>

              </c:if>

            </div>

          </c:set>

 

          <div id="content_elem_a_searchres">

            <h1><fmt:message key="search.searchresults.heading" /></h1><br
/>

            <fmt:message key="search.searchresults.result">

              <fmt:param value="${query}" />

              <fmt:param value="${search.searchResultCount}" />

            </fmt:message>

          </div>

          

          ${navigation}

          

          <div id="content_elem_b_searchres">

            <c:forEach items="${results}" var="item">

              <c:set var="sitepath" value="${fn:replace(item.path,
cmsRequest.siteRoot, '')}" />

              <c:set var="title"><c:choose><c:when test="${empty
item.title}">${sitepath}</c:when><c:otherwise>${item.title}</c:otherwise></c
:choose></c:set>

              <div class="search_results_list_item">

                 <div>

                   <img src="<cms:link>${ icons.iconPath[ sitepath ]
}</cms:link>" alt=""/><a
href="<cms:link>${sitepath}</cms:link>">${title}</a> (${item.score}%)

                 </div>

                 <div>${item.excerpt}</div>

              </div>

            </c:forEach>

          </div>

          

          ${navigation}

       </c:otherwise>

     </c:choose>

 

 

 

I hope this helps.

 

Regards Stefan

 

  _____  

Fra: opencms-dev-bounces at opencms.org
[mailto:opencms-dev-bounces at opencms.org] På vegne af Mario D'Angelo
Sendt: 13. november 2006 14:01
Til: opencms-dev at opencms.org
Emne: [opencms-dev] Search Module Help NEEDED!!!

 

 

Hi List,

 

I am really losing my mind trying to get the cmsSearch to work on OpenCms
6.2.2, but with non success. Can anybody that had success on using it on the
same version (6.2.2) post both the search form and the search jsp code?

 

I really need help with this,

 

Thank you,

 

Mario D’Angelo

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20061113/eb032171/attachment.htm>


More information about the opencms-dev mailing list