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:
- Naming convention
- Creating controllers
- Creating actions (Actions)
- Query parameters from the request and the session
- Take advantage of flash scopes
- Processing of the data (models)
- Generate the response (response)
- 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 managed to controllers such as controller users. Groovy. The first part of the name is part of the URI (eg managed user), via which the Actions are 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 managedUser/list mentioned
class ManagedUserController { def list = { if(!params.max)params.max = 10 [ managedUserList: ManagedUser.list( params ) ] } } If only the controller in the URI addressed, then the "Default Action" is executed. This index can be set on the property.
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:
def findBy = params["findBy"] def loggedUser = 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.
flash.message = "ManagedUser ${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.
managedUser.properties = params Models are implemented in Grails as maps. Forward the simplest way a model Object to the view is, the instance returned.
def managedUser = new ManagedUser() managedUser.properties = params return ['managedUser':managedUser] 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.
render(view:'edit',model:[managedUser:managedUser]) 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.
// einfache Weiterleitung redirect(action:list) // Weiterleitung, mit Parametern redirect(action:show,id:managedUser.id) // Weiterleitung auf einen anderen Controller 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.
class WizzardsController { def firstStep = { chain(action:secondStep,model:["step1":new Object()]) } def secondStep = { chain(action:thirdStep,model:["step2":new Object()]) }; def thirdStep= { return ["step3":new Object()]) }; } Update: the latest version of the paper I have to improve maintainability as part of a small tutorial to Grails, and outsourced direct links in the right sidebar.




May 14th, 2007 at 4:32 pm
Hello Gero,
I miss this kind of overview of Grails as interessierterm ('precursor to entry'
on the Grails site. Would not it make sense to position them once there?
In addition, I'm still not clear what actually follows Grails innneren architecture. It looks to me like simple JSPs that come along just in the garb of the GSP. But such a collaboration would be possible with JSF (I do not mean of course a redirect or similar between these two concepts)?
December 3rd, 2009 at 4:36 pm
Hello,
I have a problem to pass variables to beforeInterceptor afterInterceptor within a controller.
How does it work?
Can someone help me there?
December 6th, 2009 at 12:00 pm
Unfortunately not. I use neither the nor the beforeInterceptor afterInterceptor. Instead, I use to hedge the security filter.
What do you want to implement because with this combination. Perhaps there is a possibility the other.
December 16th, 2009 at 12:58 pm
I would like to beforINterceptor domain instance to write to the database and then update afterinterceptor .. Statistics for a request ..
December 17th, 2009 at 9:23 am
If you have successfully written to the database, you can them in afterInterceptor again read from the database and then update course.
The performance is of course not as sparkling, but if the access to the record on an index, it should not be so tragic.