1

This is the situation:

I have 3 pages, Home/Search/Post

I have a header and the search box is in that header which is component. Controller for that header component is SearchController.

So first time user type and search I am calling a search method in thr SearchController which actually returns the Pagereference with parameter(I want the query string) of /Search.page.

But when i search again in the search box, it doent show any search result in vf page. I have checked the developer console, and it is showing the result in it. I don't know what there is no result in second time, it actually display the same result again and again. I am giving my code so that you can clearly understand the question.

Header.component

<apex:component controller="SearchController">
<apex:form enctype="application/x-www-form-urlencoded">
<apex:inputText id="searchText" value="{!searchText}" styleclass="text-box search-header" html-placeholder="Search for Blog"/>
<apex:commandButton action="{!search}" value=" " id="search"/>
</apex:form>
</apex>

HomePage

<apex:page showheader="false" controller="HomePageController" standardStylesheets="false">
<c:header />
</apex>

SearchController

public with sharing class SearchController {
public SearchController(){
        queryString = ApexPages.currentPage().getParameters().get('q');
        searchText = queryString;
        system.debug('~~queryString'+queryString);

        *All Searching code goes here...*

}
public Pagereference **search**(){
        system.debug('~~search '+searchText);
        Pagereference p = Page.Search;
        p.getParameters().put('q',searchText);
        return p;

    }
}

Search Page (Same as Home page)

<apex:page showheader="false" controller="SearchController" standardStylesheets="false">
<c:header />
</apex>
1

It seems your issue is the URL Parameter q is not being set. There is no Page Reference method like setParameters(), but you can do the following:

public with sharing class SearchController {
    String searchText;

    public Pagereference search(){
        return new PageReference('/apex/Search?q=' + searchText);
    }
}

If this VF Page is used in a Site, you'll need to use your Site Prefix rather than /apex. This is easily done using the Site.getPathPrefix() method:

Example:

public with sharing class SearchController {
    String searchText;

    public Pagereference search(){
        String pathPrefix = Site.getPathPrefix();
        if(String.isBlank(pathPrefix))
            pathPrefix = '/apex';
        return new PageReference(pathPrefix + '/Search?q=' + searchText);
    }
}

Reference:


Update

To sort out the issue, let's write out the Use Case. I think the following is what you're trying to achieve:

  1. Search text is inputted, and the Component Controller returns a Page Reference to the VF Page Search with a URL Parameter q containing necessary data.
  2. In the Controller for Search, it checks if there is any data in the URL Parameter q, and if so, it "performs the the search" and gathers relevant data to display on the VF Page.

(1) is achieved with above, so for (2), make sure Search is properly getting the q URL Parameter and then properly executing the search.

5
  • Thank for replying. Actually I have tried this also but its not working, I am not able to figure it out what is happening. – ankit maini Aug 17 '14 at 20:36
  • Check out my update. – Scott Pelak Aug 17 '14 at 20:55
  • Yes you are right, Now here is my story. When I am at Home Page and perform search operation it call the search method, In search method I am NOT performing the search operation, rather than this I actually redirect to Search page with parameter in URL. eg. c.ap1.visual.force.com/apex/Search?q=Group+By+in+apex Now the constructor comes in the execution, it checks the url parameter and perform the search and display the result. IF I move my searching in search method I gets ?q=null because at that time NO url param. I don't know how to handle this, I need url parameter + search result – ankit maini Aug 18 '14 at 7:13
  • So, the Search page's controller looks for a Url Parameter and performs the search based on the parameter given. The next step would be to make sure the Home page component sends a "proper" Url Parameter. What does the Search controller do if q == null? As well as the other possibilities. It might help by writing a static method in the Search controller that generates the Url Parameter based on a String input, and then you just call that method in the Home page controller. That way you always get a proper Url Parameter – Scott Pelak Aug 19 '14 at 14:06
  • Correct.. Now its working Thanks @amator – ankit maini Aug 20 '14 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.