Grails Controllers
Within the MVC design pattern takes a controller which requests (requests) and forward the responses generated (Responses) and forwards the data (Model) to the View. The following should you know about Grails Controllers:
- Naming convention
- Creating Controllers
- Creating actions (Actions)
- Query parameters from the request and the session
- Benefits of Flash Scopes
- Processing of the data (Models)
- Generate the response (Response)
- Routing and chaining of actions (Actions)
Creating controllers / naming convention
Grails controllers are in the grails-app\controller . They end in the file name always as Controller User Controller. Groovy. The first part of the name is part of the URI (eg user), through which the actions are addressed.
Controller can also 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
1 2 3 4 5 6 | Controller class User ( def list = ( ! params. max ) params. max = 10 if (params. max) params. max = 10 User. list ( params ) ] [UserList: User. List (params)] ) ) |
it is a complete example in [1]
If only the controller in the URI brought up, so 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 are provided dynamically at runtime with values. These include:
- actionUri
- controllerUri
- actionName
- controller name
- flash
- grailsApplication
- grailsAttributes
- log
- params
- session
- request
- response
- ServletContext
If you now want the parameters from the request or the session access, so you access the features of params or session to:
"findBy" ] def findBy = params ["findBy"] "logged_user" ] def loggedUser = session ["logged_user"]
Benefits of Flash Scopes
With the Flash scope, it is possible an 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 is done very simply on the allocation of the parameters to the quality properties of each model.
params user. properties = params
Models are implemented in Grails as Maps. The easiest way to pass an Object Model to the View is the instance returned.
User ( ) def user = new User () params user. properties = params 'user' : user ] return ['user': user]
Generate the response (Response)
To view or chunks of code from the controller to render out a special, is the function of Grails render made available.
'edit' ,model : [ user : user ] ) render (view: 'edit', model: [user: user])
To view or chunks of code from the controller to render out a special, is the function of Grails render made available.
Routing and chaining of actions (Actions)
The forwarding to another action is via the function redirect . The forwarding this optional parameter can be added. On actions of other controllers is made on a string.
/ / Simple forwarding list ) redirect (action: list) / / Forwarding, with parameters show,id : user. id ) redirect (action: show, id: user. id) / / Redirect to another controller "/address/list" ) redirect (action: "/ address / list")
Actions can of course also be linked, 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.
1 2 3 4 5 6 7 8 9 10 11 | class WizzardsController ( def = (FirstStep secondStep,model : [ "step1" : new Object ( ) ] ) chain (action: secondStep, model: ["step1": new Object ()]) ) def = (secondStep thirdStep,model : [ "step2" : new Object ( ) ] ) chain (action: thirdstep, model: ["step2": new Object ()]) ); def = (thirdstep "step3" : new Object ( ) ] ) return ["step3": new Object ()]) ); ) |




August 31st, 2008 at 1:37 pm
[...] The current version of the contribution I have to improve the maintainability as part of a small tutorial on Grails [...]