如何将 JSON 对象添加到 Java 中的作用域变量? [英] How can I add a JSON object to a scoped variable in Java?

查看:31
本文介绍了如何将 JSON 对象添加到 Java 中的作用域变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 applicationScope、sessionScope 和 viewScope 中使用了许多 JSON 对象来跟踪相关数据.在 SSJS 中编写和读取这些非常简单:`

I have used many JSON object in applicationScope, sessionScope, and viewScope to track related data. Writing and reading these in SSJS is very simple:`

//Create a app scope variable
applicationScope.put("myvarname", {p1:"part 1", p2:"part2"});

// read and use the app scope variable ...
var myvar = applicationScope.get("myvarname");
//Work with parts as myvar.p1, myvar.p2, etc...

在我编写的 Java 代码中,我学会了使用 com.ibm.jscript.std.ObjectObject 包使用 SSJS 编写的这些变量,其代码如下:

In the Java code I have been writing I have learned to read these variables which were written using SSJS using the com.ibm.jscript.std.ObjectObject package with code like this:

ObjectObject myvar = (ObjectObject) ExtLibUtil
        .getApplicationScope().get(dbkey);
FBSValue localFBS = myvar.get("p1");
String myp1 = localFBS.stringValue();
localFBS = myvar.get("p2");
String myp2 = localFBS.stringValue();

现在,当然,我想使用 Java Bean 编写一个新条目,然后 SSJS 和其他 Java Bean 可以以相同的方式读取该条目.我设法使用 Map 和 Hashtable 写入范围,但是当尝试使用 ObjectObject 读取时,这些会导致逻辑崩溃.

Now, of course, I want to write a new entry using the Java Bean that can then be read by SSJS and other Java Beans in the same manner. I managed to write to the scope using a Map and a Hashtable, but these crash the logic when trying to read using the ObjectObject.

那么,我将如何使用 ObjectObject 和/或 FBSValue 包在范围内构建新条目?我找不到如何创建一个新的 FBSValue,然后可以将其添加到 ObjectObject.我相信像我这样的新人错过了一件简单的事情.

So, how would I go about building a new entry in the scope using the ObjectObject and/or FBSValue packages? I cannot find how to create a new FBSValue that can then be added to an ObjectObject. I am sure it is a simple thing a Newbs like me has missed.

/纽布斯

推荐答案

你可以构造一个空的 ObjectObject,用 FBSValues 填充它,然后直接把它放到作用域 Map 中:

You can construct an empty ObjectObject, populate it with FBSValues, and just put it directly into the scope Map:

ObjectObject myvar = new ObjectObject();
try {
    myvar.put("p1", FBSUtility.wrap("part 1"));
    myvar.put("p2", FBSUtility.wrap("part 2"));
} catch (InterpretException e) {
    e.printStackTrace();
}
Map<String, Object> applicationScope = ExtLibUtil.getApplicationScope();
applicationScope.put("myvarname", myvar);

稍后检索它时(如您提供的示例中所示),SSJS 会将其视为 JSON,而 Java 将完全按照存储时的方式查看它.

When retrieving it later (as in the examples you provided), SSJS will see it as JSON, Java will see it exactly as it was stored.

如果您需要存储更深的层次结构,除了基元之外,您还可以将 ArrayObject 和 ObjectObject 的实例放在 ObjectObject 中,因此,就像 JSON 本身一样,您可以根据需要将它们嵌套得尽可能深.

If you need to store deeper hierarchies, you can put instances of ArrayObject and ObjectObject inside an ObjectObject in addition to primitives, so, just like JSON itself, you can nest these as deep as you need.

如果您要将其存储在高于 requestScope 的任何位置,请确保仅包含真正的 JSON(字符串、数字、布尔值、数组、对象);具体来说,FunctionObject 没有实现 Serializable,所以 JSON 是安全的存储,JavaScript 不是.严格来说,这只会在 8.5.2 和 8.5.3 中存储在 viewScope 中时变得有毒(即使如此,只有当应用程序的持久性选项未设置为将所有页面都保存在内存中时).但是,如果 IBM 实现了集群支持,那么存储在 sessionScope 和 applicationScope 中的所有对象都需要可序列化以允许服务器间状态传输……因此,为了使设计面向未来,最好坚持这一点任何存储时间超过单个请求持续时间的原则.

Just be sure to only include true JSON (strings, numbers, booleans, arrays, objects) if you'll be storing it anywhere higher than the requestScope; specifically, FunctionObject does not implement Serializable, so JSON is safe to store, JavaScript is not. Strictly speaking, this only becomes toxic when stored in the viewScope in 8.5.2 and 8.5.3 (and even then, only when the application's persistence option is not set to keep all pages in memory). But if IBM ever implements cluster support, then all objects stored in sessionScope and applicationScope will need to be serializable to allow for inter-server state transport... so, in the interest of future-proofing the design, it's wise to hold to this principle for anything stored longer than the duration of a single request.

这篇关于如何将 JSON 对象添加到 Java 中的作用域变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆