cookie plugin grails - cookie is null
with Grails' cookie plugin, which is correctly installed, I want to set a cookie:
response.setCookie("username", "cookiename", 1000)
this method "setCookie" is underlined, so in the IDE it means it could not be found, this version doesn't work either
def cookieService
cookieService.set(response, "username", "cookiename", 1000)
this line says, the cookie is null
println(cookieService.get("username"))
What did I miss? I was wondering it has something to do with that grails is running on a server and 开发者_如何学运维it performs a task on the client side (which usually is done by JS).
Dan: just add the following code to your config.groovy
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)
}
you are getting null coz of the path.. This will solve ur issue :)
精彩评论