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

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `private MimeTypesApiSupport` | `mimeTypesSupport` |

  - ### 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
    `` | `private DataBindingSource` | `getCommandObjectBindingSourceForPrefix(java.lang.String prefix, DataBindingSource params)` Return a DataBindingSource for a command object which has a parameter name matching the specified prefix.
    `` | `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
    `` | `private org.springframework.validation.Errors` | `getErrorsInternal(GrailsWebRequest webRequest)`
    `` | `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.
    `` | `private boolean` | `isTokenValid(GrailsWebRequest webRequest)` Checks whether the token in th request is valid.
    `` | `void` | `redirect(java.util.Map argMap)` Redirects for the given arguments.
    `` | `private java.lang.Object` | `resetToken(GrailsWebRequest webRequest)` Resets the token in the request
    `` | `void` | `setErrors(org.springframework.validation.Errors errors)` Sets the errors instance of the current controller
    `` | `private java.lang.Object` | `setErrorsInternal(GrailsWebRequest webRequest, org.springframework.validation.Errors errors)`
    `` | `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), java.lang.Object#wait(long, int), 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()`

- - ### Field Detail

    - #### private MimeTypesApiSupport mimeTypesSupport

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

    - #### private DataBindingSource getCommandObjectBindingSourceForPrefix(java.lang.String prefix, DataBindingSource params)

      Return a DataBindingSource for a command object which has a parameter name matching the specified prefix. If params include something like widget.name=Thing and prefix is widget then the returned binding source will include name=thing, not widget.name=Thing.

      Parameters:
        `prefix` - The parameter name for the command object
        `params` - The original binding source associated with the request
      Returns:
        The binding source suitable for binding to a command object with a parameter name matching the specified prefix.

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

    - #### private org.springframework.validation.Errors getErrorsInternal(GrailsWebRequest webRequest)

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

    - #### private boolean isTokenValid(GrailsWebRequest webRequest)

      Checks whether the token in th request is valid.

      Parameters:
        `request` - The servlet request

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

      Redirects for the given arguments.

      Parameters:
        `argMap` - The arguments
      Returns:
        null

    - #### private java.lang.Object resetToken(GrailsWebRequest webRequest)

      Resets the token in the request

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

      Sets the errors instance of the current controller

      Parameters:
        `errors` - The error instance

    - #### private java.lang.Object setErrorsInternal(GrailsWebRequest webRequest, org.springframework.validation.Errors errors)

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