(Quick Reference)

9 Traits

Version: 7.0.0-M1

9 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.

9.1 Traits Provided by Grails

Grails artefacts are automatically augmented with certain traits at compile time.

Domain Class Traits

  • {apiDocs}grails/artefact/DomainClass.html[grails.artefact.DomainClass]

  • {apiDocs}grails/web/databinding/WebDataBinding.html[grails.web.databinding.WebDataBinding]

  • {gormApiDocs}/org/grails/datastore/gorm/GormEntity.html[org.grails.datastore.gorm.GormEntity]

  • {gormApiDocs}/org/grails/datastore/gorm/GormValidateable.html[org.grails.datastore.gorm.GormValidateable]

Controller Traits

  • {gspApiDocs}grails/artefact/gsp/TagLibraryInvoker.html[grails.artefact.gsp.TagLibraryInvoker]

  • {asyncApiDocs}grails/artefact/AsyncController.html[grails.artefact.AsyncController]

  • {apiDocs}grails/artefact/controller/RestResponder.html[grails.artefact.controller.RestResponder]

  • {apiDocs}grails/artefact/Controller.html[grails.artefact.Controller]

Interceptor Trait

  • {apiDocs}grails/artefact/Interceptor.html[grails.artefact.Interceptor]

Tag Library Trait

  • {gspApiDocs}grails/artefact/TagLibrary.html[grails.artefact.TagLibrary]

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

{apiDocs}grails/web/api/WebAttributes.html[grails.web.api.WebAttributes]

Common Web Attributes

{apiDocs}grails/web/api/ServletAttributes.html[grails.web.api.ServletAttributes]

Servlet API Attributes

{apiDocs}grails/web/databinding/DataBinder.html[grails.web.databinding.DataBinder]

Data Binding API

{apiDocs}grails/artefact/controller/support/RequestForwarder.html[grails.artefact.controller.support.RequestForwarder]

Request Forwarding API

{apiDocs}grails/artefact/controller/support/ResponseRedirector.html[grails.artefact.controller.support.ResponseRedirector]

Response Redirecting API

{apiDocs}grails/artefact/controller/support/ResponseRenderer.html[grails.artefact.controller.support.ResponseRenderer]

Response Rendering API

{apiDocs}grails/validation/Validateable.html[grails.validation.Validateable]

Validation API

9.1.1 WebAttributes Trait Example

{apiDocs}grails/web/api/WebAttributes.html[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 demo

import grails.web.api.WebAttributes

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
    }
}

The traits are compatible with static compilation…​

src/main/groovy/demo/Helper.groovy
package demo

import 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
    }
}