Package: grails.artefact

## [Groovy] Trait Controller

- grails.artefact.Controller

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

      @groovy.transform.CompileStatic
      trait Controller

  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)`

- - ### Property Detail

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

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

  - ### Method Detail

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

    - #### java.lang.String getActionUri()

      Returns the URI of the currently executing action

      Returns:
        The action URI

    - #### java.lang.String getControllerUri()

      Returns the URI of the currently executing controller

      Returns:
        The controller URI

    - #### org.springframework.validation.Errors getErrors()

      Obtains the errors instance for the current controller

      Returns:
        The Errors instance

    - #### @java.lang.SuppressWarnings("unchecked") 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

      Returns:
        The ModelAndView

    - #### static org.springframework.context.ApplicationContext getStaticApplicationContext()

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

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

    - #### boolean hasErrors()

      Return true if there are an errors

      Returns:
        true if there are errors

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

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

    - #### void redirect(java.util.Map argMap)

      Redirects for the given arguments.

      Parameters:
        `argMap` - The arguments
      Returns:
        null

    - #### void setErrors(org.springframework.validation.Errors errors)

      Sets the errors instance of the current controller

      Parameters:
        `errors` - The error instance

    - #### void setModelAndView(org.springframework.web.servlet.ModelAndView mav)

      Sets the ModelAndView of the current controller

      Parameters:
        `mav` - The ModelAndView

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

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

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