Grails Cookbook - A collection of tutorials and examples

How to make Grails use an external properties file for datasource configuration

This tutorial will show how to use an external properties file to configure the datasource of a Grails application. Hardcoding database properties should only be limited for development purposes. But when deploying an application to another environment (E.g. test, UAT, production), it is usually convenient to be able to change things freely.

Grails render images on the fly in GSP

This tutorial will show how to generate PNG images on the fly and display inside a GSP. This can serve as a basis on how to create a more complex behavior. For example, creating report graphs for display in your applications.

Grails Example - Upload Excel Data File To Database

This example will show how to handle Excel upload and save each row in the file as a record in the database.

Grails Spring Security Core Plugin - Registering Callback Closures

I was searching for a way to hook business logic after successful user login while using Spring Security Core plugin. The simplest way to do this is to register callback closures. It let's you hook your custom code inside Config.groovy after certain Spring Security events.

Grails Google Maps Geocoding Example

This example shows how to translate an address to it's corresponding latitude and longitude values using Google Map's Service.

Grails - How To Create Custom Table Index Or Composite Index

By default, Grails will create the necessary indexes on primary keys and foreign keys in all domain classes. However, we sometimes need to create additional indexes for performance purposes.

How to make Grails use JNDI datasource with Tomcat

The most common way to configure database access in Grails is to hard code the connection properties inside Datasource.groovy. This is great for development purposes. But for production, we want a flexible way where admins can change which database to connect to, without needing to recompile the application.
The standard way of doing this is to leverage JNDI. We configure the database connection in the application server, and pass it to our Grails application. Below is an example on how to do it with Tomcat.

How To Check If Environment is Test, Development, or Production in Grails

Sometimes it is very helpful to execute some code depending on which environment you are in. For example, you may want to insert test data inside Bootstrap when you run the application in development mode, but not in production mode.
This is simple in Grails on different sections of your application.

Add MySQL driver jar to a Grails Project using BuildConfig

In the old days, the common way of adding external libraries to a Grails project is by adding the JAR files in the lib folder of your application. This method is easy and simple. But the more modern and recommended way is to utilize BuildConfig.

Improving Performance of Large Batch Insert

Sometimes we need to insert large number of rows to the database. (For example: transferring data from external source to our database). It is preferable that performance is fast and the expected time to finish is proportional to the number of rows to be inserted.