Controller (Grails 5.2.3)

Package: grails.artefact

## [Groovy] Trait Controller

- grails.artefact.Controller

- All Implemented Interfaces and Traits:
    DataBinder, RequestForwarder, ResponseRedirector, ResponseRenderer, ServletAttributes, WebAttributes

      @groovy.transform.Trait
      @groovy.transform.CompileStatic
      trait Controller
      extends java.lang.Object
      implements DataBinder, RequestForwarder, ResponseRedirector, ResponseRenderer, ServletAttributes, WebAttributes

  Classes that implement the Controller trait are automatically treated as web controllers in a Grails application

  Authors:
    Jeff Brown
    Graeme Rocher
  Since:
    3.0

- - ### Properties Summary

    Properties
    Type | Name and description
    --- | ---
    `org.springframework.validation.Errors` | `errors`
    `org.springframework.web.servlet.ModelAndView` | `modelAndView`

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `void` | `bindData(java.lang.Class targetType, java.util.Collection collectionToPopulate, javax.servlet.ServletRequest request)` Binds data for the given type to the given collection from the request
    `` | `java.lang.String` | `getActionUri()` Returns the URI of the currently executing action
    `` | `java.lang.String` | `getControllerUri()` Returns the URI of the currently executing controller
    `` | `org.springframework.validation.Errors` | `getErrors()` Obtains the errors instance for the current controller
    `` | `java.lang.reflect.Method` | `getExceptionHandlerMethodFor(java.lang.Class<? extends java.lang.Exception> exceptionType)`
    `` | `org.springframework.web.servlet.ModelAndView` | `getModelAndView()` Obtains the ModelAndView for the currently executing controller
    `` | `static org.springframework.context.ApplicationContext` | `getStaticApplicationContext()`
    `` | `java.lang.String` | `getTemplateUri(java.lang.String name)` Obtains a URI of a template by name
    `` | `java.lang.String` | `getViewUri(java.lang.String name)` Obtains a URI of a view by name
    `` | `boolean` | `hasErrors()` Return true if there are an errors
    `` | `void` | `header(java.lang.String headerName, java.lang.Object headerValue)` Sets a response header for the given name and value
    `` | `java.lang.Object` | `initializeCommandObject(java.lang.Class type, java.lang.String commandObjectParameterName)` Initializes a command object.
    `` | `void` | `redirect(java.util.Map argMap)` Redirects for the given arguments.
    `` | `void` | `setErrors(org.springframework.validation.Errors errors)` Sets the errors instance of the current controller
    `` | `void` | `setModelAndView(org.springframework.web.servlet.ModelAndView mav)` Sets the ModelAndView of the current controller
    `` | `TokenResponseHandler` | `withForm(groovy.lang.Closure callable)` Used the synchronizer token pattern to avoid duplicate form submissions
    `` | `TokenResponseHandler` | `withForm(GrailsWebRequest webRequest, groovy.lang.Closure callable)`
    `` | `java.lang.Object` | `withFormat(groovy.lang.Closure callable)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class java.lang.Object` | `java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()`

- - ### Property Detail

    - #### org.springframework.validation.Errors errors

    - #### org.springframework.web.servlet.ModelAndView modelAndView

  - ### Method Detail

    - #### @groovy.transform.Generated void bindData(java.lang.Class targetType, java.util.Collection collectionToPopulate, javax.servlet.ServletRequest request)

      Binds data for the given type to the given collection from the request

      Parameters:
        `targetType` - The target type
        `collectionToPopulate` - The collection to populate
        `request` - The request

    - #### @groovy.transform.Generated java.lang.String getActionUri()

      Returns the URI of the currently executing action

      Returns:
        The action URI

    - #### @groovy.transform.Generated java.lang.String getControllerUri()

      Returns the URI of the currently executing controller

      Returns:
        The controller URI

    - #### @groovy.transform.Generated org.springframework.validation.Errors getErrors()

      Obtains the errors instance for the current controller

      Returns:
        The Errors instance

    - #### @groovy.transform.Generated @java.lang.SuppressWarnings(value: unchecked) java.lang.reflect.Method getExceptionHandlerMethodFor(java.lang.Class<? extends java.lang.Exception> exceptionType)

    - #### @groovy.transform.Generated org.springframework.web.servlet.ModelAndView getModelAndView()

      Obtains the ModelAndView for the currently executing controller

      Returns:
        The ModelAndView

    - #### @groovy.transform.Generated static org.springframework.context.ApplicationContext getStaticApplicationContext()

    - #### @groovy.transform.Generated java.lang.String getTemplateUri(java.lang.String name)

      Obtains a URI of a template by name

      Parameters:
        `name` - The name of the template
      Returns:
        The template URI

    - #### @groovy.transform.Generated java.lang.String getViewUri(java.lang.String name)

      Obtains a URI of a view by name

      Parameters:
        `name` - The name of the view
      Returns:
        The template URI

    - #### @groovy.transform.Generated boolean hasErrors()

      Return true if there are an errors

      Returns:
        true if there are errors

    - #### @groovy.transform.Generated void header(java.lang.String headerName, java.lang.Object headerValue)

      Sets a response header for the given name and value

      Parameters:
        `headerName` - The header name
        `headerValue` - The header value

    - #### @groovy.transform.Generated java.lang.Object initializeCommandObject(java.lang.Class type, java.lang.String commandObjectParameterName)

      Initializes a command object. If type is a domain class and the request body or parameters include an id, the id is used to retrieve the command object instance from the database, otherwise the no-arg constructor on type is invoke. If an attempt is made to retrieve the command object instance from the database and no corresponding record is found, null is returned. The command object is then subjected to data binding and dependency injection before being returned.

      Parameters:
        `type` - The type of the command object
      Returns:
        the initialized command object or null if the command object is a domain class, the body or parameters included an id and no corresponding record was found in the database.

    - #### @groovy.transform.Generated void redirect(java.util.Map argMap)

      Redirects for the given arguments.

      Parameters:
        `argMap` - The arguments
      Returns:
        null

    - #### @groovy.transform.Generated void setErrors(org.springframework.validation.Errors errors)

      Sets the errors instance of the current controller

      Parameters:
        `errors` - The error instance

    - #### @groovy.transform.Generated void setModelAndView(org.springframework.web.servlet.ModelAndView mav)

      Sets the ModelAndView of the current controller

      Parameters:
        `mav` - The ModelAndView

    - #### @groovy.transform.Generated TokenResponseHandler withForm(groovy.lang.Closure callable)

      Used the synchronizer token pattern to avoid duplicate form submissions

      Parameters:
        `callable` - The closure to execute
      Returns:
        The result of the closure execution

    - #### @groovy.transform.Generated TokenResponseHandler withForm(GrailsWebRequest webRequest, groovy.lang.Closure callable)

      Main entry point, this method will check the request for the necessary TOKEN and if it is valid will call the passed closure.

      For an invalid response an InvalidResponseHandler is returned which will invoke the closure passed to the handleInvalid method. The idea here is to allow code like:

           withForm {
             // handle valid form submission
           }.invalidToken {
              // handle invalid form submission
           }

    - #### @groovy.transform.Generated java.lang.Object withFormat(groovy.lang.Closure callable)

      The withFormat method is used to allow controllers to handle different types of request formats such as HTML, XML and so on. Example usage:

              withFormat {
                  html { render "html" }
                  xml { render "xml}
              }

      Parameters:
        callable
      Returns:
        The result of the closure execution selected