Login required
Download

Cookie Plugin

(0)
Author(s) dalew75
Current Release 0.2   (2 years ago)
Grails Version 1.1.1 > *
Tags
Dependency
compile ":cookie:0.2"
Last updated by admin 2 years ago
grails install-plugin cookie
Last updated by dalew75 2 years ago

Cookie Plug-in

The cookie plug-in extends the request and response objects found in controllers and filters by adding methods to easily set, get, and delete cookies. It also provides access to these methods via a service, CookieService.

Installation

To install the cookie plug-in just run the following command

grails install-plugin cookie

Usage

The cookie plug-in extends the request and response objects found in controllers, filters, etc to allow the following:

Example of setting a new cookie: (this sets a cookie with the name "username" to the value "cookieUser123" with a expiration set to a week, defined in seconds)

response.setCookie("username","cookieUser123",604800)

To get the cookie value:

request.getCookie("username") // returns "cookieUser123"

To delete the cookie:

response.deleteCookie("username") // deletes the "username" cookie

Usage by calling CookieService directly

The cookie plug-in provides a CookieService that can be used anywhere in your Grails application.

To use CookieService, declare a dependency injection:

def cookieService

Setting a new cookie:

cookieService.set(response,"username","cookieUser123",604800)

Getting the cookie value:

cookieService.get("username") // returns "cookieUser123"

Deleting the cookie:

cookieService.delete(response,"username") // deletes the "username" cookie

Tag Lib

You can also get a cookie value with a tag:

<cookie:get name='username' />

Plugin version history

*2009-08-010 0.1 Added methods to request and response objects to get, set, and delete cookies

*2009-08-07 0.1 Initial release

TODO

  • Investigate if there's a way to do the set and delete methods from CookieService without having to pass in the response object
Last updated by esumerfd 11 months ago
Q: How do I set the cookie path.

A: This is not supported in the plugin at the moment but here is a workaround, Add the following to your Config.groovy:

/*
 * Fix the grails cookie plugin allowing the cookie path to be set.
 */
com.studentuniverse.grails.plugins.cookie.services.CookieService.metaClass.setCookie = { response, name, value, maxAge ->
  def cookie = new javax.servlet.http.Cookie(name, value)
  cookie.setMaxAge(maxAge)
  cookie.setPath("/")
  response.addCookie(cookie)
}
Last updated by admin 2 years ago