[opencms-dev] Search pagination not working for anonymous user

Manoj.Sharma at nevagroup.com Manoj.Sharma at nevagroup.com
Mon May 18 07:35:48 CEST 2015


Hi All,
           I have written a customize search page which search for content 
on opencms 9 system. search working perfectly when user logined(as Admin) 
and pagination also working perfectly. But when i logged out and try to 
search any content then first page displayed correctly but pagination did 
not working, it does not taking me to the next page while i press next 
button in pagination or click any number in pagination. What exactly 
problem with the pagination i did not understand as in url page number 
also getting changed for pagination. Please help on this, code is given 
below:

<%@ page buffer="8kb" import="org.opencms.main.*, org.opencms.search.*, 
org.opencms.file.*, org.opencms.jsp.*, java.util.*" %>
<%@ page taglibs="c,cms,fn" %>
<%@page session = "false" trimDirectiveWhitespaces="true" import = 
"org.opencms.jsp.CmsJspActionElement"%>
<%@page import = 
"org.opencms.file.CmsObject,org.opencms.relations.CmsCategoryService, 
org.opencms.file.CmsResource, java.util.List, java.util.Iterator, 
java.text.SimpleDateFormat, java.util.Date"%>
<%@page import = "org.opencms.file.CmsPropertyDefinition, 
java.util.Comparator, java.util.Collections" %>

 
        <%
                org.opencms.jsp.CmsJspActionElement cms = new 
CmsJspActionElement(pageContext, request, response);
 
                // Get the search manager
                CmsSearchManager searchManager = 
OpenCms.getSearchManager(); 
        %>
        <jsp:useBean id="search" scope="request" 
class="org.opencms.search.CmsSearch">
                <jsp:setProperty name = "search" property="*"/>
                <%
                        search.setMatchesPerPage(25);
                %>
        </jsp:useBean>
        <%
                Enumeration parameter = request.getParameterNames();
                if(!parameter.hasMoreElements()){
                        response.sendRedirect("domain-name/search");
                        return;
                }
        if( request.getParameter("query") ==null){
                        search.init(cms.getCmsObject());
                }else{
                        search.setQuery(request.getParameter("query"));
 application.setAttribute("query",request.getParameter("query"));
                        search.init(cms.getCmsObject());
                }
                // Create a JSP action element
    %>

                                <div class="search-result">
                                        <h1 
class="margin-bottom-10">Search result</h1>
 
                                        <%
                                                int resultno = 1;
                                                int pageno = 0;
                                                if 
(request.getParameter("searchPage")!=null) { 
                                                        pageno = 
Integer.parseInt(request.getParameter("searchPage"))-1;
                                                }
                                                resultno = 
(pageno*search.getMatchesPerPage())+1;
 
                                                String fields = 
search.getFields();
                                           if (fields==null) {
                                                fields = 
request.getParameter("fields");
                                           }
 
                                           List result = 
search.getSearchResult();
                                           if (result == null) {
                                        %>
                                        <%
                                                        if 
(search.getLastException() != null) { 
                                        %>
                                        <h3>Error</h3>
                                        <%= 
search.getLastException().toString() %> 
                                        <%
                                                        }
 
                                                } else {
 
                                                        ListIterator 
iterator = result.listIterator();
                                        %>
                                        <h3 class="result-found 
margin-bottom-10"><%= search.getSearchResult().size() %> Results found for 
query < <%= application.getAttribute("query") %> ></h3>
                                        <%
                                                        while 
(iterator.hasNext()) {
 CmsSearchResult entry = (CmsSearchResult)iterator.next();
                                        %>
                                        <div class="result-row" 
id="result-row-<%=resultno%>" onmouseout="outWrapper('<%=resultno%>')" 
onmouseover="hoverWrapper(<%=resultno%>)">
                                                <a class="result-record" 
href="<%= 
cms.link(cms.getRequestContext().removeSiteRoot(entry.getPath())) %>">
                                                        <h3 
class="margin-bottom-10" style="line-height:1.6;" 
id="title-<%=resultno%>"><%= entry.getTitle() %></h3>
                                                        <span 
class="img-date-wrapper">
                                                                <h6 
class="created-date" id="created-date-<%=resultno%>"><%= 
entry.getDateCreated() %></h6>
                                                        </span>
                                                        <span 
class="desc-wrapper">
                                                                <h5 
class="desc" id="desc-<%=resultno%>"><% if(entry.getExcerpt()==null || 
entry.getExcerpt()==""){out.println("No description available.");}else{ 
out.println(""+entry.getExcerpt()+" ...");} %></h5>
                                                        </span>
 
                                                </a>
                                        </div> 
                                        <%
                                                        resultno++;  
                                                        }
                                                }
                                        %> 
                                        <div 
class="next-prev-paging-wrapper margin-bottom-10">
                                        <%
                                                if 
(search.getPreviousUrl() != null) {
                                        %>
                                                          <input 
type="button" class="btn-u previous" value="<< previous" 
onclick="location.href='<%= cms.link(search.getPreviousUrl()) %>';">
                                        <%
                                                }
                                                Map pageLinks = 
search.getPageLinks();
                                                Iterator i = 
pageLinks.keySet().iterator();
                                                out.println("<div 
class='paging'>");
                                                while (i.hasNext()) {
                                                        int pageNumber = 
((Integer)i.next()).intValue();
                                                        String pageLink = 
cms.link((String)pageLinks.get(new Integer(pageNumber)));
                                                        if (pageNumber != 
search.getSearchPage()) {
                                        %>
                                                                <a 
href="<%= pageLink %>&fields=<%= fields %>"><%= pageNumber %></a>
 
                                        <%
                                                        } else {
                                        %>
                                                                <span 
class="currentpage"><%= pageNumber %></span>
                                        <%
                                                        }
                                                }
                                                out.println("</div>");
                                                if (search.getNextUrl() != 
null) {
                                        %>
                                                          
 <input type="button" class="btn-u next" value="next >>" 
onclick="location.href='<%= cms.link(search.getNextUrl()) %>';">
 
                                        <%
                                                } 
                                        %>
                                        </div>
                                </div>


Cheers
Manoj Kumar Sharma
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://webmail.opencms.org/pipermail/opencms-dev/attachments/20150518/4ed86f6f/attachment.htm>


More information about the opencms-dev mailing list