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.web
若是你须要在多个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类型。cookie
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.session
Cookie是被标记的,因此它在客户端不能被修改,不然就会失效。Play session不能用做缓存,若是你须要缓存与某个session相关的数据,你可使用play内置的缓存机制,在session中保存一个惟一的用户session ID来关联缓存的数据与用户。app
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.).ide
session是没有超时的,当用户关闭浏览器的时候就会失效。若是你须要一个超时功能,只须要在用户的serssion中保存一个时间戳。(例如 session最大有效期,最大失效期等)ui
You can retrieve the incoming Session from the HTTP request:spa
你能够从HTTP请求中获取收到的session:code
public static Result index() {
String user = session("connected");
if(user != null) { return ok("Hello " + user);
} else {
return unauthorized("Oops, you are not connected");
}
}
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");
}
If you want to discard the whole session, there is special operation:
若是你想丢弃整个session,能够这样实现:
public static Result index() {
session().clear();
return ok("Bye");
}
The Flash scope works exactly like the Session, but with two differences:
Flash域用法和Session差很少,可是它有两点不一样:
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");
}