【问题标题】:How do I create and retrieve Cookies in Grails 2.0?如何在 Grails 2.0 中创建和检索 Cookie?
【发布时间】:2012-01-21 09:02:43
【问题描述】:

我正在尝试使用 Grails 2 (RC3) 创建一个 cookie。我将它用于 Facebook 画布应用程序,这意味着每次浏览器刷新时,会话都会丢失。

我尝试过使用 cookies 插件,但它似乎与 Grails 2 不兼容。

任何帮助将不胜感激!

【问题讨论】:

    标签: grails cookies


    【解决方案1】:

    您可以使用<g:cookie> 标签检索 GSP 中 cookie 的值

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

    你也可以在控制器中使用这个标签:

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

    您可以使用 Servlet API 设置 cookie

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

    【讨论】:

    【解决方案2】:

    你可以使用Cookie Plugin:

    // 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')
    

    【讨论】:

      猜你喜欢
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 2014-04-20
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      相关资源
      最近更新 更多