【问题标题】:Store sessionScope java.util.TreeMap variable in a document in xPage将 sessionScope java.util.TreeMap 变量存储在 xPage 的文档中
【发布时间】:2014-02-28 01:35:39
【问题描述】:

我正在开发一个应用程序,我正在创建一个java.util.TreeMap,其中包含从应用程序的各种其他文档中获取的数据,然后将该treemap 分配给一个sessionScope 变量。这工作正常。 现在我想提供一个功能,我需要将此地图存储在 NotesDocument 中。

但是当我尝试这样做时,我收到了一个错误。

var doc:NotesDocument = database.createDocument();
doc.replaceItemValue("Form","testForm");
print("json = "+sessionScope.get("Chart_Map"));
doc.replaceItemValue("Calender_Map",sessionScope.get("Chart_Map"));
doc.save();

例外:

执行 JavaScript 动作表达式时出错 脚本解释器错误,line=4, col=13: [TypeError] 调用方法 NotesDocument.replaceItemValue(string, java.util.TreeMap) null**发生异常

是否可以将java.util.TreeMap 存储在notesdocument 字段中?

如果是,那么如何实现?

如果没有,那为什么不呢?这和serializability有关吗?

【问题讨论】:

    标签: xpages lotus-notes lotus-domino xpages-ssjs serverside-javascript


    【解决方案1】:

    除非您使用 MimeDomino Document 数据源,否则您不能将 Java 对象存储在 Document 字段中 http://www.openntf.org/main.nsf/blog.xsp?permaLink=NHEF-8XLA83

    或者甚至更好的是内置此功能的新 openntf Domino API http://www.openntf.org/main.nsf/project.xsp?r=project/OpenNTF%20Domino%20API

    使用 MimeStorage

    【讨论】:

    • 小对象可以作为CustomDataBytes存储在一个item中。
    • 我不知道replaceItemValueCustomData方法的使用。我尝试使用它,它创造了奇迹。这正是我想要的。它有 64k 的限制,但我可以通过某种或其他方式解决它。谢谢
    • 我很高兴使用 OpenNTF Domino API 将地图的地图存储在 Notes 项中。它为快速访问报告数据提供了极大的灵活性。
    【解决方案2】:

    Fredrik 是对的,MimeDomino 最有意义。如果您还没有准备好并且您的字段对于普通的 Notes 项目来说不是太大,您可以按照 Sven 的建议使用 CustomDataBytes - 或者通过子类化 TreeMap 来使用 JSON。它可能看起来像这样:

    import java.util.TreeMap;
    import java.util.Vector;
    import com.google.gson.Gson;
    import com.google.gson.JsonSyntaxException;
    import lotus.domino.Item;
    import lotus.domino.NotesException;
    
    public class TreeMapItem extends TreeMap<String, String> {
        private static final long   serialVersionUID    = 1L;
        public static TreeMapItem load(Item source) throws JsonSyntaxException, NotesException {
            Gson g = new Gson();
            TreeMapItem result = g.fromJson(source.getText(), TreeMapItem.class);
            return result;
        }
        public void save(Item target) throws NotesException {
            Gson g = new Gson();
            target.setValueString(g.toJson(this));
        }
    }
    

    我使用了 Google 的 Gson,这很简单,但您可能需要将其部署为插件,以便 Java 安全性工作。 XPages 中也有内置的 JSON - 更多的工作。另一种方法是在 Domino 中使用 2 个字段,一个用于加载键,一个用于值 - 这将符合经典的 Domino 实践。

    第三种方法是存储使用竖线字符分隔的值:

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void saveCompact(Item target) throws NotesException {
        Vector v = new Vector();
        for (Map.Entry<String, String> me : this.entrySet()) {
            v.add(me.getKey()+"|"+me.getValue());
        }
        target.setValues(v);
    }
    
        @SuppressWarnings("rawtypes")
    public static TreeMapItem loadCompact(Item source) throws NotesException {
        TreeMapItem result = new TreeMapItem();
        Vector v = source.getValues();
        for (Object o : v) {
            String[] candidate = o.toString().split("|");
            if (candidate.length > 1) {
                result.put(candidate[0], candidate[1]);
            }
        }
        return result;
    }
    

    让我们知道它是如何为您工作的

    【讨论】:

    • 感谢您提供多种解决方案。
    • 我不知道方法 replaceItemValueCustomData。我尝试使用它,它创造了奇迹。这正是我想要的。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多