如何使用 JAVA 获取 sessionStorage 的句柄

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27555069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:58:58  来源:igfitidea点击:

How to get a handle to the sessionStorage using JAVA

javaxpagessession-storage

提问by Bill F

I'm looking at a situation that requires a fair bit of data storage that I have considered using sessionScope variables for. In a small test that I ran this worked well, but the data does get written back to the server with each change to the data. I'm considering creating a JAVA bean to update and control the structure of this data and store it in a series of sessionStorage variables. The sessionStorage is easy in CSJS but I'm not sure how to access these variables from a JAVA Bean. The application is something like a shopping cart so I would maintain the data in the sessionStorage until the document submit, then load the values into the document and save it and I think this would work best as a JAVA.

我正在考虑使用 sessionScope 变量需要大量数据存储的情况。在我运行的一个小测试中,它运行良好,但数据确实会随着数据的每次更改而写回服务器。我正在考虑创建一个 JAVA bean 来更新和控制此数据的结构并将其存储在一系列 sessionStorage 变量中。sessionStorage 在 CSJS 中很容易,但我不确定如何从 JAVA Bean 访问这些变量。该应用程序类似于购物车,因此我将在 sessionStorage 中维护数据,直到文档提交,然后将值加载到文档中并保存它,我认为这最适合作为 JAVA。

回答by Knut Herrmann

The sessionStorageobject stores the data for one session in browser window/tab on client side.

所述的sessionStorage对象存储的数据在浏览器窗口/标签一个会话在客户端

There is no way to access the sessionStorage from server with Java.

无法使用 Java 从服务器访问 sessionStorage。

You can though send the data to server and there you can pick it up with Java.

您可以将数据发送到服务器,然后您可以在那里使用 Java 获取它。

You can send it per Ajax or you can submit it in hidden fields.

您可以按 Ajax 发送,也可以在隐藏字段中提交。

If you store a complex data structure with objects and arrays in sessionStorage then you might convert it to JSON as an easy to use transport medium.

如果您在 sessionStorage 中存储具有对象和数组的复杂数据结构,那么您可以将其转换为 JSON 作为一种易于使用的传输介质。

You would convert the sessionStorage to a JSON string on client side with

您可以在客户端将 sessionStorage 转换为 JSON 字符串

JSON.stringify(sessionStorage)

You can parse the JSON string with Java on server side with classes from com.ibm.commons.util.io.jsonpackage

您可以使用com.ibm.commons.util.io.json包中的类在服务器端使用 Java 解析 JSON 字符串

JsonJavaFactory factory = JsonJavaFactory.instanceEx;
json = (JsonJavaObject) JsonParser.fromJson(factory, jsonString);

回答by Fredrik Norling

This will get the sessionScope not the localStorage, webstorage, sessionstorage locally in the browser

这将在浏览器中本地获取 sessionScope 而不是 localStorage、webstorage、sessionstorage

But if you want sessionScope this is how you do it Import these two classes

但是如果你想要 sessionScope 这就是你做的方式导入这两个类

import java.util.Map;
import javax.faces.context.FacesContext;

And then you can get it using this code

然后您可以使用此代码获取它

FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map sessionScope = externalContext.getSessionMap();