8 Traits - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari
Version: 3.0.17
8 Traits
Overview
Grails provides a number of traits which provide access to properties and behavior that may be accessed from various Grails artefacts as well as arbitrary Groovy classes which are part of a Grails project. Many of these traits are automatically added to Grails artefact classes (like controllers and taglibs, for example) and are easy to add to other classes.8.1 Traits Provided by Grails
Grails artefacts are automatically augmented with certain traits at compile time.Domain Class Traits
- grails.artefact.DomainClass
- grails.web.databinding.WebDataBinding
- org.grails.datastore.gorm.GormEntity
- org.grails.datastore.gorm.GormValidateable
Controller Traits
- grails.artefact.gsp.TagLibraryInvoker
- grails.artefact.AsyncController
- grails.artefact.controller.RestResponder
- grails.artefact.Controller
Interceptor Trait
Tag Library Trait
Service Trait
Below is a list of other traits provided by the framework. The javadocs provide more detail about methods and properties related to each trait.Trait | Brief Description |
---|---|
grails.web.api.WebAttributes | Common Web Attributes |
grails.web.api.ServletAttributes | Servlet API Attributes |
grails.web.databinding.DataBinder | Data Binding API |
grails.artefact.controller.support.RequestForwarder | Request Forwarding API |
grails.artefact.controller.support.ResponseRedirector | Response Redirecting API |
grails.artefact.controller.support.ResponseRenderer | Response Rendering API |
grails.validation.Validateable | Validation API |
8.1.1 WebAttributes Trait Example
WebAttributes is one of the traits provided by the framework. Any Groovy class may implement this trait to inherit all of the properties and behaviors provided by the trait.// src/main/groovy/demo/Helper.groovy package demoimport grails.web.api.WebAttributesclass Helper implements WebAttributes { List<String> getControllerNames() { // There is no need to pass grailsApplication as an argument // or otherwise inject the grailsApplication property. The // WebAttributes trait provides access to grailsApplication. grailsApplication.getArtefacts('Controller')*.name } }
// src/main/groovy/demo/Helper.groovy package demoimport grails.web.api.WebAttributes import groovy.transform.CompileStatic@CompileStatic class Helper implements WebAttributes { List<String> getControllerNames() { // There is no need to pass grailsApplication as an argument // or otherwise inject the grailsApplication property. The // WebAttributes trait provides access to grailsApplication. grailsApplication.getArtefacts('Controller')*.name } }