I had a GSP that displayed results from a search using the Grails paginateAjax tag. For awhile, everything was working fine and then we started to see some “erratic” behavior with the pagination. The initial search would always work correctly (the one that displays page one). It would show the correct number of pages in the pagination links too.
However, when the user would click the “Next” button, the Search Results panel would just say no results are found. This problem was very confusing as the initial search was able to determine that pagination was necessary and calculate the correct number of pages. However, clicking on those pagination links would not work.
The root cause of the problem was spaces in the values that are being searched on. In one example where this problem could be seen, I would search on a customer name of “Test Customer” (note the space between the two words).
When the Search button is clicked, the form on the page is submitted and the values in the text fields get passed to the server in the request as they are entered. So in this case “Test Customer” is passed in the request. With this value we are able to get the first page of search results with no problem. When HTML (i.e. GSP) is being created to render the results, the pagination buttons are put on the page.
Those “buttons” are nothing more than <a href=”some_url”> links that were created using the Grails createLink tag and styled. As part of creating those links, Grails looks at the query parameters and replaces spaces with “+” signs to make a valid URL (that last one is a Google Books link to page 147 of the book “The Definitive Guide to Grails”). Remember, URLs are not allowed to have spaces in them. And as far as I can tell, replacing spaces with “+” signs is an accepted practice.
In our controller code when we go to get the next page of results, we essentially ask the request for the customer name and we get the value “Test+Customer”. We were using that value without checking for the “+” sign. This would result in us executing a query against the database with a “WHERE customername = ‘Test+Customer’ “. This would not get any results because there is no customer with name “Test+Customer”. It’s “Test Customer”.
It’s really rather simple. In the controller code, we need to replace “+” signs with a space before using those values in a query.