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

  1. package demo
  2. import grails.web.api.WebAttributes
  3. class Helper implements WebAttributes {
  4. List<String> getControllerNames() {
  5. // There is no need to pass grailsApplication as an argument
  6. // or otherwise inject the grailsApplication property. The
  7. // WebAttributes trait provides access to grailsApplication.
  8. grailsApplication.getArtefacts('Controller')*.name
  9. }
  10. }

The traits are compatible with static compilation…​

src/main/groovy/demo/Helper.groovy

  1. package demo
  2. import grails.web.api.WebAttributes
  3. import groovy.transform.CompileStatic
  4. @CompileStatic
  5. class Helper implements WebAttributes {
  6. List<String> getControllerNames() {
  7. // There is no need to pass grailsApplication as an argument
  8. // or otherwise inject the grailsApplication property. The
  9. // WebAttributes trait provides access to grailsApplication.
  10. grailsApplication.getArtefacts('Controller')*.name
  11. }
  12. }