Over a million developers have joined DZone.
Gold Partner

Detecting the Grails Environment in a GSP

· Java Zone

The Java Zone is brought to you in parnership with New Relic. Read about the 10 Surprisingly Interesting Ways to Earn a Living Using Java and rejuvenate your love for programming.

Last night I needed to make sure that my analytics tracking script was not running anywhere but in my production environment. After reading another blog post I came up with this simple GSP if block.

**** UPDATE ****

Thanks Burt for the comment pointing out that the original code I posted is actually deprecated and for pointing out a better way for testing environment.

According to the docs (and Burt), this is the correct way to test for environment in a GSP.

<g:if env="production">
<!-- Markup to include ONLY when in production -->
</g:if>

**** The code below is deprecated ****

<g:if test="${ grails.util.GrailsUtil.getEnvironment().equals(org.codehaus.groovy.grails.commons.GrailsApplication.ENV_PRODUCTION) }">
<!-- Markup to include ONLY when in production -->
</g:if>

Hope this helps others who are looking to do the same within their GSPs.

Detecting the Grails Environment in a GSP

The Java Zone is brought to you in parnership with New Relic. Read about the 10 Surprisingly Interesting Ways to Earn a Living Using Java and rejuvenate your love for programming.

Topics:

Published at DZone with permission of Steve Good , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}