Grails controllers

Within the MVC design pattern takes the controller requests (requests) and generates the opposite responses (responses) and forwards the data (model) to the view. The following should you know about Grails controller:

  1. Naming convention
  2. Creating controllers
  3. Creating actions (Actions)
  4. Query parameters from the request and the session
  5. Take advantage of flash scopes
  6. Processing of the data (models)
  7. Generate the response (response)
  8. Forwarding and chaining of actions (Actions)

Creating controllers / naming convention

Grails controllers are in the grails-app\controller . They end up in the file name always at the controller as UsersController. Groovy. The first part of the name is part of the URI (eg, user) through which the actions to be addressed.
Controllers can also use the command grails create-controller can be created.

Creating actions (Actions)

Actions (actions) are defined as properties of a controller and mapped to a URI. Thus the action of the controller's list for the user on the managed URI user/list mentioned

  A
 2
 3
 4
 5
 6
  class User {controller
     def list = {
         ! params. max ) params. max = 10 if (params. max) params. max = 10
         User. list ( params ) ] [Userlist: User list (params).]
     }
 } 

is a complete example can be found at [1]

If only the controller in the URI addressed, then the "Default Action" is executed. This index can be set on the property.

  redirect ( action : list,params : params ) } def index = {redirect (action: list, params: params)} 

Query parameters from the request and the session

Each controller has a number of predefined properties that can be supplied dynamically at run time with values. These include:

  • nuri actio
  • controllerUri
  • actionName
  • controllerName
  • flash
  • grailsApplication
  • grailsAttributes
  • log
  • params
  • session
  • request
  • response
  • ServletContext

If one wants to access the parameters from the request or the session, one accesses the properties params or session to:

  "findBy" ] findBy def = params ["findBy"]
 "logged_user" ] loggedUser def = session ["logged_user"] 

Take advantage of flash scopes

With Flash it is possible a scope attribute for the next request (request) to provide. This functionality is used to inform the user about the status of actions, such that the deletion was successful.

  "User ${params.id} deleted." flash. message = "$ {user} params.id deleted." 

Processing of the data (models)

The parameters in Web applications are passed as strings, so it is necessary to map them to the data objects (models). In Grails, this is done very simply by assigning the parameters to the characteristic properties of each model.

  params user. properties = params 

Models are implemented in Grails as maps. Forward the simplest way a model object to the view, the instance returned.

  User ( ) def user = new User ()
 params user. properties = params
 'user' : user ] return ['user': user] 

Generate the response (response)

To render a specific view or pieces of code from the controller out of Grails is the function render made ​​available.

  'edit' ,model : [ user : user ] ) render (view: 'edit', model: [user: user]) 

To render a specific view or pieces of code from the controller out of Grails is the function render made ​​available.

Forwarding and chaining of actions (Actions)

Forwarding to another action takes place via the redirect . The optional parameters Forwarding Web can thereby be added. In other action controller is referenced by a string.

  / / Simple forwarding
 list ) redirect (action: list)
 / / Next line, with parameters
 show,id : user. id ) redirect (action: show, id: user id.)
 / / Redirect to another controller
 "/address/list" ) redirect (action: '/ address / list ") 

Actions can also be chained, of course, is given to the model objects that are added during the concatenation, and other actions of the chain. This is useful, for example in the implementation of wizards.

  A
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
  class {WizzardsController
     def first step = {
         secondStep,model : [ "step1" : new Object ( ) ] ) chain (action: second step, model: ["step1": new Object ()])
     }
     def second step = {
         thirdStep,model : [ "step2" : new Object ( ) ] ) chain (action: third step, model: ["step2": new Object ()])
     ;}
     def third = {step
         "step3" : new Object ( ) ] ) return ["step3": new Object ()])
     ;}
 } 

[1] example of a controller

One Response to "Grails controllers"

  1. Grails Controllers »all-IT Says:

    [...] The current version of the article I have for better maintainability as part of a small tutorial on Grails [...]

Add a comment

Yes, I would like to be notified about comments!