Tweaking Grails Scaffolding
Posted by jt - 31/12/09 at 08:12 amOne of the really cool things about Grails is its ability to auto-magically create CRUD screens. This can be very handy for proto-typing and learning. Typically, you’ll change the generated screens as the application grows. I’m building a large enterprise type application in Grails. For the small look-up type tables, the default scaffolding is just fine.
I’ve come across a situation, where I keep changing the same thing over and over in the generated views. Being the lazy developer that I am, its given me the motivation to fix it. One handy feature of Grails is if you add Date properties ‘lastUpdated’ and ‘dateCreated’ to any domain class, the GORM framework will automatically populate these for you. Now of course it would be pointless to update these via a web form. Actually it probably would be rather undesirable to do this. I googled this issue, and did not find a solution. I did find a Jira ticket for it though.
The solution is easy enough though. We just need to make a minor change to the default scaffolding. Its just a matter of installing the templates and making a few edits.
Lets walk through a quick example.
Here is my domain class:
class OrderType { Company company Warehouse warehouse String orderTypeCode String description Date dateCreated Date lastUpdated static constraints = { orderTypeCode(unique:['company', 'warehouse'], blank:false, maxLength:30) description(blank:false, maxLength:50) } String toString() { return orderTypeCode } }
The default scaffolding generates the following:
List:
Add Order Type:
Edit Order Type:
This are the views I wish to change. There is a forth screen called ’show’, which shows the details of the record. To me, it makes sense to leave the date values on this screen.
To make the changes, the first step is to install the templates. Run the command:
grails install-templates
This command will install the templates in /src/templates. We need to edit the following files:
- /src/templates/scaffolding/create.gsp
- /src/templates/scaffolding/edit.gsp
- /src/templates/scaffolding/list.gsp
In each file, locate this section:
Here we just need to add the columns we wish to exclude from the view to the list ‘excludedProps’ as I have done below.
Now if we re-generate the views, we can see the changes.
Tah-dah – no more date fields! This change will be there for my project on any other CRUD screens I create with the scaffolding.
One important note, there is a display constraint you can use in the domain class. (This is virtually un-documented from what I can tell.) I could have done this:
dateCreated(display:false)
But, this is a all or nothing option. In this case, I wanted the values to be generated on the ’show’ views, so I could not use this constraint option.






A bunch of random technology stuff that has my attention. I work with a lot of Oracle, Java, and dabble with various open source software packages.
February 10th, 2010 at 1:05 pm
Very interested in your scaffolding tweaks. Unfortunately the various pictures referenced in your blog are not showing properly. Perhaps something has gone wrong?
February 10th, 2010 at 1:47 pm
YEah – I upgraded my wordpress version, and lost the images in the process. I need to dig around and see if I can restore them. Sorry about that!