Last updated by bronoman 2 months ago
- Edit
- View Info
Artifact and Scaffolding Templates (since 0.4)
When creating artifacts or using scaffolding, Grails uses templates to create the domain classes, controllers, views etc. The default templates are part of the Grails distribution for out of the box behaviour, but can be customized for project specific needs.Scenario
Imagine you are using Action Interceptors for checking if an user is authenticated. In this case, (almost) all controllers in your application need a before interceptor to do the authentication check. The easiest way to do this is to create a SecuredBaseController containing the authentication interceptorclass SecuredBaseController { def beforeInterceptor = [action:this.&auth] def auth() { if(!session.user) { redirect(controller:'authentication',action:'login') return false } } }
Solution
As mentioned earlier, Grails uses templates for creating artifacts and scaffolding. The default template for creating new controllers looks like:class @artifact.name@Controller { def index = { } }
artifact.name
will be replaced by the name you specified for the new controller.To secure all newly created controllers by default, we need to extend the SecuredBaseController in the template:
class @artifact.name@Controller extends SecuredBaseController {
def index = { }
}
Customizing templates
To customize the templates for you project you need to the install the templates:grails install-templates