Search code examples
grailscookiesgsp

Cookie Retrieval in GSP Not Working


Grails 2.1.1

In my controller:

def someAction() {
   def projectVersionInstance = ProjectVersion.get(params.id)
   Cookie cookie = new Cookie("lastProjectVersion","${projectVersionInstance.id}")
   response.addCookie(cookie)
   .....
}

In my GSP:

<g:if test="${cookie(name: 'lastProjectVersion')}">
   ....
</g:if>

The content inside the g:if tag isn't showing up. I even just tried the following:

<g:cookie name="lastProjectVersion" />

And I get nothing. I can display it in the controller and I can see it in the browser cookie management.


Solution

  • I had the problem too and it come from a redirect at the end of the method in controller.

    When I used it, I lost all the cookies I set, but not the JSESSIONID one.

    After googling, I found these help (http://www.zugiart.com/2011/04/http-redirect-and-cookies/).

    When defining your cookie, set the path to '/' : cookie.path = '/'.

    Now, you can redirect too.