Controllers

Grails Controllers

What is a controller?

A controller handles requests and creates or prepares the response. They can generate the response or delegate to a view. To create a controller simply create a class whose name ends with "Controller" and place it within the "grails-app/controllers" directory.

The first part of your controller name is mapped to a URI and each action defined within your controller maps to URI within the controller name URI.

Controllers are request-scoped.  A new instance is created for each request. 

Further Reading

  1. Creating Controllers and Actions
  2. Controllers Scopes
  3. Data Binding
  4. Models & Rendering Views
  5. Redirects & Chaining
  6. Interceptors
  7. File Uploads
  8. Dependency Injection
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Oct 07, 2007

    Burkard Endres says:

    i tried to write a simple controller to try out grails a little, but i could not...

    i tried to write a simple controller to try out grails a little, but i could not manage to set an attribute to a session.

    i tried

    this.session["books",Book.findAll()]

    and this.session["books"] = Book.findAll()

    what i found similar in some example. Could you please provide a proper example how Attributes should be set to request, session and application scope?

    thanks

  2. Feb 06, 2006

    Burkard Endres says:

    this.session.books = Book.findAll()] this.session["books"] = Book.findAll()] s...

    this.session.books = Book.findAll()]

    this.session["books"] = Book.findAll()]

    should work 

  3. Feb 06, 2006

    Burkard Endres says:

    of course this.session.books = Book.findAll() this.session["books"] = Book.fin...

    of course

    this.session.books = Book.findAll()

    this.session["books"] = Book.findAll()

    should work

     

  4. Jul 09, 2007

    Kristian Mandrup says:

    You often need the ability to attach multiple filesto a domain object. One way ...

    You often need the ability to attach multiple filesto a domain object.

    One way to do this would be to create an Attachment domain class with a byte[] file property and some metadata properties such as date, name, description tags etc. Then have your "core" domain object contain a static hasMany  = [attachments:Attachment, ...]

    Would be nice to have a simple way to bind such Attachments these to a form supporting multiple file uploads.

    Fx pseudo-code

    $

    Unknown macro: {for each attachment in attachments}

    <input type="file" name="$

    Unknown macro: {attachment.file}
    ...>

    $

    Unknown macro: {end for}
     

    The following blog http://xytang.blogspot.com/2007/03/web-form-based-pdfs-merger-task-provide.html&nbsp;

    contains an example to start from. Would be nice to generalize this for easier reuse! Any ideas? Maybe already supported using some Ajax magic/widgets?

    Cheers!