Grails turned my spaces into plus signs!

The Problem

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 Cause

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”.

The Solution

It’s really rather simple.  In the controller code, we need to replace “+” signs with a space before using those values in a query.


About stevewall123

I am a Lead Software Engineer in Minneapolis working for Thomson Reuters. I am currently working on projects using Java, JavaScript, Spring, Elasticsearch, Hazelcast, Liquibase and Tomcat. Previously, I used C#, GWT, Grails, Groovy, JMS and JBoss Drools Guvnor. In the past I have worked on projects using J2EE, Swing, Webwork, Hibernate, Spring, Spring-WS, JMS, JUnit and Ant.
This entry was posted in Grails and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s