@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
Type | Name and description |
---|---|
org.springframework.validation.Errors |
errors |
org.springframework.web.servlet.ModelAndView |
modelAndView |
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) |
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() |
Binds data for the given type to the given collection from the request
targetType
- The target typecollectionToPopulate
- The collection to populaterequest
- The requestReturns the URI of the currently executing action
Returns the URI of the currently executing controller
Obtains the errors instance for the current controller
Obtains the ModelAndView for the currently executing controller
Obtains a URI of a template by name
name
- The name of the templateObtains a URI of a view by name
name
- The name of the viewReturn true if there are an errors
Sets a response header for the given name and value
headerName
- The header nameheaderValue
- The header valueInitializes 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.
type
- The type of the command objectRedirects for the given arguments.
argMap
- The argumentsSets the errors instance of the current controller
errors
- The error instanceSets the ModelAndView of the current controller
mav
- The ModelAndViewUsed the synchronizer token pattern to avoid duplicate form submissions
callable
- The closure to execute
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
}
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}
}