2011-12-15 2 views
17

Grails 2 (RC3) kullanarak bir çerez oluşturmaya çalışıyorum. Bunu bir Facebook tuval uygulaması için kullanıyorum. Bu, her tarayıcı yenilendiğinde oturumun kaybolduğu anlamına gelir.Grails 2.0'da Çerezleri nasıl oluşturabilir ve alabilirim?

Kurabiye eklentisi kullanarak denedi, ama buna Herhangi bir yardım büyük takdir Grails 2.

ile uyumlu olduğunu görünüyor!

Sen de bir kumandadan gelen bu etiketi kullanabilirsiniz

Hello <g:cookie name="myCookie" /> 

<g:cookie> etiketi kullanarak GSP bir çerez değeri alabilir

cevap

31

:

String name = g.cookie(name: 'myCookie') 

kullanarak bir çerez ayarlayabilirsiniz Servlet API

Cookie cookie = new Cookie("myCookie","Cookie Monster") 
cookie.maxAge = 100 
response.addCookie(cookie) 
+0

Awesome. Çok teşekkür ederim. İsme göre bir çerez almak için bir yol var mı yoksa request.getCookies() almak ve sonra oradan ayrıştırmak zorunda mıyım? – cavneb

+5

Bunun cevabını buldum: 'def signedRequestCookie = request.cookies.find {it.name == 'signed_request'}' ... Tekrar teşekkürler! – cavneb

+1

Yukarıdaki örnekte, "g.cookie" adı (ad: 'myCookie') ' –

3

Sen Cookie Plugin kullanabilirsiniz:

// Inject service 
def cookieService 
... 
// This sets a cookie with the name `username` to the value `admin`  with a expiration set to a week, defined in seconds 
cookieService.setCookie('username', 'admin', 7 * 24 * 60) 
cookieService.getCookie('username') // returns 'admin' 
cookieService.deleteCookie('username')