Gregg
Gregg

Reputation: 35864

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.

Upvotes: 0

Views: 983

Answers (2)

proutt
proutt

Reputation: 36

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.

Upvotes: 2

Fabiano Taioli
Fabiano Taioli

Reputation: 5540

I think the cookie is not available to the cookie method "cookie()" until the next http call. Why not you simply pass the cookie value from controller to gsp with a model var?

Upvotes: 0

Related Questions