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");
}