2. Groovy as Java
Syntactically very close to Java
Groovy files fully parsed and classes are generated
on the fly, via a custom class loader.
Can be compiled and fully integrated with
traditional Java application.
Language level support for lists, maps, regular
expressions.
Supports closures, dynamic typing, meta object
protocol.
3. Controllers
A controller handles requests and creates or prepares the
response.
A controller can generate the response directly or delegate to a
view.
Destination folder: grails-app/controllers directory
4. Create a controller
Command : grails create-controller book
package myapp
class BookController {
def index() { }
}
6. Closure vs controller
Earlier versions of Grails actions were implemented with Closures. This is
still supported, but the preferred approach is to use methods.
Leveraging methods instead of Closure properties has some advantages:
Memory efficient
Allow use of stateless controllers (singleton scope)
You can override actions from subclasses and call the overridden superclass
method with super.actionName()
7. Default action rules
If there is only one action, it's the default
If you have an action named index, it's the default
Alternatively you can set it explicitly with the defaultAction property:
static defaultAction = "list"
8. Scopes with controller
If there is only one action, it's the default
If you have an action named index, it's the default
Alternatively you can set it explicitly with the defaultAction property:
static defaultAction = "list"
9. Scopes available in controllers
servletContext - Also known as application scope, this scope lets you share state
across the entire web application. The servletContext is an instance of
ServletContext
session - The session allows associating state with a given user and typically uses
cookies to associate a session with a client. The session object is an instance of
HttpSession
request - The request object allows the storage of objects for the current request
only. The request object is an instance of HttpServletRequest
params - Mutable map of incoming request query string or POST parameters
flash - Messages
10. Redirect with controller
// Call the login action within the same class
redirect(action: login)
// Also redirects to the index action in the home controller
redirect(controller: 'home', action: 'index')
// Redirect to an explicit URI
redirect(uri: "/login.html")
// Redirect to a URL
redirect(url: "http://grails.org")
redirect(action: 'myaction', params: [myparam: "myvalue"])
11. Render with controller
Object for example:
def theShining = new Book(title: 'The Shining', author: 'Stephen King')
render(view: "viewName", model: [book: theShining])
render(template: "book", model: [book: theShining])
12. @ annotation with controller
package sample
import grails.plugins.springsecurity.Secured
class ScreenController {
def publicPage() {
render "This is a public page"
}
@Secured(['ROLE_COMMON'])
def commonPage() {
render "This is a common role page"
}
}
commonPage - only user with ROLE_COMMON role can access this page.
13. URLs
The first part of the URL is the application name, or Application Context Root. The next part of
the URL is the controller name.
Notice that the Controller part of UserController gets dropped off, and the first letter is changed
to lowercase.
After the controller name, you’ll see an optional action name. This name corresponds directly to
the action in the Controller.