0

鉴于:

我有一个List<ComplexObjectThatContainsOtherObjectsAndEvenLists>并且我想跨页面/请求保留这些数据。这个对象非常大,包含多达 1000 个对象。

当前实施:

我目前正在做的是serializing使用下面的这个复杂对象(我刚刚在 SO 中找到了这个代码,我很感谢作者,不幸的是我不记得了,我很抱歉)

    public static String serialize(Object object) {
        ByteArrayOutputStream byteaOut = new ByteArrayOutputStream();
        GZIPOutputStream gzipOut = null;
        try {
            gzipOut = new GZIPOutputStream(new Base64OutputStream(byteaOut));
            gzipOut.write(new Gson().toJson(object).getBytes("UTF-8"));
        } catch(Exception e) {
            return null;
        } finally {
            if (gzipOut != null) try { gzipOut.close(); } catch (IOException logOrIgnore) {}
        }

        return new String(byteaOut.toByteArray());
    }

并将String输出隐藏<input type="hidden">在我的页面中,并在需要时将其传递回我的控制器。该字符串的长度约为 1300-2000 个字符。

问题:

String在会话中保存这个更好吗?(见下文)

session.setAttribute("mySerializedString", mySerializedString);

能否请您提供优点和缺点?

到目前为止我的优点和缺点(虽然我不确定):

我不确定,但hidden我认为实现在页面呈现时(因为它太长)以及当它提交回控制器时会产生影响,尽管如果我选择手动取消设置会话变量,这不会给我带来麻烦session执行。

4

0 回答 0