play2.0文档-面向java开发者(4)
博客专区 > DavidBao 的博客 > 博客详情
play2.0文档-面向java开发者(4)
DavidBao 发表于2年前
play2.0文档-面向java开发者(4)
  • 发表于 2年前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

Session and Flash scopes 

Session和Flash作用域


How it is different in Play

在play中有何不同


If you have to keep data across multiple HTTP requests, you can save them in the Session or the Flash scope. Data stored in the Session are available during the whole user session, and data stored in the flash scope are only available to the next request.

如果你需要在多个HTTP请求中保存数据,你可以把它们保存在Session或Flash域中。保存在Session中的数据,对于整个用户 session 都有效,而保存在Flash域中的数据只对下一次请求有效。


It’s important to understand that Session and Flash data are not stored in the server but are added to each subsequent HTTP Request, using Cookies. This means that the data size is very limited (up to 4 KB) and that you can only store string values.

必须要明白session和Flash域不是保存在server端的,而是用Cookies添加到后面的每个HTTP请求里。也就是说,数据最大只有4KB,而且只能存string类型。


Cookies are signed with a secret key so the client can’t modify the cookie data (or it will be invalidated). The Play session is not intended to be used as a cache. If you need to cache some data related to a specific session, you can use the Play built-in cache mechanism and use store a unique ID in the user session to associate the cached data with a specific user.

Cookie是被标记的,所以它在客户端不能被修改,否则就会失效。Play session不能用作缓存,如果你需要缓存与某个session相关的数据,你可以使用play内置的缓存机制,在session中保存一个唯一的用户session ID来关联缓存的数据与用户。

There is no technical timeout for the session, which expires when the user closes the web browser. If you need a functional timeout for a specific application, just store a timestamp into the user Session and use it however your application needs (e.g. for a maximum session duration, maxmimum inactivity duration, etc.).

session是没有超时的,当用户关闭浏览器的时候就会失效。如果你需要一个超时功能,只需要在用户的serssion中保存一个时间戳。(例如 session最大有效期,最大失效期等)

Reading a Session value

读取 Session 值

You can retrieve the incoming Session from the HTTP request:

你可以从HTTP请求中获取收到的session:

public static Result index() {

    String user = session("connected"); 

    if(user != null) { return ok("Hello " + user);

    } else {

        return unauthorized("Oops, you are not connected");

    }

}


Storing data into the Session

Session 中存储数据


As the Session is just a Cookie, it is also just an HTTP header, but Play provides a helper method to store a session value:

就像session仅仅是一个Cookie一样,它也仅仅是一个HTTP头,但是,play提供了一个便利方法来保存session值:

public static Result index() {

    session("connected", "user@gmail.com");

    return ok("Welcome!");

}


The same way, you can remove any value from the incoming session:

同样,你可以删除session中的任何值:

public static Result index() {

   session.remove("connected");

   return ok("Bye");

}


Discarding the whole session

丢弃整个session


If you want to discard the whole session, there is special operation:

如果你想丢弃整个session,可以这样实现:

public static Result index() {

    session().clear();

    return ok("Bye");

}


Flash scope

Flash 作用域


The Flash scope works exactly like the Session, but with two differences:

Flash域用法和Session差不多,但是它有两点不同:

  • data are kept for only one request
  • 数据只保存在一个请求里

  • the Flash cookie is not signed, making it possible for the user to modify it.
  • Flash cookie 没被标记,这使得用户可以修改它。

Important: The flash scope should only be used to transport success/error messages on simple non-Ajax applications. As the data are just kept for the next request and because there are no guarantees to ensure the request order in a complex Web application, the Flash scope is subject to race conditions.

重要:Flash只能用在简单的非Ajax应用中传递成功/错误消息。由于数据仅在下个请求中有效,而且无法在复杂的web应用中保证请求的顺序,因此Flash作用域会受到竞争条件的支配。

Here are a few examples using the Flash scope:

这是几个使用Flash域的例子:

public static Result index() {

    String message = flash("success");

    if(message == null) {

        message = "Welcome!";

    } 

    return ok(message);

} 

public static Result save() {

    flash("success", "The item has been created"); return redirect("/home");

}

共有 人打赏支持
粉丝 113
博文 264
码字总数 116640
×
DavidBao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: