Viewing the source of a compiled GSP in Grails
March 15, 2008 – 3:55 pmSo you’re working happily on your Grails application, coding away, being extremely productive until BOOM! You get a Grails Runtime Exception error page like this when you hit a GSP:

What the heck? Reading the stack trace you see that the exception is caused somewhere in a GSP:

You’ve found the error of course, it’s right there on line 33 of the grails-app/views/person/show.gsp. Um, not quite. If you look at line 33 of show.gsp you won’t find anything of consequence:
<td valign=”top” class=”name”>Name:</td>
There isn’t any code on that line that could possibly cause a null pointer exception. So then you realize – the line 33 the stack trace is referring to is the line in the compiled GSP! But where oh where would that source for the GSP be? You search in your ~/.grails directory but don’t find anything obvious there. A couple fruitless google searches later and you start undoing your changes, one by one, until you find the line of code that caused your problem. What if, you ask, there was an easy way to see the source of a compiled Grails GSP? Then you find out:
there is.
When running in “development” mode (i.e. this trick doesn’t work in test or production or other environments), just append the parameter “showSource” to the URL (e.g. http://localhost:8080/[APP_NAME]/person/show/3?showSource) and TADA – you get the compiled source code:

Now you can figure out where line 33 is (I usually end up copying the source into an editor like TextMate that shows line numbers) and see what caused your error. In this particular case, gender is null and when the GSP tries to obtain the “code” attribute on the “gender” property, it causes a NPE.

Add a quick fix:
person.gender?.code
try the page again and everything’s hunky dory.
Thank you showSource parameter. You just made my life oh so much easier.

12 Responses to “Viewing the source of a compiled GSP in Grails”
Nice! I didn’t realize you could do this and had briefly looked around previously for the compiled source to my .gsp pages without any luck. This makes it super easy to find what’s going on.
I bet with a little bit of playing around it’d be possible to modify showSource to give it a line number value so that the resulting source would return just that line (possibly with a few context lines around it).
By tednaleid on Mar 15, 2008
Thanks for this post. I know this will come in very handy. I’ve worked with some very basic Grails apps used in different tutorials. I did face such long, cryptic error dumps when I started customizing them. Knowing that one can see the source will be be a good time saver.
But I think better error messages should be on the Grails roadmap… I better head over to the Grails bug tracking system and see if they have something like this listed.
Cheers again!
By Indy Nagpal on Mar 16, 2008
wow. that’s an important tip! thanks
By Tom on Mar 17, 2008
When i tried it, the source of error page was shown. It was a syntax/parse error though..
By Uysal on Mar 19, 2008
Wow, nice. You just blew my mind. I was wondering how to get at those compiled sources. Is this documented? Wow.
By Shawn Hartsock on May 13, 2008
As of Grails 1.0.3 line numbers are apparently appended to each line. No more cop/paste to the editor!
By ClutchControl on Jun 10, 2008
Hi, nice post! but this way we are only able to get the source generated for the GSP, what if I want to get the source generated for a template. Basically I need to match the error line number in the template with the source generated(if possible). But source only includes the line where render call is made, its not including the full template. Thanks…
By Pradeep on Nov 28, 2008
@Pradeep – check out http://www.piragua.com/2008/03/31/using-the-showsource-parameter-trick-with-rendered-templates/
By Mike on Nov 30, 2008
Thanks for you previous response. It helped…
I got another requirement where I need to get the source of a template which resides out of the parent folder. What I mean is
For Example: I have a profile.gsp in customer folder and I have a template _demoForm.gsp in Shared folder.
This template is used in profile.gsp.
It will be great if there is some simple solution for getting the source of that template.
Thanks…
Thanks in advance
By Pradeep on Dec 2, 2008
@Pradeep
Same concept applies even if the template is not in the same folder, for instance, if I had a common GSP in the root of the views folder, you can see the source this way: http://localhost:8080/_addressForm.gsp?showSource
By Mike on Dec 6, 2008